Attach and Detach from Docker Container

1. Overview

This tutorial is going to cover how to attach and detach from Docker container. It could be necessary either to view its ongoing output or to control it interactively and then detach from it when we’re done.

2. Attach to a Running Docker Container

To attach to a running Docker container, we can use the docker attach command which has the syntax as the following:

Here are some command’s options:

Name, shorthand Default Description
–detach-keys Table Override the key sequence for detaching a container
–no-stdin false Do not attach STDIN
–sig-proxy true Proxy all received signals to the process

Example:

We can attach to the same contained process multiple times simultaneously, screen sharing style, or quickly view the progress of our detached process.

Let’s say we have an opensuse docker and start it in background as the following:

To attach to the above running container, we can issue the following command:

Then, it will let us connect with the attachdemo container, and we can see the output of the top command.

3. Detach from a Docker Container

3.1. Detach from a Docker container and stop it

To detach from a Docker container and stop it, we can use CTRL-c key sequence which will  send SIGKILL to the container. If –sig-proxy is true (the default),CTRL-c will send a SIGINT to the container.

3.2. Detach from a Docker container and leave it running

In general, to detach from a Docker container and leave it running, we can use the CTRL-p CTRL-q key sequence.

Points to note:

To detach from a Docker container, we often use CTRL-c, this often causes the container to be stopped. To work around, we can use the -sig-proxy is false when attach a running container.

Example:

Let’s start a Docker container:

And then attach to it with -sig-proxy

Let’s use CTRL-c to detach from the container and then check its status:

We can see the container is still alive.

4. Get Into a Docker Container

When we attach to a running Docker container, we often need to control, monitor it or just simply for debugging purpose. In such a case, to execute an interactive bash shell on the container is most likely preferred. To do that, we can use the docker exec command which is used to run a command in a running container.

Example:

Let’s create a Docker container:

And then execute an interactive bash shell on the above container:

Let’s see the terminal:

We now can do whatever with the bash and detach from a Docker container and leave it running by CTRL-c.

5. Conclusion

We just got through how to attach to a running Docker container, how to detach from a Docker container and how to get into a Docker container by executing an interactive bash shell on the container. Those are useful when we often work with Docker.

Here are other tutorials for your reference:

How To Remove Unused Docker Images, Containers, Volumes, and Networks

How To Pull A Docker Image And Run A Container

Copy Files, Folders from Host to Docker Container and Vice Versa

Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step

Install Docker on CentOS 7.X

Introduction to Docker Compose

Vagrant Docker Provider Tutorial

Using Apache Kafka Docker

Insecure docker registry

How to Get IP Address of a Docker Container