docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker run -d和docker run -it区别

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

作者:RedCong

记得第一次接触docker的时候,教程中写着docker run -it之类的命令,当时对这个-it选项是一知半解,下面这篇文章主要给大家介绍了关于docker run -d和docker run -it的区别的相关资料,需要的朋友可以参考下

docker run -it

测试不带前台进程的,例如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 # 这种情况就不会退出了

总结

到此这篇关于docker run -d和docker run -it的区别详解的文章就介绍到这了,更多相关docker run -d和docker run -it区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文