Hands-on labs Intermediate Free
Docker & Docker Compose labs
Three progressive labs: build, configure then expose a multi-service application with Compose.
- 3 labs
- 53 steps
Docker Compose foundations
Build and run a multi-service application with two custom Dockerfiles.
3h30 · indicative Foundations 18 steps
An HTML page calls the PHP API; MariaDB is reachable in phpMyAdmin; a test message appears in Mailpit.
Prerequisites
- Docker Engine or Docker Desktop and Docker Compose V2 available
- Terminal, browser and Internet connection for the first build
- Student pack: folder ateliers/niveau-1/
Getting to grips with the project
-
Step 1 : Check the prerequisites
Explain the difference between Docker Engine, image, container and Compose.
docker version && docker compose versionThe versions display without error.
-
Step 2 : Explore the folder structure
In ateliers/niveau-1, identify compose.yaml, api/Dockerfile, app/Dockerfile and the PHP/HTML/SCSS sources.
The two built services and the three images used directly are identified.
-
Step 3 : Define the role of the five services
Link api, app, db, phpmyadmin and mailer to their functions; state that Mailpit receives emails for testing only.
The diagram of exchanges browser → app/API → database or SMTP is explained.
Create the custom images
-
Step 4 : API Dockerfile: start from Debian
Complete FROM, WORKDIR and the copying of the sources; show that Debian does not come with PHP ready to use.
cat api/DockerfileThe Dockerfile correctly identifies its base image and its working directory.
-
Step 5 : Install PHP with RUN
Add php-cli, php-mysql, php-mbstring and the dependencies via apt-get; clean the APT lists.
The RUN commands prepare the dependencies at build time.
-
Step 6 : Install Composer and the PHP dependencies
Install Composer with signature verification; run composer install from composer.json.
Composer is present in the image and the dependencies are installed.
-
Step 7 : Start the PHP API
Fill in EXPOSE 8000 and CMD for the PHP development server. Tell apart documenting a port from publishing it through Compose.
The API listens on 0.0.0.0:8000 in the container.
-
Step 8 : app Dockerfile: compile the SCSS with Node
Use a Node stage to install Sass and build the CSS files.
CSS generation happens during docker build.
-
Step 9 : Serve the frontend with Nginx
Create a second FROM nginx stage and copy the resources produced from the Node stage.
The final image serves HTML/CSS/JS without a Node runtime engine.
Assemble the Compose stack
-
Step 10 : Configure MariaDB
Declare db via image, environment and a named volume; explain why persistence matters.
db_data protects the data files against the container's ordinary recreation.
-
Step 11 : Configure phpMyAdmin and Mailpit
Use their existing images. Set PMA_HOST=db and Mailpit's web interface on 8025.
phpMyAdmin targets db; Mailpit receives the API's SMTP on mailer:1025.
-
Step 12 : Configure api and app with build
Fill in build.context and dockerfile for the two custom services. Define the db and SMTP connection, the dependencies and the ports.
The five services have their essential settings.
-
Step 13 : Validate and build the project
Fix the errors reported by Docker Compose; create and start the images.
docker compose config && docker compose up -d --buildThe command finishes without error and builds api and app.
-
Step 14 : Observe the containers and diagnose
Identify the services started, check the API or database logs if needed.
docker compose ps && docker compose logs --tail=60 apiThe useful services are running and MariaDB becomes healthy.
Check end-to-end operation
-
Step 15 : Check the interface and the Hello World
Open http://localhost:8080 then test the backend's JSON response through the Nginx proxy.
curl -i http://localhost:8080/api/helloThe page shows the information passed by the API.
-
Step 16 : Test the database
Go to http://localhost:8081, log in with the lab's fictitious credentials and find the tp_docker database.
The phpMyAdmin connection to MariaDB works.
-
Step 17 : Send a test message
Submit the form, observe the record in the database and the message at http://localhost:8025.
curl -s -X POST http://localhost:8080/api/messages -H "Content-Type: application/json" -d '{"sender":"TP","content":"Bonjour Docker"}'A row is created in the database and an email appears in Mailpit.
-
Step 18 : Stop without losing the volumes
Stop the stack and explain the difference between down and down -v.
docker compose downThe containers and network are removed; the named volumes are kept.
Resources
- Docker Compose — concepts — Understanding project, service, network and volume
- Dockerfile reference — Definition of the FROM, RUN, COPY, CMD, ARG and ENV instructions
- Docker Compose — services reference — Syntax of services, ports, environment, build and depends_on
- Multi-stage build — Use Node at build time without keeping it in the final container
- Sass — CLI — Compiling SCSS files
- Mailpit — Docker images — Mail-capture interface and internal SMTP port
- phpMyAdmin — Docker image — PMA_HOST parameters and connecting to MariaDB
Dynamic configuration with .env
Externalise the values, parameterise the builds and tell ARG, ENV and Compose interpolation apart.
3h30 · indicative Parameterisation 17 steps
The stack is rebuilt from versions and parameters defined in .env, and the students explain what acts at build time or at run time.
Prerequisites
- Level 1 completed or Docker Compose operation already mastered
- Student pack: folder ateliers/niveau-2/
- Understand the differences between the .env file, YAML interpolation and variables in the container
Prepare the project's values
-
Step 1 : Create the .env file
Copy the template supplied in the level 2 folder; check the difference between the shared template and machine-specific values.
cp .env.example .envA .env file is present and is not meant to be versioned.
-
Step 2 : Identify the image variables
Spot DEBIAN_TAG, PHP_VERSION, NODE_VERSION, NGINX_VERSION, MARIADB_VERSION, PHPMYADMIN_VERSION and MAILPIT_VERSION.
The versions are used to select a build base or an image tag.
-
Step 3 : Identify the ports and credentials
Spot APP_PORT, API_PORT, PHPMYADMIN_PORT, MAILPIT_PORT and the DB_* settings.
The correspondence between .env values and runtime ports/variables is explained.
-
Step 4 : Tell .env and environment apart
Understand that a variable in .env does not automatically appear in a container: it must be referenced in Compose if needed.
The variables injected into api and db are precisely identified.
Parameterise Dockerfiles with ARG and ENV
-
Step 5 : Configure Debian and PHP at build time
Introduce ARG DEBIAN_TAG before FROM, then ARG PHP_VERSION to choose the names of the packages installed by APT.
The role of the two ARGs and their scope are known.
-
Step 6 : Check that PHP is really available
Do not assume an arbitrary value of PHP_VERSION exists in the selected Debian image's repositories.
The student can explain an apt-get failure caused by an unavailable version.
-
Step 7 : Set ENV for the API
Use ENV APP_ENV=development and recognise that an ENV value can be overridden by Compose.
APP_ENV is visible at run time and distinct from the build ARGs.
-
Step 8 : Parameterise the front-end multi-stage build
Define NODE_VERSION before FROM node and NGINX_VERSION before FROM nginx, with the right scope for each stage.
The two stages of the Dockerfile select the expected versions.
Interpret and apply Compose
-
Step 9 : Inject the build arguments
Complete build.args for api and app using the `${…}` variables from the .env file.
The resolved configuration contains the expected build arguments.
-
Step 10 : Make images and ports configurable
Use interpolation on the MariaDB/phpMyAdmin/Mailpit tags and the ports published on the host side.
The values are centralised without changing the Compose sources.
-
Step 11 : Know the default and required values
Explain `${VAR:-value}` and `${VAR:?message}`; analyse the error produced when a required variable is missing.
Compose refuses a configuration lacking a required variable.
-
Step 12 : Display the configuration as actually resolved
Check the interpolation, avoiding sharing outputs that contain credentials.
docker compose config --environment && docker compose configThe tags, ports and build.args match the .env.
-
Step 13 : Rebuild and restart the stack
Rebuild the custom images and check the services, then HTML/API access.
docker compose up -d --build && docker compose psThe five services are active and the Hello World works.
Test the consequences of a change
-
Step 14 : Test a custom port
Temporarily change APP_PORT in .env, recreate the services if necessary, then check the new URL.
docker compose up -d && docker compose psThe browser responds on the host port given in .env.
-
Step 15 : Check the container's environment
Observe APP_ENV in the API container and describe the difference between build and runtime.
docker compose exec api printenv APP_ENVThe value returned is consistent with the configuration.
-
Step 16 : Understand the limits of the MariaDB volume
Explain why changing DB_PASSWORD does not automatically change a user already created in the existing volume.
The student identifies the risk associated with down -v (data destruction).
-
Step 17 : Justify protecting secrets
Check .gitignore, do not publish the lab's passwords, name a secrets solution for a real context.
The sensitive .env file is excluded from the repository and the method's limits are understood.
Resources
- Docker Compose — concepts — Understanding project, service, network and volume
- Dockerfile reference — Definition of the FROM, RUN, COPY, CMD, ARG and ENV instructions
- Compose variables and interpolation — The ${VAR}, ${VAR:-default} and ${VAR:?required} syntaxes
- Variables in containers — How the environment key behaves
- ARG and ENV in Dockerfile — Scope and uses of build arguments and environment variables
- Docker secrets security — Why .env is not a secrets vault
Traefik and reverse proxy
Centralise the HTTP entry point, route by host and path and isolate the networks.
3h30 · indicative Network and exposure 18 steps
Traefik exposes the application, the API, phpMyAdmin and Mailpit from a single local HTTP port, while MariaDB stays on the internal network.
Prerequisites
- Levels 1 and 2 completed or Docker architecture mastered
- Student pack: folder ateliers/niveau-3/
- Understand port publishing, DNS resolution and reverse proxy
Introduce the reverse proxy
-
Step 1 : Retrieve level 3 and its environment
Copy .env.example to .env, examine the supplied compose.yaml and compare the ports with those of level 2.
cp .env.example .envThe environment file exists and the services are identified.
-
Step 2 : Describe a reverse proxy
Explain how an HTTP entry point receives a request then sends it to an internal service based on a rule.
The path browser → Traefik → app/API/tools is correctly described.
-
Step 3 : Configure the Traefik service
Declare a Traefik image, the Docker provider, the web entrypoint and the dashboard, limiting exposure to the local machine.
Traefik listens on the configured HTTP entry point.
-
Step 4 : Understand the Docker socket
Analyse the /var/run/docker.sock:ro mount needed for automatic discovery and its security risk.
The mount is identified as sensitive, not suited as is to Internet exposure.
-
Step 5 : Publish a single HTTP port
Publish 127.0.0.1:8088:80 on Traefik and remove the needless direct publications from the application services.
External HTTP access goes only through Traefik.
Create the routes via Docker labels
-
Step 6 : Explicitly enable discovery
Configure exposedbydefault=false, then the traefik.enable=true labels only for the services to expose.
A service without the appropriate label is not published.
-
Step 7 : Route the frontend
Create the rule Host(`tp.localhost`) to Nginx internal port 80.
The interface is reachable at http://tp.localhost:8088.
-
Step 8 : Route the API on /api
Configure Host(`tp.localhost`) && PathPrefix(`/api`) to port 8000; adjust the priority if needed.
The /api/hello call is handled by the API, not by Nginx.
-
Step 9 : Route phpMyAdmin
Create db.tp.localhost to internal port 80.
phpMyAdmin responds on its lab subdomain.
-
Step 10 : Route Mailpit
Create mail.tp.localhost to internal port 8025.
The Mailpit interface is reachable via Traefik.
-
Step 11 : Enable a lab dashboard
Configure a router to api@internal then open /dashboard/ with the trailing slash.
The local Traefik dashboard describes the routers and services detected.
Separate the network communications
-
Step 12 : Define a proxy network
Attach Traefik and the web services to the proxy network; set the traefik.docker.network label when a service has several networks.
Traefik reaches app, api, phpMyAdmin and mailer.
-
Step 13 : Define an internal backend network
Attach db, api, phpmyadmin and mailer to the backend: internal: true network; exclude db from the proxy network.
The database has no port published on the host.
-
Step 14 : Explain the internal DNS
Justify why DB_HOST=db and SMTP_HOST=mailer remain usable between services even after introducing Traefik.
Internal communication does not depend on the *.localhost names.
Start, check and secure
-
Step 15 : Analyse and launch Compose
Resolve the network, label and port references, then start the stack.
docker compose config && docker compose up -d --buildThe stack is operational and Traefik starts correctly.
-
Step 16 : Test API routing
Check Host and PathPrefix by calling the Traefik port; use an explicit Host if *.localhost is not resolved.
curl -i -H "Host: tp.localhost" http://127.0.0.1:8088/api/helloThe API's JSON response is returned by Traefik.
-
Step 17 : Validate the four interfaces
Test tp.localhost:8088, db.tp.localhost:8088, mail.tp.localhost:8088 and traefik.tp.localhost:8088/dashboard/.
Each interface responds and the API still sends emails to Mailpit.
-
Step 18 : Inspect then clean up
Examine the Traefik logs and the networks; stop cleanly without removing the DB volume.
docker compose logs --tail=80 traefik && docker compose downThe routes are understood; the volume's data is not erased.
Resources
- Docker Compose — concepts — Understanding project, service, network and volume
- Dockerfile reference — Definition of the FROM, RUN, COPY, CMD, ARG and ENV instructions
- Traefik — Docker provider — Discovering services via the Docker provider
- Traefik — HTTP routers — Host and PathPrefix rules and route priority
- Compose networks — Isolated networks, Docker DNS and service connectivity
- Docker Engine security — Risks linked to access to the daemon and the Docker socket
In the same series Step 2 of 2
- 1 Course Docker & Docker Compose — The course
-
2 Lab Docker & Docker Compose labs You are here
In the same category
Infrastructure · Intermediate
Docker & Docker Compose — The course
Images, containers, volumes, networks and Compose: vocabulary, essential commands and good practice.
Infrastructure · Intermediate
Kubernetes — The course
Pods, deployments, services, configuration and storage: orchestrating containers, from theory to kubectl.
Infrastructure · Intermediate
Terraform & Ansible — The course
Describe infrastructure with Terraform and configure it with Ansible: providers, playbooks, roles and idempotence.