How to open multiple terminals in docker?

Docker

Docker 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):

  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.
  1. Per @eltonStoneman's advice: docker exec -it <container_id> bash
  • Now your docker terminal is showing an interactive terminal to the container.
  1. 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

  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>:~$

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">

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtlantic0View Question on Stackoverflow
Solution 1 - DockerElton StonemanView Answer on Stackoverflow
Solution 2 - DockerisaacdreView Answer on Stackoverflow
Solution 3 - DockerNerijus Gedrimas 'Nereus Calm'View Answer on Stackoverflow
Solution 4 - DockergsumkView Answer on Stackoverflow
Solution 5 - DockerTrevView Answer on Stackoverflow
Solution 6 - DockerVladoDemcakView Answer on Stackoverflow