Labs · DevOps
Lab: Start a Two-Container Stack (Web + MySQL) with Docker Compose, Keep Data in a Named Volume and Keep Secrets in .env
Course: DevOps · Chapter 7: Docker Commands, Networks, Volumes and Compose · Lesson: 7.10 Lab: commands, a name, a volume, one stack
Chapter 7 explains networks, volumes and Compose; this lab runs one small stack end to end.
Chala mitrano! Ek ek docker run lihina kantaaLvaana aahe. Aaj ek compose file, ek command, aakha stack up.
Suppose we are…
Suppose we are on an Instagram-style team and every new developer needs the same local setup: the app plus a MySQL database. Typing two long docker run commands with networks and volumes is slow and different on every laptop. One compose.yaml file in the repo gives everyone the same stack with one command.
In this lab we run MySQL and Adminer (a small web database screen) together. They reach each other by service name, data lives in a named volume, and the password lives only in a .env file that Git ignores.
Goal of this lab
By the end you will have:
- A
compose.yamlwith two services on one network. - A table that survives
docker compose downandupbecause of a named volume. - The database password only in
.env, never in the compose file.
What you need
- Docker with Compose v2:
docker compose versionworks. - Free ports 8080 on your laptop.
- 40 minutes.
Part 1: Files
Create the folder and the
.envfile. Use a practice password, never a real one:mkdir shop-stack && cd shop-stack printf "MYSQL_ROOT_PASSWORD=lab-only-Change-Me-1\nMYSQL_DATABASE=shop\n" > .env printf ".env\n" > .gitignoreCreate
compose.yaml:services: db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE} volumes: - dbdata:/var/lib/mysql healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] interval: 10s retries: 10 web: image: adminer:4 ports: - "8080:8080" depends_on: db: condition: service_healthy volumes: dbdata:
Part 2: Start the stack
Start everything in the background and check it:
docker compose up -d docker compose psWhat you should see:
dbashealthyandwebasrunningwith0.0.0.0:8080->8080/tcp.Open
http://localhost:8080. Log in with System MySQL, Server db, Username root, the password from.env, Database shop.What you should see: the Adminer screen for database
shop. The server namedbworks because Compose gives each service a DNS name on the stack network.Create a table and a row from the terminal:
docker compose exec db sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" shop -e "CREATE TABLE orders(id INT PRIMARY KEY, item VARCHAR(40)); INSERT INTO orders VALUES (1, \"masala tea\"); SELECT * FROM orders;"'What you should see: one row:
1 masala tea.
Part 3: Prove the data survives
Remove the containers (not the volume) and start again:
docker compose down docker compose up -d docker volume lsWhat you should see: a volume ending in
_dbdata.Read the row again:
docker compose exec db sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" shop -e "SELECT * FROM orders;"'What you should see:
1 masala teais still there.Read the logs of one service:
docker compose logs --tail 20 db
Clean up
Remove containers, network and the volume (this deletes the practice data):
docker compose down -v docker volume lsWhat you should see: the
_dbdatavolume is gone.
Ravindra Bagale's Tip
docker compose down keeps your data; docker compose down -v deletes it. Read the command twice before adding -v on a real project.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Writing the password directly in compose.yaml | The secret goes into Git | Use ${VAR} and a git-ignored .env |
Using localhost as the DB host inside Adminer | Connection refused | Use the service name db |
| Web starts before MySQL is ready | First connection fails | Use a healthcheck and condition: service_healthy |
Running down -v by habit | All database data deleted | Use plain down unless you want a fresh start |
Self-check checklist
0 of 5 done
Try-at-home challenge
Add a third service nginx that serves a static index.html from a bind mount (./site:/usr/share/nginx/html:ro) on port 8081. Then run docker network inspect on the stack network and list the three containers.
Samajla ka? One file, one command, same stack for every developer. Aata pudhe jaauya CI/CD kade.