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

Here is how it works.

Running a docker container busybox, a tiny linux image in detached mode and container name is testso

bash $ docker run -itd --name testso busybox
b60d0847bb81065d5f5d4b3a3acff3102d03e7a8a084c0770da4487427787479

You can see container running

bash $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b60d0847bb81        busybox             "sh"                7 seconds ago       Up 2 seconds                            testso

Now stopping the above container testso and check no container is running.

bash $ docker stop testso
testso
bash $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Now,your question addressed by starting earlier stopped container testso and see the container running in the background.

bash $ docker start testso
testso
bash $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b60d0847bb81        busybox             "sh"                46 seconds ago      Up 2 seconds                            testso

So, when the container is docker run with -d option first, the container can just use docker start containerid which automatically run in detached mode.

Hope this is helpful.

UPDATE: Regarding running for second time, as you rightly pointed there are two options and out of it :

  1. Instead of running it using the command docker run --name=mycontainer image, you may just start the existing container which you just trying and the above answer helps.
  2. Wipe out the existing container and re-run docker run --name=mycontainer image.
    To wipe you existing container, use command - docker rm -f mycontainer

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 (again, all this does is create the filesystem layer):

[sysadmin@vmr-132-9 ~]$ docker create --name=test centos:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"

See it?

sysadmin@vmr-132-9 ~]$ 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 doing anything 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)

[sysadmin@vmr-132-9 ~]$ docker start test test
[sysadmin@vmr-132-9 ~]$ docker logs test
hello world
hello world
hello world
hello world

Tags:

Docker