Bird
Raised Fist0
Dockerdevops~10 mins

Docker Compose for dev environment - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Docker Compose for dev environment
Write docker-compose.yml
↓
Run 'docker-compose up'
↓
Docker Compose reads config
↓
Create network and volumes
↓
Start containers
↓
Containers run with dev settings
↓
Develop and test
↓
Run 'docker-compose down' to stop
Docker Compose reads the config file, sets up containers with dev settings, and runs them for easy development.
Execution Sample
Docker
version: '3.8'
services:
  web:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    environment:
      - NODE_ENV=development
This docker-compose.yml defines a web service that builds from current folder, maps port 3000, mounts current folder for live code updates, and sets NODE_ENV to development.
Process Table
StepActionEvaluationResult
1Read docker-compose.ymlParse YAMLServices: web defined with build, ports, volumes, environment
2Create networkDefault network createdNetwork 'default' ready
3Create volumeMount current folderVolume mount .:/app set for web
4Build imageDockerfile in .Image built for web service
5Start containerRun container with ports and envContainer 'web' running on port 3000 with NODE_ENV=development
6Container runningCode changes synced via volumeLive reload possible during development
7Run 'docker-compose down'Stop and remove containersContainers stopped and network removed
💡 Stopped after 'docker-compose down' command to clean up dev environment
Status Tracker
VariableStartAfter Step 4After Step 5Final
NetworkNonedefault network createddefault network activeremoved after down
VolumeNonevolume mount setvolume activeremoved after down
Container webNoneimage builtcontainer runningcontainer stopped and removed
Key Moments - 3 Insights
Why do we mount the current folder as a volume in the web service?
Mounting the current folder allows live code changes to be reflected inside the container immediately, enabling fast development without rebuilding the image. See execution_table step 3 and 6.
What happens when we run 'docker-compose down'?
'docker-compose down' stops running containers and removes the network and volumes created for the dev environment, cleaning up resources. See execution_table step 7.
Why do we set NODE_ENV=development in environment variables?
Setting NODE_ENV=development tells the app inside the container to run in development mode, enabling features like debugging and live reload. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the container actually started?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Check the 'Action' column for when the container runs with ports and environment.
According to the variable tracker, what is the state of the network after step 5?
ANo network created
BDefault network active
CNetwork removed
DCustom network active
💡 Hint
Look at the 'Network' row under 'After Step 5' in variable_tracker.
If we remove the volume mount from the compose file, what changes in the execution?
ACode changes won't reflect live inside container
BCode changes inside container update live
CContainer will not start
DPorts will not be mapped
💡 Hint
Refer to key_moments about volume mount and live code updates.
Concept Snapshot
Docker Compose for dev environment:
- Define services in docker-compose.yml
- Use volumes to mount code for live updates
- Map ports to access apps
- Set environment variables for dev mode
- Run 'docker-compose up' to start
- Run 'docker-compose down' to stop and clean
Full Transcript
Docker Compose helps developers run multiple containers easily for development. You write a docker-compose.yml file describing your app's services, ports, volumes, and environment variables. When you run 'docker-compose up', it reads this file, creates a network, mounts your code folder inside the container for live updates, builds the image, and starts the container. This setup lets you develop and test your app quickly. When done, 'docker-compose down' stops containers and cleans up resources. This visual trace showed each step from reading the config to running and stopping containers, tracking network, volume, and container states.

Practice

(1/5)
1. What is the main purpose of using docker-compose.yml in a development environment?
easy
A. To build Docker images only
B. To define and run multiple containers with one command
C. To replace Docker CLI commands completely
D. To monitor container resource usage

Solution

  1. Step 1: Understand Docker Compose role

    Docker Compose uses a YAML file to define multiple containers and their settings.
  2. Step 2: Identify main benefit in dev

    It allows running all containers together with a single command, simplifying setup.
  3. Final Answer:

    To define and run multiple containers with one command -> Option B
  4. Quick Check:

    Compose = multiple containers, one command [OK]
Hint: Compose runs many containers with one file and command [OK]
Common Mistakes:
  • Thinking Compose only builds images
  • Believing Compose replaces all Docker CLI commands
  • Confusing Compose with monitoring tools
