DOCKER - 10 Practical Exercise v2
Now that you're comfortable with the basic container commands, time for a small exercise: start a three-container application. We won't wire the containers together with application data or source files — we just want to practise launching different images side by side, see how they differ, and manage them as a group. Useful resources include the --help flag on any command and the official documentation at docs.docker.com. The site is open source and continuously updated through GitHub, so it's worth bookmarking even if you don't read everything today.
Goals of the exercise
You should run each container with -d (detach) and --name, picking any names you like — naming makes local management much easier. Because containers cannot share the same host port, you'll publish each one on a different port:
- nginx — proxy role, stays on host port
80 - httpd (Apache) — host port
8080 - mysql — default port
3306
The new flag to discover is -e (or --env), which sets environment variables inside the container. It's the standard way to inject configuration that differs between development, test and production. The official mysql image offers an option to generate a random root password on first start; you'll need to peek at the container logs at startup to find the generated password.
When everything is up and you're satisfied, tear it all down with docker container stop and docker container rm. Use docker container ls to confirm the three containers are running, and docker container ls -a afterwards to make sure none are left behind. Try the exercise on your own, and we'll go through the solution in the next video.
Summary
This practical Docker exercise teaches you how to start and manage multiple containers simultaneously with different image types and configurations. You'll learn to assign unique ports to each container, configure environment variables, and use essential Docker commands like docker run, docker ps, docker stop, and docker rm to manage your containers effectively.
Key points
- Running multiple containers requires assigning each container a unique port, as multiple containers cannot listen on the same port simultaneously
- Use the --name flag when running containers to make them easier to manage and identify
- Environment variables can be passed to containers using the -e or --environment flags with the docker run command
- The Nginx container acts as a proxy and typically runs on port 80, while services like Apache/httpd run on alternate ports like 8080
- Use docker logs to view initialization information, such as randomly generated MySQL root passwords
- Use docker ps to list running containers and docker ps -a to view all containers including stopped ones
FAQ
Why do multiple containers need different ports?
Each container cannot listen on the same port simultaneously. Assigning unique ports allows each container to receive incoming traffic independently.
How do you pass environment variables to a Docker container?
Use the -e or --environment flag followed by the variable name and value when running docker run, such as -e MYSQL_ROOT_PASSWORD=mypassword.
How can you find the randomly generated MySQL root password?
Check the container's logs using the docker logs command, which will display the randomly generated password during the initial startup.