Docker Community Forums

Share and learn in the Docker community.

Please help me docker keep failing

docker: error during connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/containers/create: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.
See ‘docker run --help’.

Step 1 — Resolving Problems with the Dockerfile
The most common place you may run into issues is when you’re building your Docker image from a Dockerfile. Before we dive in, let’s clarify the difference between images and containers.

An image is a read-only resource that you create using a configuration file called Dockerfile. It’s what you ship and share through Docker Hub or your private registry.
A container, is a read and write instance that your create out of the image you built.
You can learn more about these concepts in the tutorial Docker Explained: Using Dockerfiles to automate building of images.

When you look at a Dockerfile, you can clearly see the step-by-step process Docker uses build the image because each line in the Dockerfile corresponds to a step in the process. This generally means that if you got to a certain step, then all of the previous steps completed successfully.

Let’s create a little project to explore some issues you might encounter with a Dockerfile. Create a docker_image directory in your home directory, and use nano or your favorite editor to create a Dockerfile in that folder

mkdir ~/docker_image
nano ~/docker_image/Dockerfile
Add the following content to this new file:

~/docker_image/Dockerfile

base image

FROM debian:latest

install basic apps

RUN aapt-get install -qy nano
There’s an intentional typo in this code. Can you spot it? Try to build an image from this file to see how Docker handles a bad command. Create the image with the following command:

docker build -t my_image ~/docker_image
You’ll see this message in your terminal, indicating an error:

Output
Step 2 : RUN aapt-get install -qy nano
—> Running in 085fa10ffcc2
/bin/sh: 1: aapt-get: not found
The command ‘/bin/sh -c aapt-get install -qy nano’ returned a non-zero code: 127
The error message at the end means that there was a problem with the command in Step 2. In this case it was our intentional typo: we have aapt-get instead of apt-get. But that also meant that the previous step executed correctly.

Modify the Dockerfile and make the correction:

Dockerfile

install basic apps

RUN apt-get install -qy nano
Now run the docker build command again:

docker build -t my_image ~/docker_image
And now you’ll see the following output:

Output
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:latest
—> ddf73f48a05d
Step 2 : RUN apt-get install -qy nano
—> Running in 9679323b942f
Reading package lists…
Building dependency tree…
E: Unable to locate package nano
The command ‘/bin/sh -c apt-get install -qy nano’ returned a non-zero code: 100
With the typo corrected, the process moved a little faster, since Docker cached the first step rather than redownloading the base image. But as you can see from the output, we have a new error.

The Debian distribution we’ve used as the foundation for our image couldn’t find the text editor nano, even though we know it is available on the Debian package repositories. The base image comes with cached metadata, such as repositories and lists of available packages. You may occasionally experience some cache issues when the live repositories you’re pulling data from have changed.

To fix this, modify the Dockerfile to do a cleanup and update of the sources before you install any new packages. Open the configuration file again:

nano ~/docker_image/Dockerfile
Add the following highlighted line to the file, above the command to install nano:

~/docker_image/Dockerfile

base image

FROM debian:latest

clean and update sources

RUN apt-get clean && apt-get update

install basic apps

RUN apt-get install -qy nano
Save the file and run the docker build command again:

docker build -t my_image ~/docker_image
This time the process completes successfully.

Output
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:latest
—> a24c3183e910
Step 2 : RUN apt-get install -qy nano
—> Running in 2237d254f172
Reading package lists…
Building dependency tree…
Reading state information…
Suggested packages:
spell
The following NEW packages will be installed:
nano

—> 64ff1d3d71d6
Removing intermediate container 2237d254f172
Successfully built 64ff1d3d71d6
Let’s see what happens when we add Python 3 and the PostgreSQL driver to our image. Open the Dockerfile again.

nano ~/docker_image/Dockerfile
And add two new steps to install Python 3 and the Python PostgreSQL driver:

~/docker_image/Dockerfile

base image

FROM debian:latest

clean and update sources

RUN apt-get clean && apt-get update

install basic apps

RUN apt-get install -qy nano

install Python and modules

RUN apt-get install -qy python3
RUN apt-get install -qy python3-psycopg2
Save the file, exit the editor, and build the image again:

docker build -t my_image ~/docker_image
As you can see from the output, the packages install correctly. The process also completes much more quickly because the previous steps were cached.

