How to open multiple terminals in docker?
DockerDocker Problem Overview
I need to launch two distinct processes on a docker container which requires two terminals.What is the best way to achieve this?
Docker Solutions
Solution 1 - Docker
You can run docker exec -it <container> bash
from multiple terminals to launch several sessions connected to the same container.
Solution 2 - Docker
To expand on @eltonStoneman's great answer (For all those new docker folks like me):
-
Open a docker terminal
-
Get the image running as a container in the background:
docker run -d -it <image_id>
- Tip:
docker ps
will show the container_id that you just fired up from said image.
- Per @eltonStoneman's advice:
docker exec -it <container_id> bash
- Now your docker terminal is showing an interactive terminal to the container.
- Open up another docker terminal and perform step 3 to create another interactive terminal to the container. (Rinse and Repeat)
Solution 3 - Docker
docker run -it container_name bash
starts a new container with bash promt.
docker exec -it container_name bash
joins already running container's bash prompt.
Solution 4 - Docker
First get the name of the container
docker container ls
Then get run docker exec command to get in that container
docker exec <container_id> bash
Solution 5 - Docker
Using Docker Compose: Let's say you have a Compose yml that enables X-Windows.
You can follow the steps below to launch terminals for a graphic IDE (e.g. qtCreator), nautilus and a terminal window.
Assumptions:
- Host is Windows 10. 1803
- Image is Ubuntu Xenial
- Docker engine is 18.03.1-ce
- Docker Compose is 1.21.1
- Windows Xming X Server is 7.7.0.25 - using IPv4 interface 192.168.1.101
Dockerfile: Dockerfile-dev-ubuntu_xenial - creates the Docker image
FROM ubuntu:xenial
ARG DEBIAN_FRONTEND=noninteractive
LABEL maintainer "Your NAME <[email protected]>"
RUN apt-get update ; apt-get install -y apt-utils desktop-file-utils dialog nautilus build-essential debhelper fakeroot ccache lsb-release
RUN apt-get install -y autotools-dev autoconf pkg-config libtool curl gedit git wget unzip lintian
RUN apt-get install -y qtcreator valgrind
RUN apt-get install -y sudo \
&& groupadd -r user -g 1000 \
&& useradd -u 1000 -r -g user -m -d /user -s /sbin/nologin -c "Build pkg user" user \
&& chmod 755 /user \
&& echo "user ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/user \
&& chmod 0440 /etc/sudoers.d/user
WORKDIR /user
USER user
VOLUME ["/buildpkg", "/user/projects", "/user/resources"]
CMD /bin/bash
Compose.yml: compose-dev-linux.yml
version: '3'
services:
# Commands:
# Build: docker-compose -f compose-dev-linux.yml build dev_ubuntu_xenial
# Up : docker-compose -f compose-dev-linux.yml up -d dev_ubuntu_xenial
# Run : docker-compose -f compose-dev-linux.yml run dev_ubuntu_xenial
# Down : docker-compose -f compose-dev-linux.yml down
# Host folders:
# %USERPROFILE%/Projects
# %USERPROFILE%/Projects/Docker-builds
# %USERPROFILE%/Projects/Docker-resources
# Docker configuration file locations:
# %USERPROFILE%/Dockerfiles/Dockerfile-dev-ubuntu_xenial
# %USERPROFILE%/compose-dev-linux.yml
dev_ubuntu_xenial:
security_opt:
- seccomp:unconfined
cap_add:
- SYS_ADMIN
environment:
- DISPLAY=192.168.1.101:0
network_mode: host
image: "application-dev-platform/application:ubuntu_xenial"
container_name: application-dev-ubuntu_xenial
command: bash -c "/bin/bash"
tty: true
build:
context: ./Dockerfiles
dockerfile: Dockerfile-dev-ubuntu_xenial
volumes:
- ./Projects:/user/projects
- ./Projects/Docker-builds:/buildpkg
- ./Projects/Docker-resources:/user/resources
Run: - initial Powershell terminal
- Build image:
docker-compose -f compose-dev-linux.yml build dev_ubuntu_xenial
- Launch Docker detached:
docker-compose -f compose-dev-linux.yml up -d dev_ubuntu_xenial
- List container(s):
docker ps
- Launch container:
docker exec -it <CONTAINER ID> bash
- Launch qtCreator:
user@linuxkit-<generatedid>:~$ qtcreator
Run: - new Powershell terminal
- Launch container:
docker exec -it <CONTAINER ID> bash
- Launch nautilus:
nautilus
Run: - new Powershell terminal
- Launch container:
docker exec -it <CONTAINER ID> bash
- Launch terminal:
user@linuxkit-<generatedid>:~$
Solution 6 - Docker
If you are able to run Kitematic - you can click on exec button to open terminal in selected container.
http://i.stack.imgur.com/PHrvc.png">