2. Which of the following is the correct syntax to expose port 8080 on the host mapped to port 80 in a docker-compose.yml service?
easy
A. ports: - "80:8080"
B. expose: - "80:8080"
C. expose: - "8080:80"
D. ports: - "8080:80"

Solution

  1. Step 1: Understand ports syntax in Compose

    The format is "host_port:container_port" to map host port to container port.
  2. Step 2: Match ports for host 8080 to container 80

    To expose container port 80 on host port 8080, use "8080:80" under ports.
  3. Final Answer:

    ports: - "8080:80" -> Option D
  4. Quick Check:

    HostPort:ContainerPort = 8080:80 [OK]
Hint: Ports use host:container format [OK]
Common Mistakes:
  • Reversing host and container ports
  • Using expose instead of ports for host mapping
  • Incorrect YAML indentation
3. Given this docker-compose.yml snippet, what will be the effect of the volume configuration?
services:
  app:
    image: myapp
    volumes:
      - ./src:/app/src
medium
A. The host's ./src folder is shared with /app/src inside the container
B. The container's /app/src folder is copied to host's ./src folder once
C. The container will have an empty /app/src folder
D. The volume will cause a syntax error

Solution

  1. Step 1: Understand volume syntax in Compose

    Format "host_path:container_path" mounts host folder inside container.
  2. Step 2: Analyze given volume mapping

    Host folder ./src is mounted to container path /app/src, sharing files live.
  3. Final Answer:

    The host's ./src folder is shared with /app/src inside the container -> Option A
  4. Quick Check:

    Volume mount = host folder shared inside container [OK]
Hint: Volumes share host folder inside container path [OK]
Common Mistakes:
  • Thinking volume copies files only once
  • Confusing source and target paths
  • Assuming volume creates empty folder
4. You wrote this docker-compose.yml but get an error when running docker-compose up. What is wrong?
services:
  web:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    environment:
      - DEBUG=true
    command: nginx -g 'daemon off;'
    restart: always
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:
medium
A. The environment variable for db should use list syntax or key-value mapping consistently
B. The volume pgdata is not defined properly under volumes
C. The command for web service is invalid
D. The environment variable for db uses wrong syntax, should be a list

Solution

  1. Step 1: Inspect YAML syntax in command field

    The command: nginx -g 'daemon off;' lacks quotes around the full string, causing YAML to parse it as separate unquoted scalars, leading to a syntax error.
  2. Step 2: Confirm other sections are valid

    Environment uses valid list/map styles (mixed OK), volumes properly defined.
  3. Final Answer:

    The command for web service is invalid -> Option C
  4. Quick Check:

    Multi-word command needs full quotes or YAML list [OK]
Hint: YAML command with spaces: quote whole or use list [OK]
Common Mistakes:
  • Blaming environment syntax mixing (both styles valid)
  • Misreading pgdata volume definition
  • Overlooking unquoted multi-word command
5. You want to set up a development environment with a web app and a database using Docker Compose. The web app needs live code updates from your host machine, and the database should persist data between restarts. Which docker-compose.yml configuration correctly achieves this?
hard
A. services: web: image: mywebapp volumes: - ./code:/app ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: - dbdata:/var/lib/mysql volumes: dbdata:
B. services: web: image: mywebapp ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: dbdata: driver: local
C. services: web: image: mywebapp volumes: - /app:/code ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: - dbdata:/var/lib/mysql volumes: dbdata:
D. services: web: image: mywebapp volumes: - ./code:/app ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass

Solution

  1. Step 1: Ensure live code updates for web app

    Mount host folder ./code to container /app using volumes for live updates.
  2. Step 2: Ensure database data persistence

    Use a named volume (dbdata) mounted to /var/lib/mysql to keep data between restarts.
  3. Step 3: Check ports and environment

    Expose port 3000 for web and set MYSQL_ROOT_PASSWORD for db environment.
  4. Final Answer:

    Configuration with volumes for web and named volume for db -> Option A
  5. Quick Check:

    Volumes for live code and persistent db = services: web: image: mywebapp volumes: - ./code:/app ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: - dbdata:/var/lib/mysql volumes: dbdata: [OK]
Hint: Use volumes for code and named volume for DB persistence [OK]
Common Mistakes:
  • Not mounting host code folder for live updates
  • Missing volume for database persistence
  • Incorrect volume paths or missing volumes section
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard