Skip to content

Start Multiple Docker Containers

In this lecture we're running two containers side-by-side.

Video

Running multiple docker containers from the command line step by step

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

docker run -it -d --rm --name linux1 ubuntu /bin/bash

This command:

  • creates container named "linux1"
  • additional flags:
  • -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.

Next we'll start the second container:

docker run -it -d --rm --name linux2 ubuntu /bin/bash

👆🏻 This creates container "linux2"

docker attach linux1

This 👆🏻 attaches to container linux1

ls

This lists the file system on linux1

mkdir mylinux1

Creates a new directory on container linux1

ls

Shows that "mylinux1" was created

docker attach linux2

Attaches to container linux2

ls

This one does a few things: - 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

exit

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

docker ps -a

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

exit

Exits the only running container linux1 and deletes the container

On any CLI enter

docker ps -a

That should show you nothing anymore. Perfect, cleanup is done, let's continue!


Last update: May 29, 2022