Home » Docker attached vs detached mode

Docker attached vs detached mode

  • by
Docker attached vs detached mode

1. Overview

In this article, we will go through the differences between attached vs detached mode in docker containers.

2. Docker attached and detached mode

When starting a Docker container, you must first decide if you want to run the container in the background in a “detached” mode or in the default “attached” mode.

For example, the following command runs the container in the foreground attached mode.

> docker run -p 8081:8081 9f356a6e96f4
output logs of the container rolls here

You can run the container in the background detached mode by using the -d=true or just -d option.

> docker run -d -p 8081:8081 9f356a6e96f4
>

Also, if you use the docker start command to run an already stopped container, it runs in the background detached mode by default.

> docker start 207ad40bbe73
207ad40bbe73
> 

3. Docker attached vs detached mode

3.1. Attached mode

The attached mode (interactive session) will attach your terminal’s standard input, output, and error (or any combination of the three) to a running container. This allows you to view the container’s ongoing output or to control it interactively, as though the commands were running directly in your terminal.

It simply means you will see the output of the container in your terminal.

When using Docker run command, it attaches your terminal to the container so we can listen to output printed by the container but we can’t input anything into the container or into the application running into the container.

You can fix this by using any of the following configuration options:

  1. --interactive or -i: Launches the container in interactive mode and keeps STDIN open even if not attached
  2. --tty or -t: Allocate a pseudo-TTY. This option tells the Docker to allocate a virtual terminal session within the container.

If you combine both options, we can input something so the container will listen to the input and we will also get a terminal exposed by the container which is actually the device where we enter the input.

3.2. Detached mode

The detach option on the docker command line shows that the docker client or terminal (docker) will make a request to the server (dockerd), and then the client will exit while that request continues on the server. Docker is a client/server application where the client is just a thin frontend on a REST API to send every command to the server.

With a docker run --d, this means it will create the container, the server will respond with a container id if successful, and the container will continue to run on the server while you are free to run other commands in your terminal.

For example, the following run command runs a container in the background and returns so that you can continue executing other docker commands such as docker ps.

> docker run -d -p 8081:8081 9f356a6e96f4
36ef4edbf9898d9cc1abe13d8dd0916eff3b44a9e92fdd6f078842a7157e2258
> docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS          PORTS                    NAMES
36ef4edbf989   9f356a6e96f4   "docker-entrypoint.s…"   5 seconds ago   Up 3 seconds    0.0.0.0:8081->8081/tcp   dazzling_cray

If you run without the detach option, the client will immediately run an attach API call after the container is created so you can see the output and optionally provide input to the running process on the container. This is useful if your container is running something interactive.

You can attach to an already detached container by using the docker attach command. This allows you to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name.

 docker attach 6d12d35591f4

If you want to enter input while using the docker start command, you must use -i option. There is no need to use the -tty option as docker remembers it if we ran the container originally with that flag.

The flag -a attaches the terminal to the container.

> docker start -a -i dazzling_dart

Alternatively, you can use docker logs to get logs of the container in the detached mode.

> docker logs 6d12d35591f4
Hi
> 

If you want to keep listening for the logs, you can use -follow option in docker logs. It basically means that you are attaching your terminal again with your container for the logs.

>  docker logs -f dazzling_cray
Hi

4. Conclusion

To sum up, we have learned docker attached vs detached mode.

Leave a Reply

Your email address will not be published. Required fields are marked *