Is there any way to start a Docker container in detached mode?

Unless you specifically attach (-a or -i options) when you start the container, by definition you are detached.

Creating a container simply builds the filesystem layer. Starting it runs the ENTRYPOINT (or CMD) process. Run does both the create and the start, as you surmised. So you cannot “attach” to a created container… there is no process to attach to.

Here I create a container (all this does is create the filesystem layer):

$ docker create \
    --name=test \
    centos:latest \
    /bin/sh -c "while true; do echo hello world; sleep 1; done"

Note that the STATUS is Created:

$ docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                     PORTS               NAMES
9d5bf75a8077        centos:latest            "/bin/sh -c 'while tr"   15 seconds ago      Created                                        test

It isn’t running yet. Now start it without attaching, nothing is printed to the terminal STDOUT, because I am not attached. But STDOUT is going to the log-driver (json-file) which you can view with docker logs:

$ docker start test test
$ docker logs test
hello world
hello world
hello world
hello world

Leave a Comment