DOCKER - 11 Solution Exercice v2

This video walks through the solution to the previous multi-container exercise. Your exact commands may differ from mine — that's fine as long as the end result is three containers running three different images with the required parameters. We'll detach each container, name it, publish a port, and pass any environment variable the image needs.

Launching the three containers

Start with MySQL. We detach it, expose port 3306, give it a name, and ask the image to generate a random root password:

docker container run -d -p 3306:3306 --name dataB \
  -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql

docker container logs dataB

The logs contain the auto-generated password — I copy it just in case I need to connect to the database later. Next, the Apache web server on host port 8080, and finally Nginx as a proxy on host port 80:

docker container run -d --name webserver -p 8080:80 httpd
docker container run -d --name proxy -p 80:80 nginx
docker container ls

All three appear running with the right ports forwarded and the right names. To verify them quickly, hit each port with curl:

  • curl localhost — returns the Nginx default page
  • curl localhost:8080 — returns the Apache "It works!" page
  • docker container logs dataB — confirms MySQL is ready

Once everything is verified, clean up with a single batch command:

docker container stop proxy dataB webserver
docker container rm proxy dataB webserver
docker container ls -a

The last ls -a returns an empty list — the system is back to a clean state and the exercise is complete.

Summary

This lesson walks through the complete solution to a Docker multi-container exercise. You'll learn how to spin up three different containers running distinct images with proper environment variables and port mappings, verify they're running correctly using Docker commands like docker ps and docker logs, and then clean up all containers and images once you're done.

Key points

  • Launching multiple containers from different images with docker run and environment variables
  • Verifying container status and logs using docker ps and docker logs commands
  • Mapping container ports to host ports (e.g., port 80 to port 80)
  • Checking that all three containers are running properly with correct names and port assignments
  • Stopping containers with docker stop and removing them with docker rm
  • Complete cleanup process including removing all unused containers and images

FAQ

What does this exercise solution teach you?

This lesson demonstrates the practical workflow of running and managing multiple Docker containers simultaneously, including launching them with proper configuration, monitoring their status, and performing complete cleanup.

What Docker commands are covered in this solution?

The key commands covered are docker run (for launching containers), docker ps (for listing running containers), docker logs (for viewing container output), docker stop (for stopping containers), and docker rm (for removing containers).

Why is the cleanup step important at the end?

Cleanup ensures you remove all stopped containers and images to free up disk space and keep your Docker environment organized, preventing clutter from accumulating over time.