admin管理员组文章数量:1122832
I am trying to conceptually understand how docker run
behaves if no flags -it
or -d
are provided.
- If the
-it
flags are provided, docker runs in "interactive mode". The-i
flag causesstdin
to be attached to the container. I think that-t
both allocates atty
environment and causesstdout
andstderr
to be attached to the container. - If the
-d
flag is provided, docker runs in "detached mode". This doesn't attachstdin
orstdout
.
However, the behavior without either flag is similar in some causes. No stdin
or stdout
.
So what is a good way of conceptually understanding how a container runs if neither -d
nor -it
flags are provided?
Some examples:
The default CMD
for the image ubuntu:latest
is /bin/bash
.
$ docker pull ubuntu:latest
$ docker history ubuntu:latest
CMD ["/bin/bash"]
The container will exit immediately when we run it. This is because the default command is /bin/bash
.
$ docker run ubuntu:latest
$ docker ps -a
4f1d3f7ebccf ubuntu:latest "/bin/bash" 1 second ago Exited (0) Less than a second ago boring_kowalevski
If we run in interactive mode, the container will remain open until CTRL+D
is pressed. If we press CTRL+D
, it exits.
$ docker run -it ubuntu:latest
root@b9cdc0aba0e1:/#
exit # (press CTRL+D)
b9cdc0aba0e1 ubuntu:latest "/bin/bash" 38 seconds ago Exited (127) 20 seconds ago relaxed_hugle
If we run in detached mode, the container also exits. It prints a hash before exiting.
$ docker run -d ubuntu:latest
97e6bef6750399782127e4ce9fe1400bebf20e209ba661ee637e69c758ea31fb
$ docker ps -a
97e6bef67503 ubuntu:latest "/bin/bash" 27 seconds ago Exited (0) 27 seconds ago zealous_bhaskara
If we run with -t
only it behaves in a strange way. We can't exit using CTRL+D
. We don't appear to be able to run anything, such as echo hello
.
$ docker run -t ubuntu:latest
root@7c79b9c28eca:/# echo hello
^C^C
root@7c79b9c28eca:/#
The only way to stop it appears to be to issue a docker stop
command.
$ docker ps -a
7c79b9c28eca ubuntu:latest "/bin/bash" About a minute ago Up About a minute kind_franklin
$ docker stop kind_franklin
After about 10 seconds, the container will stop.
root@7c79b9c28eca:/# user@host:~$
$ docker ps -a
7c79b9c28eca ubuntu:latest "/bin/bash" 3 minutes ago Exited (137) 33 seconds ago kind_franklin
If we run with -i
then the output looks a bit weird. But the container does remain running. Here's a side-by-side comparison of -i
and -it
.
$ docker run -i ubuntu:latest
echo hello
hello # (CTRL+D)
$ docker run -it ubuntu:latest
root@7e3c2d21989a:/# echo hello
hello
root@7e3c2d21989a:/# # (CTRL+D)
exit
$
Running with all three flags is interesting. In this case, the container continues to run in the same way as it does with -it
, however the session is not attached yet. We can then attach to the container with docker attach
and then it continues to function as if we had run with -it
and without -d
directly.
$ docker run -dit ubuntu:latest
f84c37823d83a26258e8ab372176e177b685e911f6668e648f7a3990850cd8e0
$ docker ps
f84c37823d83 ubuntu:latest "/bin/bash" 4 seconds ago Up 3 seconds charming_varahamihira
$ docker attach charming_varahamihira
root@f84c37823d83:/# echo hello
hello
root@f84c37823d83:/#
exit
Now let's try something different. Change the default behavior so that the container runs forever. Now, rather than exiting when no flags are supplied, it continues to run but does not receive input or send output. There is no way to stop it other than to run docker stop
.
docker run ubuntu:latest /bin/bash -c 'sleep infinity'
echo hello # input
# (CTRL+D)
If we run with the -d
flag, the container behaves in a sensible way. It continues to run indefinitely.
$ docker run -d ubuntu:latest /bin/bash -c 'sleep infinity'
6523960d7740afb3282bb7edaeb9c90f0a2ef12efa1cdd7481c5d2c428c72546
$ docker ps
6523960d7740 ubuntu:latest "/bin/bash -c 'sleep…" 8 seconds ago Up 7 seconds agitated_payne
We can create a new bash session with docker exec -it agitated_payne bash
. In this case CTRL+D
will exit this bash
session, but not the original bash
session, so the container will continue to run.
If we attach to the container with docker attach
, the container again behaves strangely with no input or output. We cannot stop it with CTRL+D
. The only way to get our prompt back is to stop the container with docker stop
.
We can also try all three flags.
$ docker run -dit ubuntu:latest /bin/bash -c 'sleep infinity'
But this appears to behave the same way as just having the -d
flag. docker attach
does not attach i/o and the container cannot be stopped with CTRL+D
.
With just the flags -it
, the container behaves similarly.
I am trying to conceptually understand how docker run
behaves if no flags -it
or -d
are provided.
- If the
-it
flags are provided, docker runs in "interactive mode". The-i
flag causesstdin
to be attached to the container. I think that-t
both allocates atty
environment and causesstdout
andstderr
to be attached to the container. - If the
-d
flag is provided, docker runs in "detached mode". This doesn't attachstdin
orstdout
.
However, the behavior without either flag is similar in some causes. No stdin
or stdout
.
So what is a good way of conceptually understanding how a container runs if neither -d
nor -it
flags are provided?
Some examples:
The default CMD
for the image ubuntu:latest
is /bin/bash
.
$ docker pull ubuntu:latest
$ docker history ubuntu:latest
CMD ["/bin/bash"]
The container will exit immediately when we run it. This is because the default command is /bin/bash
.
$ docker run ubuntu:latest
$ docker ps -a
4f1d3f7ebccf ubuntu:latest "/bin/bash" 1 second ago Exited (0) Less than a second ago boring_kowalevski
If we run in interactive mode, the container will remain open until CTRL+D
is pressed. If we press CTRL+D
, it exits.
$ docker run -it ubuntu:latest
root@b9cdc0aba0e1:/#
exit # (press CTRL+D)
b9cdc0aba0e1 ubuntu:latest "/bin/bash" 38 seconds ago Exited (127) 20 seconds ago relaxed_hugle
If we run in detached mode, the container also exits. It prints a hash before exiting.
$ docker run -d ubuntu:latest
97e6bef6750399782127e4ce9fe1400bebf20e209ba661ee637e69c758ea31fb
$ docker ps -a
97e6bef67503 ubuntu:latest "/bin/bash" 27 seconds ago Exited (0) 27 seconds ago zealous_bhaskara
If we run with -t
only it behaves in a strange way. We can't exit using CTRL+D
. We don't appear to be able to run anything, such as echo hello
.
$ docker run -t ubuntu:latest
root@7c79b9c28eca:/# echo hello
^C^C
root@7c79b9c28eca:/#
The only way to stop it appears to be to issue a docker stop
command.
$ docker ps -a
7c79b9c28eca ubuntu:latest "/bin/bash" About a minute ago Up About a minute kind_franklin
$ docker stop kind_franklin
After about 10 seconds, the container will stop.
root@7c79b9c28eca:/# user@host:~$
$ docker ps -a
7c79b9c28eca ubuntu:latest "/bin/bash" 3 minutes ago Exited (137) 33 seconds ago kind_franklin
If we run with -i
then the output looks a bit weird. But the container does remain running. Here's a side-by-side comparison of -i
and -it
.
$ docker run -i ubuntu:latest
echo hello
hello # (CTRL+D)
$ docker run -it ubuntu:latest
root@7e3c2d21989a:/# echo hello
hello
root@7e3c2d21989a:/# # (CTRL+D)
exit
$
Running with all three flags is interesting. In this case, the container continues to run in the same way as it does with -it
, however the session is not attached yet. We can then attach to the container with docker attach
and then it continues to function as if we had run with -it
and without -d
directly.
$ docker run -dit ubuntu:latest
f84c37823d83a26258e8ab372176e177b685e911f6668e648f7a3990850cd8e0
$ docker ps
f84c37823d83 ubuntu:latest "/bin/bash" 4 seconds ago Up 3 seconds charming_varahamihira
$ docker attach charming_varahamihira
root@f84c37823d83:/# echo hello
hello
root@f84c37823d83:/#
exit
Now let's try something different. Change the default behavior so that the container runs forever. Now, rather than exiting when no flags are supplied, it continues to run but does not receive input or send output. There is no way to stop it other than to run docker stop
.
docker run ubuntu:latest /bin/bash -c 'sleep infinity'
echo hello # input
# (CTRL+D)
If we run with the -d
flag, the container behaves in a sensible way. It continues to run indefinitely.
$ docker run -d ubuntu:latest /bin/bash -c 'sleep infinity'
6523960d7740afb3282bb7edaeb9c90f0a2ef12efa1cdd7481c5d2c428c72546
$ docker ps
6523960d7740 ubuntu:latest "/bin/bash -c 'sleep…" 8 seconds ago Up 7 seconds agitated_payne
We can create a new bash session with docker exec -it agitated_payne bash
. In this case CTRL+D
will exit this bash
session, but not the original bash
session, so the container will continue to run.
If we attach to the container with docker attach
, the container again behaves strangely with no input or output. We cannot stop it with CTRL+D
. The only way to get our prompt back is to stop the container with docker stop
.
We can also try all three flags.
$ docker run -dit ubuntu:latest /bin/bash -c 'sleep infinity'
But this appears to behave the same way as just having the -d
flag. docker attach
does not attach i/o and the container cannot be stopped with CTRL+D
.
With just the flags -it
, the container behaves similarly.
1 Answer
Reset to default 0If the
-d
flag is provided, docker runs in "detached mode". This doesn't attachstdin
orstdout
.
Nope. Input and output to the process are separate. The thing that is detached is the docker CLI client from the running container on the docker server.
So what is a good way of conceptually understanding how a container runs if neither
-d
nor-it
flags are provided?
It is conceptually the same as if you ran:
$some_command </dev/null
Output is sent to the attached docker client, but no input is attached to the process.
If you were to detach, the logs would still be accessible by docker logs
, making it conceptually the same as:
$some_command </dev/null &>output.log &
本文标签: How does docker run conceptually if no d or it flags passedStack Overflow
版权声明:本文标题:How does `docker run` conceptually if no `-d` or `-it` flags passed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304867a1932386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
-d
,-i
, and-t
do, or why runningbash
as the main container process has specific behavior? In Unix more generally, are "background process", "stdin", and "tty" meaningful terms to you? Also see for example Docker container will automatically stop after "docker run -d" and Confused about Docker -t option to Allocate a pseudo-TTY. – David Maze Commented Nov 22, 2024 at 11:26