How do I use multiple terminals in a docker container?

I know it's weird to use multiple terminals in a docker container.

My goal is to test some commands and finally build a dockerfile with these commands. So I need to use multiple terminals, let's say two. One runs some commands, the other is used to test those commands.

If I am using a real machine, I can use it to use multiple terminals, but in docker, how can I do this?

Possibly a run

docker solution with CMD /bin/bash

, and in this bash using screen

?

EDIT 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.

+3


source to share


3 answers


If I understand the problem correctly, you can use nsenter. Assuming you have a running docker named nginx (when starting nginx), run the following command from the host:

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

      



This will run the program in the given PID namespace ($ SHELL by default). You can start more than one shell by releasing it more than once (from the host). Then you can run any binary that exists in the given docker files or tail, rm, etc. For example, write the log file nginx.

More information can be found in the nsenter man .

+2


source


Dockers will run the server in one container and the client in another. You can use links to make the server visible from the client, and you can use volumes to make files on the server available to the client. If you really want to have two terminals in the same container, there is nothing stopping you from using ssh. I tested this docker server:

from: 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"]

      

You need to place this image on your image or vice versa to combine all the features. Once you have created and started your container, you can get its IP using

docker inspect <id or name of container>

      



from docker host you can now ssh with root and password from docker file. Now you can create as many ssh clients as you like. I tested with:

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

      

from one client and

tail -f tmpfile

      

from another

+3


source


If you just want to play around, you can run sshd

in your image and explore it the way you are used to:

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

      

When you're done with your research, you can start building your Dockerfile as usual.

+1


source







All Articles