I need to launch two distinct processes on a docker container which requires two terminals.What is the best way to achieve this?
6 Answers
You can run docker exec -it <container> bash from multiple terminals to launch several sessions connected to the same container.
- 16,003
- 5
- 43
- 41
-
Thanks for the great answer. What if I now want to open up one of the previous interactive terminals that I launched and left running in background and kill it? Using docker ps only shows the container, but if I run ps aux in any of the terminals I see all the ones that I launched. – Matteo Jun 22 '18 at 18:38
-
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 pswill show the container_id that you just fired up from said image.
- Tip:
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)
- 724
- 6
- 9
-
2Thanks, especially for the `docker ps` bit. BTW, at least on Linux, a normal terminal is fine (doesn't need to be a "docker terminal") – Darren Cook Nov 18 '17 at 17:58
-
Thanks for the great answer. What if I now want to open up one of the previous interactive terminals that I launched and left running in background and kill it? Using docker ps only shows the container, but if I run `ps aux` in any of the terminals I see all the ones that I launched. – Matteo Jun 22 '18 at 18:38
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.
- 808
- 12
- 27
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
- 749
- 10
- 15
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 <your.address@yourmailhost.com>"
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>:~$
- 36
- 1
- 3
If you are able to run Kitematic - you can click on exec button to open terminal in selected container.
- 4,258
- 3
- 30
- 40
-
8He had one problem, now he has two. (run kitematic and then open a new terminal) – SparK Sep 06 '17 at 18:42
-