docker从终端运行过程

问题描述 投票:0回答:1

运行容器:

[root@localhost ~]# docker run -it centos:7.7.1908 /bin/bash
[root@79d4ba3a2080 /]# 

主机执行:

[root@localhost ~]# ps aux|grep /bin/bash
root      10124  0.0  2.7 385800 51548 pts/1    Sl+  14:25   0:00 docker run -it centos:7.7.1908 /bin/bash
root      10155  0.0  0.0  11828  1740 pts/0    Ss+  14:25   0:00 /bin/bash
root      10197  0.0  0.0 112728   976 pts/2    S+   14:27   0:00 grep --color=auto /bin/bash

查看容器运行过程fd:容器运行过程(stdin,stdout,stderr)指向pts / 0。

[root@localhost ~]# ll /proc/10155/fd
total 0
lrwx------ 1 root root 64 May   8 14:25 0 -> /dev/pts/0
lrwx------ 1 root root 64 May   8 14:25 1 -> /dev/pts/0
lrwx------ 1 root root 64 May   8 14:25 2 -> /dev/pts/0
lrwx------ 1 root root 64 May   8 14:29 255 -> /dev/pts/0

测试1:

[root@localhost ~]# echo "hello" > /proc/10155/fd/0

容器显示结果:

[root@79d4ba3a2080 /]# hello

测试2:

[root@localhost ~]# echo "hello" > /dev/pts/0

主机显示结果:

[root@localhost ~]# tty
/dev/pts/0
[root@localhost ~]# hello

结果显示,在主机pts / 0中,容器没有响应。“测试2”有什么问题?

为什么它没有显示在容器中?

码头工人做了什么?

如果有任何相关信息,请让我知道,谢谢。

linux shell docker terminal tty
1个回答
0
投票

您没有将pts设备映射到容器,请在run命令中添加以下参数:--volume /dev/pts:/dev/pts

参考:https://docs.docker.com/storage/volumes/

$ docker run  -it --rm  --volume /dev/pts:/dev/pts centos bash
[root@b5010471931d /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.5  0.0  12024  3168 pts/7    Ss   10:57   0:00 bash
root         16  0.0  0.0  43960  3324 pts/7    R+   10:57   0:00 ps aux

在主机上:

# echo hello_from_host > /dev/pts/7

在容器中:

[root@b5010471931d /]# hello_from_host
© www.soinside.com 2019 - 2024. All rights reserved.