docker?run?-d和docker?run?-it的區(qū)別詳解
docker run -it
- i : interactive 代表交互
- -t : tty 分配偽 TTY
測(cè)試不帶前臺(tái)進(jìn)程的,例如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)容器不會(huì)退出
測(cè)試帶前臺(tái)進(jì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 #這里沒(méi)有反應(yīng),我們繼續(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
總結(jié):
-it 使用交互方式運(yùn)行,進(jìn)入容器查看內(nèi)容
1.當(dāng)運(yùn)行的鏡像沒(méi)有前臺(tái)進(jìn)程。
exit #run進(jìn)去容器,exit退出,容器停止
Ctrl+P+Q # run進(jìn)去容器,ctrl+p+q退出,容器不停止
1.當(dāng)運(yùn)行的鏡像有前臺(tái)進(jìn)程。
exit #用exit無(wú)效,使用Ctrl + C ,容器會(huì)停止
Ctrl+P+Q # run進(jìn)去容器,ctrl+p+q退出,容器不停止
docker run -d
-d : detach 表示后臺(tái)運(yùn)行
測(cè)試不帶前臺(tái)進(jìn)程的,例如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)容器啟動(dòng)后會(huì)立馬退出
測(cè)試帶前臺(tái)進(jì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)容器不會(huì)退出
總結(jié):
Docker容器后臺(tái)運(yùn)行,就必須有一個(gè)前臺(tái)進(jìn)程.
容器運(yùn)行的命令如果不是那些一直掛起的命令(比如運(yùn)行top,tail),就是會(huì)自動(dòng)退出的。
docker run -d ubuntu tail -f /dev/null # 這種情況就不會(huì)退出了
總結(jié)
到此這篇關(guān)于docker run -d和docker run -it的區(qū)別詳解的文章就介紹到這了,更多相關(guān)docker run -d和docker run -it區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
docker創(chuàng)建鏡像并上傳云端服務(wù)器的實(shí)現(xiàn)示例
鏡像是一種輕量級(jí)、可執(zhí)行的獨(dú)立軟件包,用來(lái)打包軟件運(yùn)行環(huán)境和基于運(yùn)行環(huán)境開發(fā)的軟件,本文介紹了如何使用Docker創(chuàng)建鏡像,并將其上傳到云端,感興趣的可以了解一下2023-08-08docker對(duì)網(wǎng)絡(luò)和程序速度的影響解讀
本文通過(guò)對(duì)比分析,測(cè)試了在宿主機(jī)和Docker容器中部署Spring Boot程序的性能差異,在網(wǎng)絡(luò)延遲方面,Docker容器比宿主機(jī)慢0.1~0.2毫秒,在程序運(yùn)行速度方面,宿主機(jī)和Docker容器的平均運(yùn)行時(shí)間相近,單次運(yùn)行時(shí)間存在較大差異,無(wú)法確定Docker容器在速度上優(yōu)于宿主機(jī)2025-01-01手把手帶大家通過(guò)Docker部署前后端分離項(xiàng)目(親測(cè)可用)
近年來(lái)前后端分離已經(jīng)成為中大型軟件項(xiàng)目開發(fā)的最佳實(shí)踐,下面這篇文章主要給大家介紹了關(guān)于通過(guò)Docker部署前后端分離項(xiàng)目的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06Docker刪除某個(gè)鏡像的實(shí)現(xiàn)方法
在使用 Docker 時(shí),經(jīng)常需要?jiǎng)h除不再需要的鏡像、容器和卷,以釋放存儲(chǔ)空間,本文主要介紹了Docker刪除某個(gè)鏡像的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Docker搭建私有倉(cāng)庫(kù)的實(shí)現(xiàn)步驟
因?yàn)閐ockerHub公共倉(cāng)庫(kù)是外網(wǎng)的,所以訪問(wèn)就特別慢,所以一般公司都會(huì)搭建私人的鏡像倉(cāng)庫(kù)來(lái)保存鏡像,本文主要介紹了Docker搭建私有倉(cāng)庫(kù)的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03