docker容器的前台后台运行

容器运行时的 -dit 选项

2021-06-22

docker run 命令的选项

  --attach , -a 		Attach to STDIN, STDOUT or STDERR
  --detach , -d 		Run container in background and print container ID
  --interactive , -i 	Keep STDIN open even if not attached
  --tty , -t 		    Allocate a pseudo-TTY

docker exec 命令

  -d, --detach               Detached mode: run command in the background
  -i, --interactive          Keep STDIN open even if not attached
  -t, --tty                  Allocate a pseudo-TTY

  没有 -a --attach 选项
  • -a 可以多次指定 ,比如 -a stdin -a stdout
  • 不要同时使用 -t 和 -a stderr,因为 pty 本身的限制。pty

    Do not use the -t and -a stderr options together due to limitations in the pty implementation. All stderr in pty mode simply goes to stdout.

使用 bash:lastest 镜像,通过观察 /dev/fd/ 目录,查看容器控制台IO的打开关闭情况。

默认,dit组合是001,前台运行,无法输入,只能看到输出。
只有 -i 没有 -t,容器可以接受stdin的输入,也可以输出,但是通过管道,不是伪终端,是批处理模式,而不是交互模式。

默认:前台运行,stdout 和 stderr 转接出来,而stdin关了

默认 -d=false, -i=false, -t=false

默认 -d=false
-d=false时,默认-i=false,-t=false

dit:000

[work@cdh06 ~]$ docker run --rm  bash -c 'ls  -l /dev/fd/'
total 0
ls: /dev/fd/3: cannot read link: No such file or directory
lrwx------    1 root     root            64 Jun 22 09:11 0 -> /dev/null
l-wx------    1 root     root            64 Jun 22 09:11 1 -> pipe:[55404435]
l-wx------    1 root     root            64 Jun 22 09:11 2 -> pipe:[55404436]
lr-x------    1 root     root            64 Jun 22 09:11 3
[work@cdh06 ~]$ docker run --rm -d=false bash -c 'ls  -l /dev/fd/'
total 0
ls: /dev/fd/3: cannot read link: No such file or directory
lrwx------    1 root     root            64 Jun 22 09:11 0 -> /dev/null
l-wx------    1 root     root            64 Jun 22 09:11 1 -> pipe:[55409027]
l-wx------    1 root     root            64 Jun 22 09:11 2 -> pipe:[55409028]
lr-x------    1 root     root            64 Jun 22 09:11 3

此时,docker 使用了 -a STDERR -a STDOUT 选项。

前台 -t

dit: 001

$ docker run --rm -t bash -c 'ls  -l /dev/fd/'
total 0
lrwx------    1 root     root            64 Jun 22 10:14 0 -> /dev/pts/0
lrwx------    1 root     root            64 Jun 22 10:14 1 -> /dev/pts/0
lrwx------    1 root     root            64 Jun 22 10:14 2 -> /dev/pts/0

但是 stdin (/dev/fd/0)是得不到输入的。

前台 -i

dit:010


                
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值