Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Start a Two-Container Stack (Web + MySQL) with Docker Compose, Keep Data in a Named Volume and Keep Secrets in .env

Beginner40 minDocker Desktop or Docker Engine with Compose v2 · Terminal · A browser

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.yaml with two services on one network.
  • A table that survives docker compose down and up because of a named volume.
  • The database password only in .env, never in the compose file.

What you need

  • Docker with Compose v2: docker compose version works.
  • Free ports 8080 on your laptop.
  • 40 minutes.

Part 1: Files

  1. Create the folder and the .env file. 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" > .gitignore
    
  2. Create 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

  1. Start everything in the background and check it:

    docker compose up -d
    docker compose ps
    

    What you should see: db as healthy and web as running with 0.0.0.0:8080->8080/tcp.

  2. 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 name db works because Compose gives each service a DNS name on the stack network.

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

  1. Remove the containers (not the volume) and start again:

    docker compose down
    docker compose up -d
    docker volume ls
    

    What you should see: a volume ending in _dbdata.

  2. 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 tea is still there.

  3. Read the logs of one service:

    docker compose logs --tail 20 db
    

Clean up

  1. Remove containers, network and the volume (this deletes the practice data):

    docker compose down -v
    docker volume ls
    

    What you should see: the _dbdata volume 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

MistakeWhat happensFix
Writing the password directly in compose.yamlThe secret goes into GitUse ${VAR} and a git-ignored .env
Using localhost as the DB host inside AdminerConnection refusedUse the service name db
Web starts before MySQL is readyFirst connection failsUse a healthcheck and condition: service_healthy
Running down -v by habitAll database data deletedUse 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.