How to use docker container attach command to interact with running containers

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker container attach command to interact with running containers. You will start containers in detached mode, attach to them to observe their output, and detach using the default key sequence.

You will also explore attaching to containers with TTY enabled and learn how to override the default detach key sequence for greater flexibility when interacting with your Docker containers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/attach("Attach to Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555103{{"How to use docker container attach command to interact with running containers"}} docker/ps -.-> lab-555103{{"How to use docker container attach command to interact with running containers"}} docker/attach -.-> lab-555103{{"How to use docker container attach command to interact with running containers"}} docker/pull -.-> lab-555103{{"How to use docker container attach command to interact with running containers"}} end

Start a detached container running a simple command

In this step, you will learn how to start a Docker container in detached mode. When a container runs in detached mode, it runs in the background, and the Docker CLI does not attach to the container's standard input, output, or error streams. This is useful for running long-running services or applications.

We will use the ubuntu image to run a simple command that prints a message and then exits. First, let's pull the ubuntu image to ensure it's available locally.

docker pull ubuntu

Now, let's run a container in detached mode using the docker run command with the -d flag. We will run a simple echo command that prints "Hello from detached container!" and then exits.

docker run -d ubuntu echo "Hello from detached container!"

After running the command, Docker will print the container ID. This indicates that the container has been started in detached mode. You can verify that the container is running (or has completed its task) using the docker ps -a command, which shows all containers, including those that have exited.

docker ps -a

You should see an entry for the container you just started. The STATUS column will show whether the container is still running or has exited. Since our command is very short, the container will likely have exited by the time you run docker ps -a.

Attach to the running container and observe its output

In the previous step, we started a container in detached mode. Although the container ran a simple command and likely exited quickly, we can still attach to it to see its output. The docker attach command allows you to connect to the standard input, output, and error streams of a running or exited container.

First, let's get the ID of the container we started in the previous step. You can use the docker ps -a command and look for the container running the echo command. Copy the container ID.

docker ps -a

Now, use the docker attach command followed by the container ID to attach to the container.

docker attach <container_id>

Replace <container_id> with the actual ID of your container. Since the container ran a simple echo command and exited, you might not see any output immediately, or you might see the output "Hello from detached container!" if the output is still buffered. The attach command will connect you to the container's streams. Because the container has already finished its task, the attach command will likely exit immediately after connecting.

To demonstrate attaching to a container that stays running, let's start a new detached container that runs a command which keeps it alive for a few seconds. We'll use the ubuntu image again and run sleep 10.

docker run -d ubuntu sleep 10

Get the ID of this new container using docker ps. This time, the container should be in the Up state.

docker ps

Now, attach to this running container using its ID.

docker attach <new_container_id>

Replace <new_container_id> with the ID of the container running sleep 10. You will be attached to the container's standard streams. Since the sleep command doesn't produce output, you won't see anything. The terminal will appear to be waiting. The container will run for 10 seconds and then exit. When the container exits, the attach command will also exit, returning you to your terminal prompt.

Detach from the container using the default key sequence

In the previous step, you attached to a running container. When you are attached to a container, you are connected to its standard input, output, and error streams. To detach from the container without stopping it, you can use a special key sequence. By default, this sequence is CTRL+p followed by CTRL+q.

Let's start a new detached container that will run for a longer period so you have time to practice detaching. We'll use the ubuntu image and run sleep 60.

docker run -d ubuntu sleep 60

Get the ID of this new container using docker ps.

docker ps

Now, attach to this running container using its ID.

docker attach <container_id>

Replace <container_id> with the ID of the container running sleep 60. You are now attached to the container. To detach from the container and return to your terminal prompt without stopping the container, press the following key sequence:

  1. Press and hold the CTRL key, then press the p key. Release both keys.
  2. Press and hold the CTRL key, then press the q key. Release both keys.

After pressing CTRL+p followed by CTRL+q, you should be returned to your terminal prompt. The container will continue to run in the background. You can verify that the container is still running using docker ps.

docker ps

You should see the container running sleep 60 listed with a status of Up.

Start another detached container with TTY enabled

In this step, we will start another detached container, but this time we will enable a pseudo-TTY (Terminal) for the container. Enabling a TTY is often necessary when you want to interact with a container's shell or run commands that expect a terminal.

We will use the docker run command with the -d flag for detached mode and the -t flag to allocate a pseudo-TTY. We will run a simple command that keeps the container running, like sleep 60.

docker run -d -t ubuntu sleep 60

The -t flag allocates a pseudo-TTY, which is a virtual terminal that allows you to interact with the container as if you were connected to a physical terminal. Even though the container is running in detached mode (-d), having a TTY enabled is important for interactive processes or when you plan to attach to the container later and need a terminal environment.

After running the command, Docker will again print the container ID, indicating that the container has been started in detached mode with a TTY. You can verify that the container is running using docker ps.

docker ps

You should see the new container listed with a status of Up. Note the container ID, as you will need it in the next step to attach to this container.

Attach to the TTY-enabled container and detach using the default key sequence

In the previous step, we started a detached container with a TTY enabled. Now, we will attach to this container and practice detaching using the default key sequence (CTRL+p followed by CTRL+q). Attaching to a container with a TTY allows for interactive sessions, even if the initial command doesn't require one.

First, get the ID of the container you started in the previous step that is running sleep 60 with TTY enabled.

docker ps

Find the container ID for the ubuntu container running sleep 60.

Now, attach to this container using the docker attach command followed by the container ID.

docker attach <container_id>

Replace <container_id> with the actual ID of your container. You are now attached to the container's standard streams, and because a TTY is enabled, you have a terminal-like interface. Although the sleep command doesn't require interaction, you are connected to the container's virtual terminal.

To detach from the container without stopping it, use the default detach key sequence:

  1. Press and hold the CTRL key, then press the p key. Release both keys.
  2. Press and hold the CTRL key, then press the q key. Release both keys.

You should be returned to your terminal prompt. The container will continue to run in the background until the sleep 60 command finishes. You can verify that the container is still running using docker ps.

docker ps

You should see the container running sleep 60 listed with a status of Up.

Attach to a container and override the detach key sequence

In this final step, you will learn how to override the default detach key sequence when attaching to a container. This can be useful if the default sequence conflicts with other applications or if you prefer a different key combination.

We will use the docker attach command with the --detach-keys flag to specify a different key sequence. The format for the --detach-keys flag is sequence, where sequence is a comma-separated string of key combinations. For example, ctrl-a,ctrl-d would set the detach sequence to CTRL+a followed by CTRL+d.

First, let's start a new detached container with TTY enabled that will run for a while.

docker run -d -t ubuntu sleep 60

Get the ID of this new container using docker ps.

docker ps

Now, attach to this container, but this time, specify a different detach key sequence using the --detach-keys flag. Let's use ctrl-a,d as the new sequence. This means you will press CTRL+a followed by d to detach.

docker attach --detach-keys="ctrl-a,d" <container_id>

Replace <container_id> with the actual ID of your container. You are now attached to the container. To detach using the new sequence:

  1. Press and hold the CTRL key, then press the a key. Release both keys.
  2. Press the d key.

You should be returned to your terminal prompt. The container will continue to run in the background. You can verify that the container is still running using docker ps.

docker ps

You should see the container running sleep 60 listed with a status of Up.

Summary

In this lab, you learned how to start a Docker container in detached mode using the docker run -d command, which allows containers to run in the background without attaching to the terminal. You then practiced using the docker attach command to connect to the standard streams of a running or exited container to observe its output.

Furthermore, you explored how to detach from an attached container using the default key sequence (CTRL+p CTRL+q) and how the behavior of docker attach changes when the container is started with TTY enabled (-t). Finally, you learned how to override the default detach key sequence when attaching to a container, providing flexibility in managing your container interactions.