docker?run?-d和docker?run?-it的區(qū)別詳解
docker run -it
- i : interactive 代表交互
- -t : tty 分配偽 TTY
測試不帶前臺進程的,例如centos/ubuntu
> docker run -it ubuntu root@a30a87e0e065:/# exit exit > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
我們發(fā)現(xiàn)容器已經(jīng)退出了
> docker run -it ubuntu root@a30a87e0e065:/# 輸入Ctrl + P + Q > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e51e423ac575 ubuntu "bash" 10 seconds ago Up 10 seconds romantic_franklin
發(fā)現(xiàn)容器不會退出
測試帶前臺進程的,例如redis
> docker run -it redis
1:C 26 Nov 2022 15:15:37.357 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 26 Nov 2022 15:15:37.357 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
a config file use redis-server /path/to/redis.conf
1:M 26 Nov 2022 15:15:37.358 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 26 Nov 2022 15:15:37.358 # Server initialized
1:M 26 Nov 2022 15:15:37.358 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 26 Nov 2022 15:15:37.358 * Ready to accept connections
# 輸入Ctrl + P + Q
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14cef5be594b redis "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 6379/tcp silly_wright
> docker run -it redis
1:C 26 Nov 2022 15:22:49.890 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 26 Nov 2022 15:22:49.890 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 26 Nov 2022 15:22:49.891 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 26 Nov 2022 15:22:49.891 # Server initialized
1:M 26 Nov 2022 15:22:49.891 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 26 Nov 2022 15:22:49.891 * Ready to accept connections
exit #這里沒有反應,我們繼續(xù)Ctrl + C
^C1:signal-handler (1669476186) Received SIGINT scheduling shutdown...
1:M 26 Nov 2022 15:23:06.123 # User requested shutdown...
1:M 26 Nov 2022 15:23:06.123 * Saving the final RDB snapshot before exiting.
1:M 26 Nov 2022 15:23:06.130 * DB saved on disk
1:M 26 Nov 2022 15:23:06.130 # Redis is now ready to exit, bye bye...
> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8668b2bf13b1 redis "docker-entrypoint.s…" 24 seconds ago Exited (0) 7 seconds ago ecstatic_lehmann總結:
-it 使用交互方式運行,進入容器查看內容
1.當運行的鏡像沒有前臺進程。
exit #run進去容器,exit退出,容器停止
Ctrl+P+Q # run進去容器,ctrl+p+q退出,容器不停止
1.當運行的鏡像有前臺進程。
exit #用exit無效,使用Ctrl + C ,容器會停止
Ctrl+P+Q # run進去容器,ctrl+p+q退出,容器不停止
docker run -d
-d : detach 表示后臺運行
測試不帶前臺進程的,例如centos/ubuntu
> docker run -d ubuntu > docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0210648ee10f ubuntu "bash" 34 seconds ago Exited (0) 32 seconds ago jolly_wright
我們發(fā)現(xiàn)容器啟動后會立馬退出
測試帶前臺進程的,例如redis
> docker run -d redis > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3adf2698b224 redis "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 6379/tcp vigilant_agnesi
我們發(fā)現(xiàn)容器不會退出
總結:
Docker容器后臺運行,就必須有一個前臺進程.
容器運行的命令如果不是那些一直掛起的命令(比如運行top,tail),就是會自動退出的。
docker run -d ubuntu tail -f /dev/null # 這種情況就不會退出了
總結
到此這篇關于docker run -d和docker run -it的區(qū)別詳解的文章就介紹到這了,更多相關docker run -d和docker run -it區(qū)別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
docker創(chuàng)建鏡像并上傳云端服務器的實現(xiàn)示例
鏡像是一種輕量級、可執(zhí)行的獨立軟件包,用來打包軟件運行環(huán)境和基于運行環(huán)境開發(fā)的軟件,本文介紹了如何使用Docker創(chuàng)建鏡像,并將其上傳到云端,感興趣的可以了解一下2023-08-08

