109

I need to launch two distinct processes on a docker container which requires two terminals.What is the best way to achieve this?

Atlantic0
  • 2,901
  • 5
  • 15
  • 24
  • 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

6 Answers6

189

You can run docker exec -it <container> bash from multiple terminals to launch several sessions connected to the same container.

Elton Stoneman
  • 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
  • Do you know if there's a way to do this inside a Dockerfile? – Robert Sinclair Feb 15 '21 at 03:27
35

To expand on @eltonStoneman's great answer (For all those new docker folks like me):

  1. Open a docker terminal

  2. 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.
  3. Per @eltonStoneman's advice: docker exec -it <container_id> bash

    • Now your docker terminal is showing an interactive terminal to the container.
  4. Open up another docker terminal and perform step 3 to create another interactive terminal to the container. (Rinse and Repeat)

isaacdre
  • 724
  • 6
  • 9
  • 2
    Thanks, 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
10

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.

2

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

gsumk
  • 749
  • 10
  • 15
1

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

  1. Build image: docker-compose -f compose-dev-linux.yml build dev_ubuntu_xenial
  2. Launch Docker detached: docker-compose -f compose-dev-linux.yml up -d dev_ubuntu_xenial
  3. List container(s): docker ps
  4. Launch container: docker exec -it <CONTAINER ID> bash
  5. Launch qtCreator: user@linuxkit-<generatedid>:~$ qtcreator

Run: - new Powershell terminal

  1. Launch container: docker exec -it <CONTAINER ID> bash
  2. Launch nautilus: nautilus

Run: - new Powershell terminal

  1. Launch container: docker exec -it <CONTAINER ID> bash
  2. Launch terminal: user@linuxkit-<generatedid>:~$
Trev
  • 36
  • 1
  • 3
0

If you are able to run Kitematic - you can click on exec button to open terminal in selected container.

VladoDemcak
  • 4,258
  • 3
  • 30
  • 40