0
0
Dockerdevops~5 mins

Docker Compose for dev environment - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you develop software, you often need several services like a web server and a database to work together. Docker Compose helps you start and manage these services easily with one command, so you can focus on coding instead of setup.
When you want to run a web app and a database together on your computer without installing them separately.
When you need to share your development setup with teammates so everyone uses the same environment.
When you want to quickly start and stop multiple services with one command during development.
When you want to avoid conflicts between different projects by isolating their services.
When you want to test how your app works with other services like Redis or message queues locally.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: devuser
      POSTGRES_PASSWORD: devpass
      POSTGRES_DB: devdb
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data: {}

This file defines two services: web and db.

The web service uses the nginx image and maps port 8080 on your computer to port 80 inside the container. It also mounts a local html folder as read-only to serve your web files.

The db service uses the postgres image with environment variables to set up the database user, password, and database name. It maps port 5432 and uses a volume to keep data persistent.

The volumes section creates a named volume db-data to store database files outside the container.

Commands
This command starts all services defined in the docker-compose.yml file in the background. It creates containers, networks, and volumes as needed.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "db-data" with default driver Creating devenvironment_web_1 ... done Creating devenvironment_db_1 ... done
-d - Run containers in detached mode (in the background)
This command lists all running containers started by Docker Compose, showing their status and ports.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports -------------------------------------------------------------------------------- devenvironment_db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp devenvironment_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command shows the logs from the web service container, useful to check if the web server started correctly.
Terminal
docker-compose logs web
Expected OutputExpected
[devenvironment_web_1] 2024/06/01 12:00:00 [notice] 1#1: using the "epoll" event method [devenvironment_web_1] 2024/06/01 12:00:00 [notice] 1#1: nginx/1.23.0 started
This command stops and removes all containers, networks, and default volumes created by Docker Compose, cleaning up your environment.
Terminal
docker-compose down
Expected OutputExpected
Stopping devenvironment_web_1 ... done Stopping devenvironment_db_1 ... done Removing devenvironment_web_1 ... done Removing devenvironment_db_1 ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: Docker Compose lets you start and stop multiple related services with one simple file and commands.

Common Mistakes
Not mapping ports in the docker-compose.yml file
Without port mapping, you cannot access the services from your computer, so your app won't work as expected.
Always map container ports to your local machine ports using the ports section.
Running 'docker-compose up' without the -d flag and closing the terminal
The containers stop when the terminal closes because they run in the foreground.
Use 'docker-compose up -d' to run containers in the background.
Not using volumes for persistent data like databases
Data will be lost when containers are removed if volumes are not used.
Define volumes in docker-compose.yml to keep data safe across container restarts.
Summary
Use docker-compose.yml to define multiple services and how they connect.
Start all services together with 'docker-compose up -d' and check their status with 'docker-compose ps'.
Stop and clean up services with 'docker-compose down' to keep your environment tidy.