Attach and detach from Docker's process?

Docker is a fantastic tool for containerization that provides the flexibility of isolating your application with its environment on a single system. One of the features Docker offers is the ability to attach and detach from Docker processes. This post provides a step-by-step guide to working with this feature.

Prerequisites

  • Basic knowledge of Docker.
  • Docker installed on your machine. Refer this for Docker Installation.

Step 1: Start a Docker Container in detach mode

Firstly, we need to have a Docker container up and running. You can either use an existing container or create a new one for this tutorial. Let's use a basic Ubuntu container as an example.

1docker run -d --name my_container -p 8080:8080 

The flags used here are:

  • -d or --detach: This runs the container in the background.
  • -i or --interactive: This keeps STDIN open even if not attached.
  • -t or --tty: This allocates a pseudo-TTY.

This command will start an Ubuntu container with bash and it will keep running in the background.


Step 2: Verify the Running Docker Container

To verify that your Docker container is running, use the docker ps command:

1docker ps

You should see your running container in the output, something similar to this:

1CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
21d3c94dda367   ubuntu    "bash"    About a minute ago   Up About a minute          my_container

Step 3: Attach to a Docker Container

Now, let's attach to the running Docker container using the docker attach command followed by the container ID or name:

1docker attach my_container

Once attached, your terminal effectively becomes the terminal of the running Docker container. If your container runs an interactive process like a shell, you can interact with it.


Step 4: Detach From a Docker Container

While being attached to the container, if you want to detach without stopping it, you can use the escape sequence, which is CTRL-p CTRL-q. This allows you to leave the container running in the background.

Remember, if you simply press CTRL-c or CTRL-d, it will stop the container, because these commands send a SIGINT or EOF signal, respectively, to the main process inside the container, causing it to terminate.


Step 5: Reattach to the Docker Container

You can reattach to the container anytime using the docker attach command as we did in step 3.


Alternative: docker exec

An alternative to docker attach is to use docker exec to run a new process (like a shell) inside the running container. This is handy if you want to have an interactive shell inside the container but don't want to worry about accidentally stopping the container when you exit:

1docker exec -it my_container bash

Now you can exit this shell using exit or CTRL-d without stopping the container.



Miscellaneous : Search and attach container running in the background

If the docker container is running in the background then it might be difficult for you to find the container. So let's run the following command to check the stats of the container -

1docker stats

The fact that a container is running in the background does not prevent you from interacting with it. You can attach to it, run commands, and then detach again using the methods we discussed above in the post.


Troubleshooting

If you're having trouble attaching to or detaching from a Docker container, here are some common issues and their solutions:

Problem: Docker container immediately exits after starting.

Solution: Ensure that the Docker container has a long-running process. A Docker container runs as long as its main process

Posts in this Series