希望在docker集装箱中使用多个terminal?

我知道在Docker容器中使用多个terminal是很奇怪的。

我的目的是testing一些命令,并最终用这些命令build立一个dockerfile。 所以我需要使用多个terminal,比如两个。 一个是运行一些命令,另一个是用来testing命令。

如果我使用真正的机器,我可以ssh它使用多个terminal,但在docker,我该怎么做?

也许解决scheme是rundocker与CMD /bin/bash ,并在该bash中,使用screen

编辑 In my situation, one shell run a server program, the other run a client program to test the server program. Because the server program and client program are compiled together. So, the default link method in docker is not suitable. In my situation, one shell run a server program, the other run a client program to test the server program. Because the server program and client program are compiled together. So, the default link method in docker is not suitable.

如果我理解正确的问题,那么你可以使用nsenter。 假设你有一个名为nginx的正在运行的docker(在启动了nginx的情况下),从主机运行以下命令:

 nsenter -m -u -i -n -p -t `docker inspect --format {{.State.Pid}} nginx` 

这将在PID的给定名称空间中启动一个程序(默认$ SHELL)。 您可以通过多次(从主机)发出一个shell来运行多个shell。 然后你可以运行给定的docker或tail,rm等文件中存在的任何二进制文件。 例如,尾部nginx的日志文件。

进一步的信息可以在这个男人身上find。

docker的方式是在一个容器中运行服务器,在另一个容器中运行客户端。 您可以使用链接使服务器在客户端可见,您可以使用卷使服务器上的文件可从客户端获得。 如果你真的想有两个terminal到同一个容器,没有什么能阻止你使用ssh。 我testing了这个docker服务器:

来自: https : //docs.docker.com/examples/running_ssh_service/

 # sshd # # VERSION 0.0.1 FROM ubuntu:14.04 MAINTAINER Thatcher R. Peskens "thatcher@dotcloud.com" RUN apt-get update && apt-get install -y openssh-server RUN mkdir /var/run/sshd RUN echo 'root:screencast' | chpasswd RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"] 

你需要将这个图像放在你的图像上或者其他方面来获得所有的function。 在你build立并启动你的容器后,你可以使用它的IP

 docker inspect <id or name of container> 

从docker主机,你现在可以用root和docker文件的密码ssh。 现在你可以根据需要产生尽可能多的ssh客户端。 我testing了:

 while true; do echo "test" >> tmpfile; sleep 1; done 

从一个客户和

 tail -f tmpfile 

从另一个

如果你想玩耍,你可以在你的图像中运行sshd并按照习惯的方式探索它:

  docker run -d -p 22 your_image /usr/sbin/sshd -D 

当你完成了你的探索之后,你可以像往常一样继续创buildDockerfile。