How to run docker build command inside a docker container?

53 views Asked by At

This is related to working with CI/CD, I have a image in my private repo say my_image, when we trigger the pipeline gitlab-runner (shared) will pickup the image and run our ci commands on top of that.

My requirement is I need to have docker inside the image, so that output of some command "say build " will return a valid docker file and I need to build image on top of that file, using docker build

I am wondering is there any way to achieve this?.

Some context :

  • I won't be having root access to the environment on which the runner is executing the job.
  • Same is the case for the docker socket (no super user role will be given)

Thanks for any help..

I tried this with installing docker inside the container, docker -v command is working but no other docker command is working and they are failing saying (Cannot connect to docker daemon, is the daemon running?)

I don't think I can use priviliged containers accounting to the various security concerns.

I cannot mount docker binaries while running the image, since the job is being executed by the runner ( I think so, if I am wrong point it out..)

1

There are 1 answers

1
Antonio Petricca On

Yoi have to write a basic docker file like the following:

FROM <MY-BASE-IMAGE>

RUN apt update -y
RUN apt install -y curl
RUN curl https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz | tar xvz -C /tmp/ && mv /tmp/docker/docker /usr/bin/docker

Then, when you run your container image, use the -v /var/run/docker.sock:/var/run/docker.sock argument to allow the embedded docker client to run by the host docker daemon.