Docker Basics Tutorial
These exercises are designed to complement the Docker Basics lab you should have completed earlier, offering additional hands-on practice to reinforce your understanding of key Docker concepts. By working through these tasks, you’ll gain deeper insights into:
- Running and managing containers with real-world examples like MySQL and Nginx.
- Configuring ports, volumes, and environment variables for container customization and data persistence.
- Lifecycle management of containers, including starting, stopping, and cleaning up resources.
These exercises aim to challenge you while reinforcing the skills you’ve learned, helping you build a solid foundation for working with Docker and preparing for more advanced containerization topics like Kubernetes.
Each task is self-contained with clear objectives and step-by-step instructions to guide you through the setup, testing, and cleanup processes. Take your time, experiment, and explore the flexibility that Docker offers.
Create a MySQL Container
Objective
Set up and manage a MySQL container using Docker while applying key concepts like publishing ports, volume mounting, and environment variables configuration.
Requirements
- Use the official MySQL image on Docker Hub
- Use the
8.0.40
tag - Publish the container's port
3306
to the host on port33306
- Set the
MYSQL_ROOT_PASSWORD
environment variable tomypassword
- Create a
~/k8scourse/1.2/mysql
directory and mount it to/var/lib/mysql
- Name the container
custom-mysql
- Launch the container in detached mode (
-d
)
Test
- Follow the logs
- Install a mysql client and try to access the container
sudo apt update
sudo apt install mysql-client
mysql -h 127.0.0.1 -P 33306 -u root -p
- When prompted, enter the password you set as the
MYSQL_ROOT_PASSWORD
environment variable
Lifecycle Management / Cleanup
- Bring the container to the foreground
- Stop the container from the shell (
CTRL + C
) - Restart the container
- Stop and remove the container
- Remove the
mysql:8.0.40
image
Deploy a Nginx Web Server
Objective
Set up and manage an Nginx web server using Docker while learning about port publishing, static file hosting, and container naming.
Requirements
- Use the official Nginx image on Docker Hub
- Use the
sha256:fb197595ebe76b9c0c14ab68159fd3c08bd067ec62300583543f0ebda353b5be
index digest - Publish the container's port
80
to the host on port8090
- Create a directory
~/k8scourse/1.2/nginx/html
on your host machine. - Add a simple
index.html
file in that directory with a message like: "Welcome to Nginx served by Docker!" - Mount the
~/k8scourse/1.2/nginx/html
directory to/usr/share/nginx/html
inside the container. - Name the container
custom-nginx-2
- Launch the container in detached mode (
-d
)
Test
- Open a web browser and navigate to
http://localhost:8090
to confirm the server is running and serving your customindex.html
file - Check the container logs to observe Nginx access logs
Lifecycle Management / Cleanup
- Stop and restart the container.
- Remove the container.
- Remove unused Docker resources.