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.
To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the --rm option.
Detached mode, shown by the option --detach or -d , means that a Docker container runs in the background of your terminal. It does not receive input or display output. If you run containers in the background, you can find out their details using docker ps and then reattach your terminal to its input and output.
To run a docker container in the background or the detached mode from the terminal, you can use the docker run command followed by the -d flag (or detached flag) and followed by the name of the docker image you need to use in the terminal.
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
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 :
docker run --name=mycontainer image
, you may just start the existing container which you just trying and the above answer helps.docker run --name=mycontainer image
.docker rm -f mycontainer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With