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!