ML Skills
Docker
Attach And Detach
Last updated on May 25, 2021

attach-detach

It is common and necessary if we want to take a look on what is happening inside a container by interacting with it. In this post, we will see how to attach and detach from a container.

Detach

In the previous post about Docker Image and Container, we knew how to use docker run command.

Basically, this docker run command:

  • Starts a container from an image
  • Starts a main process running in the created container. When this main process stops, the container stops.

Let's run a container with the interactive terminal

docker run -it ubuntu bash

We already knew that if we press ctrl + d, the container will exit. But what if we want to jump out of the container's terminal and we also want to leave it running in the background? Let's press ctrl + p then ctrl + q to do that!

Now, check whether it's still running or not

docker ps

docker-ps-1

Okay, the container is still running! This is called detaching.

Trick: To run a container and leave it running in the background, we can use flag -d with docker run command.

Attach

To jump into the running container, we type:

docker attach eb8566be48f8 # container id in my case

docker-attach

Okay, good job! We are inside the main process of the container!

Let's leave this current terminal there and open a new terminal. Now we want to create another process in the running container. This is very common when we want to see something inside it or to figure out what is happening. Let's type:

docker exec -it eb8566be48f8 bash
touch FILE
ls

docker-exec-1

We have created a file named FILE. This file is also existed in the attaching terminal also because they are pointing at the same container.

If we exit the process in the 2nd terminal where we use exec command to jump into the container, the container is still running. But if we exit the process in the 1st terminal where we use attach command to open the container's terminal, the container will stop because this terminal is attaching into the container's main process.

Keynotes

  • Run and detach a container (leave the main process running in the background).
docker run -d -it ubuntu bash
docker run -d ubuntu top -b
  • To detach a container when we're inside its terminal, press ctrl + p ctrl + q.

  • Attach into a running container

docker attach container_id
  • Add a process to a running container
docker exec -it container_id bash
docker exec -d container_id top -b