docker run -d 和docker run -it 的区别

1 篇文章 0 订阅
1 篇文章 0 订阅

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 # 这种情况就不会退出了
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值