Infrastructure

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
Start the lab

Docker Compose foundations

Build and run a multi-service application with two custom Dockerfiles.

3h30 · indicative Foundations 18 steps

Expected outcome

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/

Lab steps

Getting to grips with the project

  1. Step 1 : Check the prerequisites

    Explain the difference between Docker Engine, image, container and Compose.

    docker version && docker compose version

    The versions display without error.

  2. 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.

  3. 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

  1. 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/Dockerfile

    The Dockerfile correctly identifies its base image and its working directory.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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 --build

    The command finishes without error and builds api and app.

  5. 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 api

    The useful services are running and MariaDB becomes healthy.

Check end-to-end operation

  1. 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/hello

    The page shows the information passed by the API.

  2. 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.

  3. 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.

  4. Step 18 : Stop without losing the volumes

    Stop the stack and explain the difference between down and down -v.

    docker compose down

    The containers and network are removed; the named volumes are kept.

Resources

Dynamic configuration with .env

Externalise the values, parameterise the builds and tell ARG, ENV and Compose interpolation apart.

3h30 · indicative Parameterisation 17 steps

Expected outcome

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

Lab steps

Prepare the project's values

  1. 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 .env

    A .env file is present and is not meant to be versioned.

  2. 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.

  3. 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.

  4. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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

  1. 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.

  2. 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.

  3. 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.

  4. Step 12 : Display the configuration as actually resolved

    Check the interpolation, avoiding sharing outputs that contain credentials.

    docker compose config --environment && docker compose config

    The tags, ports and build.args match the .env.

  5. 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 ps

    The five services are active and the Hello World works.

Test the consequences of a change

  1. 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 ps

    The browser responds on the host port given in .env.

  2. 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_ENV

    The value returned is consistent with the configuration.

  3. 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).

  4. 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

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

Expected outcome

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

Lab steps

Introduce the reverse proxy

  1. 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 .env

    The environment file exists and the services are identified.

  2. 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.

  3. 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.

  4. 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.

  5. 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

  1. 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.

  2. 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.

  3. 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.

  4. Step 9 : Route phpMyAdmin

    Create db.tp.localhost to internal port 80.

    phpMyAdmin responds on its lab subdomain.

  5. Step 10 : Route Mailpit

    Create mail.tp.localhost to internal port 8025.

    The Mailpit interface is reachable via Traefik.

  6. 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

  1. 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.

  2. 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.

  3. 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

  1. Step 15 : Analyse and launch Compose

    Resolve the network, label and port references, then start the stack.

    docker compose config && docker compose up -d --build

    The stack is operational and Traefik starts correctly.

  2. 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/hello

    The API's JSON response is returned by Traefik.

  3. 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.

  4. 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 down

    The routes are understood; the volume's data is not erased.

Resources

In the same series Step 2 of 2

  1. 1 Course Docker & Docker Compose — The course
  2. 2 Lab Docker & Docker Compose labs You are here

Share this article

In the same category

Course

Infrastructure · Intermediate

Docker & Docker Compose — The course

Images, containers, volumes, networks and Compose: vocabulary, essential commands and good practice.

3 guides · 132 concepts In the same series

Course

Infrastructure · Intermediate

Kubernetes — The course

Pods, deployments, services, configuration and storage: orchestrating containers, from theory to kubectl.

3 guides · 159 concepts In the same series

Course

Infrastructure · Intermediate

Terraform & Ansible — The course

Describe infrastructure with Terraform and configure it with Ansible: providers, playbooks, roles and idempotence.

3 guides · 146 concepts In the same series