I need to launch two distinct processes on a docker container which requires two terminals.What is the best way to achieve this?
-
Possible duplicate of [Is it possible to start a shell session in a running container (without ssh)](http://stackoverflow.com/questions/17903705/is-it-possible-to-start-a-shell-session-in-a-running-container-without-ssh) – Daerdemandt Oct 01 '16 at 19:24
-
This is not what the question is about, but rather my reason for finding this question, so it might help others in the future. In VS Code, if you have the Docker extension installed, you can simply right-click the container and attach either a VS Code instance or a terminal. – Ido_f Jan 22 '23 at 14:49
8 Answers
You can run docker exec -it <container> bash
from multiple terminals to launch several sessions connected to the same container.

- 17,906
- 5
- 47
- 44
-
1Thanks 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
-
-
All this assuming that the container features **bash**. In my case I had to go down to **sh**. And pay attention that obviously **the filesystem is the same for all the terminals!!!** – Marco Faustinelli Nov 25 '22 at 16:08
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 terminal and perform step 3 to create another interactive terminal to the container. (Rinse and Repeat)
-
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.

- 918
- 13
- 28
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

- 809
- 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
first of all I entered below command:
docker exec -it <containerName> bash
but docker said this container doesn't exist... after that I replaced containerName with containerID and this trick worked for me;
if you want to know each containers id run below command:
sudo docker ps
after that run this command to open new terminal to your container:
docker exec -it <containerID> bash
cheers!

- 11
- 4
I started a container in a terminal (-i
interactive, -t
TTY):
docker run -i -t <IMAGE_URL> /bin/bash
My command prompt now starts with root@484ded1212aa:/
in which 484ded1212aa
is the CONTAINER ID
. In another tab (or window) I used that ID:
docker exec -i -t 484ded1212aa /bin/bash
Now both terminal tabs start with root@484ded1212aa:/
and the second one contains the Git repo that I had cloned in the first terminal (between the above two commands). This was useful because I wanted to interactively change the code in one tab, and run it in another.
Another way to get the CONTAINER_ID
or NAMES
is by the command:
docker container ls
which gives me:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
484ded1212aa registry.gitlab.com/molcas/dockerfiles/gcc-4.8 "/bin/bash" 2 hours ago Up 2 hours hardcore_einstein
Notice that there's still only one container, even though it's open in two tabs.

- 1,223
- 2
- 19
- 42
If you are able to run Kitematic - you can click on exec button to open terminal in selected container.

- 4,893
- 4
- 35
- 42
-
9He had one problem, now he has two. (run kitematic and then open a new terminal) – SparK Sep 06 '17 at 18:42
-