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.
2 Solutions collect form web for “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 dettached.
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"
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
Here is how it works.
Running a docker container
busybox, a tiny linux image in detached mode and container name is
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.
Regarding running for second time, as you rightly pointed there are two options and out of it :
- 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.
- Wipe out the existing container and re-run
docker run --name=mycontainer image.
To wipe you existing container, use command –
docker rm -f mycontainer