Build docker image from a docker container

For ease of understanding, I will try to use manual operation instead of dockerfile
Start a Ubuntu container first

docker run -i -t --name base ubuntu /bin/bash

If you install docker in this container and try to execute docker PS, you will get the following error

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

The reason is actually very simple, because there is docker inside the container In fact, the simplest way to build an image in a container is to mount the sock, and then the container will share the same docker with the host sock

docker run -i -t --name base2 -v /var/run/docker.sock:/var/run/docker.sock ubuntu /bin/bash

However, the side effect is that because the host’s sock is used, all containers running on the host can be seen here, and actually use the same docker environment as the host

Create a dockerfile

FROM nginx
RUN echo 'hello dockerfile' > /usr/share/nginx/html/index.html

Build again

error checking context: 'no permission to read from '/proc/1/mem''.

This is because the current container does not have the root permission to add the host when it is created
Add –privileged

docker run -i -t --name base3 --privileged -v /var/run/docker.sock:/var/run/docker.sock ubuntu /bin/bash

Here is an episode. If you use dockerfile directly under / path, you will get an error

error checking context: ‘file (‘/proc/4427/fd/5′) not found or excluded by . dockerignore’.

The solution is not to build under the / root path

Send a Message