Docker

How to Attach and Detach from Docker Containers

In the application deployment approaches, there are instances where the developer wants to interact with containerized applications or integrate into a running container via the shell prompt. Moreover, in the case of halting or detaching the running containers. In such situations, attaching and detaching from Docker containers is of great aid in debugging the application issues.

How to Attach and Detach from Docker Containers?

There are various modes in which the Docker containers can run, listed below:

    • Default Mode
    • Interactive Mode
    • Detached Mode

Let’s delve into each of the modes individually.

Default Mode

A container can be created using the “container run” cmdlet. It is such that the container starts in the foreground:

docker container run --name web-server-one nginx:alpine

 

The above-executed cmdlet initiates the “NGINX” web server in a container. Also, in default mode, returning back to the shell prompt is impossible as long as the container process is executing. Therefore, stop/halt the container by hitting the “CTRL + C” keys.

Now, check the status of the container via the below-stated cmdlet:

docker container ls -a --filter 'name=web-server-one'

 

Here, it can be implied that the container is “Exited”. It occurs as the “CTRL + C” sends/transmits a “SIGKILL” signal to the container, which terminates/ends the container.

Interactive Mode

Likewise, the “container run” command can also be executed to initiate the container in the interactive mode using “-i” and “-t” parameters/options:

    • The “-i” option attaches/integrates the stdin of the terminal with the container.
    • The “-t” option, however, assigns a pseudo-terminal. This enables the developer to interact with the container via the terminal:

 

docker container run -it --name web-server-two nginx:alpine sh

 

Now, exit/close this container via the below-given cmdlet using its id:

docker container kill 4502e5ad4847

 

After that, analyze the container’s status via the discussed cmdlet:

docker container ls -a --filter 'name=web-server-two'

 

From here, it can be verified that the container has transitioned to the “Exited” status.

Detached Mode

Docker enables the developer to execute the container in the background as well via the “-d” option using the following cmdlet:

docker container run -d --name web-server-3 -p 80:80 nginx:alpine

 

This cmdlet executes a container in the background, displays its id, and gives a shell. The “-p” option publishes a port that maps port 80 in the container to port 80 on the Docker host.

Now, verify it by analyzing the container’s status:

docker container ls -a --filter 'name=web-server-3'

 

Here, it can be implied that the container is executing in the background.

How to Attach to a Running/Executing Docker Container?

To attach to a running/executing container, utilize the docker “attach” child cmdlet, as follows:

docker attach web-server-3

 

As seen, the logs are displayed on the terminal accordingly.

This cmdlet attaches/integrates input, output, and error streams to the “web-server-3” container.

Note: After running this cmdlet, navigate to the “http://localhost/” URL from the web browser that displays the following message:


Upon doing so, the container logs will be displayed on the terminal and the above cmdlet will be executed.

Note: In the above demonstration, the most recent container i.e., detached mode is attached. Also, note that a stopped container cannot be attached.

Lastly, exit/leave from the container by triggering the “CTRL + C” keys and analyze its status using the below-stated command:

docker container ls -a --filter 'name=web-server-3'

 

Here, it can be analyzed that the container moves to the “Exited” status successfully.

How to Detach from a Running/Executing Container?

Detaching from the container varies with respect to the mode in which the container is executing. Let’s discuss the detaching from the containers in each mode:

Default Mode

In this mode, the “CTRL + C” keys can be utilized to detach from the container, discussed before that sends/transmits a “SIGKILL” signal to the container. This results in the container to move to the “Exited” status instead of disconnecting the session.

This behavior can be overridden using the “–sig-proxy” option.

To do so, firstly, initiate the container in default mode via the “–sig-proxy” option:

docker container run --name web-server-four --sig-proxy=false nginx:alpine

 

Now, detach from the container via the “CTRL + C” keys and analyze the container’s status:

docker container ls -a --filter 'name=web-server-four'

 

In the above cmdlet, it can be implied that the container is detached without affecting its running state.

Interactive Mode

Detaching in this specific mode can be carried out likewise, via the discussed “CTRL + C” keys by firstly, starting the container in an interactive mode via the following cmdlet:

docker container run -it --name web-server-five nginx:alpine sh

 

In the next step, detach from it by hitting the “Ctrl + C” keys and analyze its status utilizing the below-provided cmdlet:

docker container ls -a --filter 'name=web-server-five'

 

In this example, as analyzed, the detach key combination terminates/ends the current interactive session only, thereby keeping the container in an executing/running state.

Detached Mode

In this mode, there is a minor change. It is such that the “–sig-proxy” option must be disabled while attaching/integrating to the container. To do so, first of all, start the container in the detached mode via this cmdlet:

docker container run -d --name web-server-six nginx:alpine

 

After that, attach to it via “–sig-proxy=false” value and exit from it/close it by hitting the “Ctrl + C” keys:

docker container attach --sig-proxy=false web-server-six

 

Finally, analyze the container’s status:

docker container ls -a --filter 'name=web-server-six'

 

In this outcome, it can be observed that the container is detached appropriately without affecting its running state.

Conclusion

To attach from the Docker Containers, the “attach” cmdlet is used and to detach from the Docker container, mostly the “Ctrl+C” keys are used. Also, the stopped container cannot be attached. This article discussed attaching and detaching from Docker containers along with multiple modes in which the Docker containers can be executed.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.