docker run -d和docker run -it的区别详解
作者:RedCong 时间:2021-07-29 14:05:39
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
我们发现容器已经退出了
> 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
发现容器不会退出
测试带前台进程的,例如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 #这里没有反应,我们继续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
我们发现容器启动后会立马退出
测试带前台进程的,例如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
我们发现容器不会退出
总结:
Docker容器后台运行,就必须有一个前台进程.
容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。
docker run -d ubuntu tail -f /dev/null # 这种情况就不会退出了
来源:https://blog.csdn.net/qq_32789063/article/details/128058919
标签:docker,run,-d
0
投稿
猜你喜欢
徐静蕾联手VANCL推出开啦饰品进军B2C告捷
2009-11-12 14:14:00
个人实录:博客从Z-blog转换到WordPress
2009-01-22 13:33:00
Stiqr——WordPress主题编辑工具
2010-05-04 14:15:00
Alexa排名和各项数据说明
2008-02-03 13:43:00
我的西联汇款取款经历
2009-04-03 12:54:00
Cyrus IMAP邮件服务器安装与配置(3)
2007-08-14 15:44:00
防范黑客来自网上的攻击的几种方法
2008-11-11 12:14:00
WordPress 3.0五大新特征
2010-05-11 16:38:00
接着Zac谈 如何在搜索引擎中建立良好品牌
2009-03-04 12:45:00
网站优化如何操作 做好九个方面基础工作
2009-03-10 10:47:00
Centos7远程桌面 vnc/vnc-server的设置详解
2023-02-16 09:29:01
网上安家步步高;ADSL建站初探
2010-04-14 18:43:00
VMware虚拟机安装苹果Mac OS的超详细教程
2023-05-07 11:22:53
电影服务器相关网络协议简介
2009-01-13 16:43:00
2011年2月Godaddy最新优惠码(含30%折扣优惠码)
2011-01-29 16:54:00
Firefox 3.6 Beta 2发布 修复190多个Bug
2009-11-11 17:00:00
Yahoo!网站性能最佳体验的34条黄金守则——服务器
2008-05-29 13:22:00
如何让百度天天更新你 收录的更多
2008-12-24 10:55:00
详解VirtualBox虚拟机网络环境解析和搭建-NAT、桥接、Host-Only、Internal、端口映射
2021-08-25 01:15:16
动易SiteWeaver利用自定义标签实现随机文章列表
2009-03-12 18:22:00