What is detached mode in the docker world? I read this article Link, but it does not explain exactly what detached mode mean.
问题:
回答1:
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.
回答2:
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.
回答3:
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