After Part 1, where we were Running a single docker container explained step by step, we’re now in Part 2: We are running multiple containers side by side.

Made with Canon 5d Mark III and loved analog lens, Leica APO Macro Elmarit-R 2.8 / 100mm (Year: 1993)
Photo by Markus Spiske / Unsplash

Prerequisites

Again, make sure you installed “Docker Desktop” and you have the Docker Daemon running. Then open a PowerShell on Windows, a Terminal on Mac/Linux and follow along! This is fun.

Docker Run Video Walkthrough

You can also follow along in this video, if you prefer. You can also get this as a full Docker Video QuickStart Course

Step by Step Docker Run Tutorial

This shows the difference between containers and images. We will create two containers (linux1 and linux2) based on the same image (ubuntu)

docker run -it -d --rm --name linux1 ubuntu /bin/bash
The screenshot shows that a container was started in the background. The ID is 9d82766…

This creates container “linux1”.

  • -d starts the container as “detached”. Use “docker attach” to attach to it later on.
  • — rm cleans up the container after stopping. The container will be removed, basically the same as “docker rm container_identifier” after stopping the container. So everything is kept tidy.
  • — name will give the container a dedicated name, which makes it easier to address the container later on.
docker run -it -d --rm --name linux2 ubuntu /bin/bash
Now we started a second container, named “linux2”, ID 7423475..

Creates container “linux2”

docker attach linux1

Attaches to container linux1

ls
Attached to “linux1” container. Listing the file system.

This lists the file system on linux1.

mkdir mylinux1

Creates a new directory on container linux1

ls

shows that “mylinux1” was created

Now open a second PowerShell Window (or Terminal on a Mac) and run in the second PowerShell Window (CLI2):

CLI2: docker attach linux2

Attaches to container linux2

CLI2: ls
Two PowerShell windows. PowerShell window 1 is connected to linux1, PowerShell Windows 2 is connected to linux2. They are different.

Shows that the directory of linux2 is different than linux1, although they are both from the same image “ubuntu”

  • They are separated, they don’t share their file-system
  • The bash process is isolated in the container
CLI2: exit

This will exit the bash and also remove the container because of the — rm command

CLI2: docker ps -a

Shows only one container which is running, the other one got removed

Shows that exiting the container also removes the container
CLI1: exit

Exits the only running container linux1 and deletes the container

docker ps -a

Shows nothing anymore

Shows that no container is in the container-list anymore

In the next part you are going to apply Docker Volumes step by step.

Heads-Up: If you like this, then also check out the video course Understanding Docker and Docker-Compose — 100% Hands-On. It’s a 2.5h Quickstart into Docker. If you want more, then checkout my blog where I have tons of Beginner Tutorials about about Blockchain, Solidity, Ethereum, Docker and other Technical Topics.