Laird Nelson
Laird Nelson

Reputation: 16154

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

I am a Docker rookie so my use of concepts and terminology below may be flawed.

I think I understand the notion of creating a container (via docker create), and of starting a container so created (via docker start). I also understand less clearly but still somewhat that docker run is used to simultaneously create and run a container. Do correct me if I'm wrong.

Unless I've missed something, if I want to start a container so that it is detached, my only option is docker run -d.

What I don't understand is: suppose I have already created my container, and it's stopped, and now I want to start it detached. How do I do that? Or is that the wrong way to think about it?

I ran into this conceptual misunderstanding (I'm sure that's what it is) by trying to run container twice using below command:
docker -d --name=fred my/image

The second time I got:

docker: Error response from daemon: Conflict. The name "/image" is already in use by container [...]

Fine; I understand now that this tries to create two containers with the same name which quite clearly cannot happen. But that led me to this conceptual question: if I have a container that is, say, stopped, how can I start it up in detached mode?

I told you I was a rookie. Thanks for any information.

Upvotes: 32

Views: 49995

Answers (3)

Michal S
Michal S

Reputation: 1554

This is bit of topic answer, but may be helpful considering question keywords: start, container, detached.

If you came to this page to find a way to docker start YourImageName that was run without -d (detached) flag, all you need is

docker start YourImageName --detach-keys d

Upvotes: 1

James Moser
James Moser

Reputation: 506

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

Upvotes: 24

Rao
Rao

Reputation: 21359

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

Upvotes: 11

Related Questions