Episode 8. Understanding Attached and Detached containers, Docker Logs

Amir Mustafa
4 min readFeb 23, 2022

--

In this article, we will understand the attached and detached modes in docker.

Attached Mode: When the terminal is running forever after entering some command. This indicates the server is running.

Detached Mode: When the server is running in the background. We can write more commands in the same terminal.

A. On Creating the container (Default):

→ When we create a container by default, the terminal will be in attached mode.

docker run -p 8000:80 <docker-container-id>

Default: Create containers starts in attached mode when no mode is specified.

B. On Restarting the container (Default):

→ When we restart the container by default, the terminal will be in detached mode.

→ Detached mode means the container is running background, no logs will print

docker start <docker-container-id>

Default: Restart containers starts in detached mode when no mode is specified.

→ It is possible to open in any mode as per our requirement. We use the below flags in terminals

-a   // for attached mode-d   // for detached mode

C. On Creating the container (Opp of default):

→ When we create a container by default, the terminal will be in attached mode.

docker run -p 8000:80 -d <docker-container-id>    // added -d flag

Opp of default case: Create containers starting in detached mode.

D. On Restarting the container (Opp of default):

→ When we restart the container by default, the terminal will be in detached mode.

→ Detached mode means the container is running background, no logs will print

docker start -a <docker-container-id>    // added -a flag

Opp of default case: Restart containers starting in attached mode.

→ The detached mode can be converted into the attached mode:

Suppose if we realize, we are not able to see logs of our application.

NOTE:

More commands can be written in detached mode (as the terminal is unblocked)

docker attach <docker-container-id> // convert detach to attach mode

View logs of Docker Containers:

→ It is possible to see the logs history of the container in docker.

→ This can be achieved in two ways:

Method 1. Docker command (using terminal):

docker logs <docker-container-id-or-name>   // All logs till now
docker logs -f <docker-container-id-or-name>// Current + Future logs

Method 2. Microsoft Docker Extension (Visual):

→ Click Docker Icon from left in Visual Studio Code

→ All the containers are listed here. Right-click on the container

→ Click View logs

→ Logs will be printed in the terminal

Video:

Closing Thoughts:

Choosing the right docker container’s mode is important. If we are developing and want to see logs, an attached mode is helpful. If we want to execute many commands and opening many terminals might slow our system, we can use detached mode.

Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.

--

--

Amir Mustafa
Amir Mustafa

Written by Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/

No responses yet