Output
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:latest
—> ddf73f48a05d
Step 2 : RUN apt-get clean && apt-get update
—> Using cache
—> 2c5013476fbf
Step 3 : RUN apt-get install -qy nano
—> Using cache
—> 4b77ac535cca
Step 4 : RUN apt-get install -qy python3
—> Running in 93f2d795fefc
Reading package lists…
Building dependency tree…
Reading state information…
The following extra packages will be installed:
krb5-locales libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2
libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 libnettle4
libp11-kit0 libpq5 libsasl2-2 libsasl2-modules libsasl2-modules-db
libtasn1-6
Suggested packages:
gnutls-bin krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap
libsasl2-modules-sql libsasl2-modules-gssapi-mit
libsasl2-modules-gssapi-heimdal python-psycopg2-doc
The following NEW packages will be installed:
krb5-locales libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2
libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libldap-2.4-2 libnettle4
libp11-kit0 libpq5 libsasl2-2 libsasl2-modules libsasl2-modules-db
libtasn1-6 python3-psycopg2
0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded.
Need to get 5416 kB of archives.
After this operation, 10.4 MB of additional disk space will be used.

Processing triggers for libc-bin (2.19-18+deb8u6) …
—> 978e0fa7afa7
Removing intermediate container d7d4376c9f0d
Successfully built 978e0fa7afa7
Note: Docker caches the build process, so you may run into a situation where you run an update in the build, Docker caches this update, and some time later your base distribution updates its sources again, leaving you with outdated sources, despite doing a cleanup and update in your Dockerfile. If you run into issues installing or updating packages inside the container, run apt-get clean && apt-get update inside of the container.

Pay close attention to the Docker output to identify where the typos are, and run updates at build time and inside the container to make sure you’re not being hindered by cached package lists.

Syntax errors and caching problems are the most common issues you may encounter when building an image in Docker. Now let’s look at problems that may arise when running containers from those images.

Step 2 — Resolving Container Naming Issues
As you launch more containers, you will eventually come across name collisions. A naming collision is where you try to create a container that has the same name as a container that already exists on your system. Let’s explore how to properly deal with naming, renaming, and deleting containers in order to avoid collisions.

Let’s launch a container from the image we built on the previous section. We will run an interactive bash interpreter inside this container to test things out. Execute the following command:

docker run -ti my_image bash
When the container starts, you’ll see a root prompt waiting for instructions:

Now that you have a running container, let’s look at what kinds of problems you might run into.

When you run a container the way you just did, without explicitly setting a name, Docker assigns a random name to the container. You can see all of the running containers and their corresponding names by running the docker ps command on the Docker host, outside of the running container.

Open a new terminal on the Docker host and run the following command:

docker ps
This command outputs the list of running containers with their names as show in the following example:

Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80a0ca58d6ec my_image “bash” 22 seconds ago Up 28 seconds loving_brahmagupta
The name loving_brahmagupta in the preceding output is the name that Docker automatically assigned to the container in the preceding example; yours will have a different name. Letting Docker assign a name to your container is fine in very simple cases, but can present significant problems; when we deploy we need to name containers consistently so we can reference them and automate them easily.

To specify a name for a container we can either use the --name argument when we launch the container, or we can rename a running container to something more descriptive.

Run the following command from the Docker host’s terminal:

docker rename your_container_name python_box
Then list your containers:

docker ps
You’ll see the python_box container in the output, confirming that you successfully renamed the container:

Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80a0ca58d6ec my_image “bash” 24 minutes ago Up 24 minutes python_box
To close the container, type exit at the prompt in the terminal containing the running container:

exit
If that’s not an option, you can kill the container from another terminal on the Docker host with the following command:

docker kill python_box
When you kill the container this way, Docker returns the name of the container that was just killed:

Output
python_box
To make sure python_box doesn’t exist anymore, list all of the running containers again:

docker ps
As expected, the container is no longer listed:

Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Now you might think you could launch another container named python_box, but let’s see what happens when we try.

We’ll use the --name argument this time for setting the container’s name:

docker run --name python_box -ti my_image bash
Output
docker: Error response from daemon: Conflict. The name “/python_box” is already in use by container 80a0ca58d6ecc80b305463aff2a68c4cbe36f7bda15e680651830fc5f9dda772. You have to remove (or rename) that container to be able to reuse that name…
See ‘docker run --help’.
When you build an image and reuse the name of an existing image, the existing image will be overwritten, as you’ve seen already. Containers are a little more complicated because you can’t overwrite a container that already exists.

Docker says python_box already exists even though we just killed it and it’s not even listed with docker ps. It’s not running, but it’s still available in case you want to start it up again. We stopped it, but we didn’t remove it. The docker ps command only shows running containers, not all containers.

To list all of the Docker containers, running and otherwise, pass the -a flag (alias for --all) to docker ps:

docker ps -a
Now our python_box container appears in the output:

Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80a0ca58d6ec my_image “bash” 12 minutes ago Exited (137) 6 minutes ago python_box
The container exists with an Exited (137) status, which is why we ran into the naming problem when we tried to create a new container with the same name.

When you want to completely remove a container, you use the docker rm command. Execute this command in your terminal:

docker rm python_box
Once again, Docker outputs the name of the container that was just removed:

Output

@lewish95 Thank you so.much