Loading

Quipoin Menu

Learn • Practice • Grow

docker / Docker Compose File
tutorial

Docker Compose File

The heart of Docker Compose is the docker-compose.yml file. This YAML file defines all the services, networks, and volumes for your application. Understanding its structure is key to using Compose effectively.

Compose File Structure

A typical docker-compose.yml has three main top‑level sections:
  • services: Defines the containers (each service becomes a container).
  • networks: (Optional) Custom networks for services.
  • volumes: (Optional) Named volumes for persistent data.

Basic Service Definition

Here’s a simple service using an image:
services:
web:
image: nginx:latest
ports:
- "8080:80"
environment:
- NGINX_HOST=localhost
volumes:
- ./html:/usr/share/nginx/html
  • image: The Docker image to use.
  • ports: Maps host ports to container ports (like -p).
  • environment: Sets environment variables.
  • volumes: Mounts volumes (bind mounts or named volumes).

Building an Image from a Dockerfile

Instead of an existing image, you can build an image from a Dockerfile:
services:
app:
build: ./app
ports:
- "3000:3000"
build points to a directory containing a Dockerfile.

Networks and Volumes in Compose

Define custom networks and volumes at the bottom:
services:
db:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend

volumes:
db-data:

networks:
backend:

Versioning

Compose files usually start with a version (e.g., version: '3.8'). However, recent Docker versions support the simplified syntax without a version tag (the default uses the latest features). We'll use the modern style without explicit version.


Two Minute Drill
  • The docker-compose.yml file defines services, networks, and volumes.
  • Each service can specify image, build, ports, environment, volumes, etc.
  • Use named volumes and networks to share resources between services.
  • Compose files are YAML; indentation matters.

Need more clarification?

Drop us an email at career@quipoinfotech.com