DOCKER - 10 Practical Exercise v2
Welcome to this introductory course to Docker. Today we are going to do a small exercise. Now that you are familiar with the basic container commands, you can try starting several containers at the same time. We will walk through the process of running multiple image types in different containers concurrently. You may need documentation: the --help flag is there for that, but you can also visit docs.docker.com, an open-source site you can contribute to via GitHub.
You are going to start a three-container application. We will not configure these containers with application data or source files; we just want ideas on how to start them, what makes them different, and how to manage them together. You will want to use -d to detach, and --name to give each container an easy-to-remember name. Because two containers cannot listen on the same host port, each must be exposed on its own port.
The three containers
- nginx — will play the role of proxy and stay on port 80.
- httpd (Apache) — will run on port 8080.
- mysql — will run on its default port 3306.
The three containers are not going to talk to each other; we will just start them and then delete them. One option you have not used yet is passing environment variables: -e (or --environment) injects any variable you want into the container. This is the most common way to pass environment-based settings (different values for dev/test/prod).
The official mysql image has an option to set a random root password on startup. To find that password, check the container logs at first launch. Once your three containers are running and you are satisfied, clean everything up with docker container stop followed by docker container rm. Use docker container ls to verify what is running and docker container ls -a to make sure nothing remains. Try it on your side, the solution will come in the next video.
Summary
This lesson introduces a hands-on Docker exercise where learners deploy and manage three containers simultaneously (Nginx proxy, Apache httpd, and MySQL). The focus is on practical Docker operations: naming containers, assigning different ports, passing environment variables, and managing the container lifecycle. Learners explore real workflows of starting, monitoring, and cleaning up multiple containers running together.
Key points
- Use the `docker run --name` flag to assign unique names to containers, making them easier to manage and reference in commands
- Assign different ports to each container since containers cannot share the same port; Nginx listens on 80 (proxy), Apache on 8080, and MySQL on its default port 3306
- Pass environment variables to containers using `docker run -e` or `--env` flags; MySQL uses these to generate a random root password at startup
- Retrieve container output and startup logs with `docker logs <container-id>` to find generated passwords and verify container behavior
- Use `docker ps` to list running containers and `docker ps -a` to view all containers including stopped ones
- Clean up after the exercise with `docker stop <container-id>` and `docker rm <container-id>` to remove containers
FAQ
Why must each container run on a different port?
Each container can only listen on a single port at a time. If multiple containers attempted to use the same port on the host, there would be a port conflict. Each container must have its own unique port so the host can correctly forward traffic to the right container.
How do I find the MySQL root password that was generated at startup?
The MySQL Docker image generates a random root password during the initial startup. You can view this password by examining the container logs with the `docker logs` command, which will display the generated password in the output.
What is the purpose of environment variables in Docker containers?
Environment variables allow you to pass configuration parameters and settings to containers at runtime without modifying the container image. This is a standard practice for maintaining different configurations across development, testing, and production environments.