Docker Detached Mode

DockerKubernetes

Docker Problem Overview


What is detached mode in the docker world? I read this article Link, but it does not explain exactly what detached mode mean.

Docker Solutions


Solution 1 - Docker

You can start a docker container in detached mode with a -d option. So the container starts up and run in background. That means, you start up the container and could use the console after startup for other commands.

The opposite of detached mode is foreground mode. That is the default mode, when -d option is not used. In this mode, the console you are using to execute docker run will be attached to standard input, output and error. That means your console is attached to the container's process.

In detached mode, you can follow the standard output of your docker container with docker logs -f <container_ID>.

Just try both options. I always use the detached mode to run my containers. I hope I could explain it a little bit clearer.

Solution 2 - Docker

The detach option on the docker command line indicates that the docker client (docker) will make a request to the server (dockerd), and then the client will exit while that request continues on the server. Part of the confusion may be that docker looks like a single process, where in reality it 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 container run --detach, this means the container will be created, 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. This is often used for a server (e.g. nginx) you want to start in the background while you continue to run other commands. Note that you can still configure a container with the --interactive and -tty options (often abbreviated -it) and later run a docker container attach to connect to an already running container. (Note, until you attach to the container running with -itd, any attempt by the container to read from stdin would hang, instead of seeing an end of input that often triggers an immediate exit if you just passed -d.)

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 (e.g. /bin/bash).

Several other commands allow the detach option, including docker-compose up -d which will start an entire project and leave it running on the server in the background. There's also many of the docker service commands which will either detach after submitting the change to the server to create or update a service's target state, or if you do not detach, the client will wait until the service's current state matches the target state and you can see the progress of the deployment. Note with docker service commands, you may have to pass --detach=false to remain attached, the behavior has changed over the past year depending on your version.

Solution 3 - Docker

>What is detached mode in the docker world?

Detached means that the container will run in the background, without being attached to any input or output stream.

  • docker provide --detach (or -d in short) option and started the program in the background.
  • This means that the program started but isn’t attached to your terminal.
  • Example docker run --detach --name web nginx:latest # Note the detach flag.

>Why do we need --detach mode?

  • Server software is generally run in detached containers because it is rare that the software depends on an attached terminal.
  • Running detached containers is a perfect fit for programs that sit quietly in the background.

Note

  • Generally, This type of program is called a daemon, or a service. A daemon generally interacts with other programs (or humans over a network) or some other communication channel. When you launch a daemon or other program in a container that you want to run in the background, remember to use either the --detach flag or its short form, -d.

Solution 4 - Docker

docker run -d -t ubuntu:14.04

docker run - Create an instance from docker image as docker container. (if image not available locally it pulls from docker hub) ubuntu - Image name

14.04 - Tag

-d, --detach - Detach mode

-t, --tty - Allocate a pseudo-TTY

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsoftshipperView Question on Stackoverflow
Solution 1 - DockerSimView Answer on Stackoverflow
Solution 2 - DockerBMitchView Answer on Stackoverflow
Solution 3 - DockerGuptaView Answer on Stackoverflow
Solution 4 - DockerSiddharth KumarView Answer on Stackoverflow