0
0
DockerHow-ToBeginner · 3 min read

How to Create docker-compose.yml for Easy Docker Setup

To create a docker-compose.yml file, write a YAML file that defines your services, networks, and volumes. Use the version key to specify the Compose file format, then list each service with its image, ports, and other settings. Save this file as docker-compose.yml in your project folder to run with docker-compose up.
📐

Syntax

The docker-compose.yml file uses YAML format to define your Docker app setup. It starts with a version key to specify the Compose file format version. Then, under services, you list each container with its configuration like image, ports, and volumes. You can also define networks and volumes globally.

yaml
version: '3.9'
services:
  service_name:
    image: image_name:tag
    ports:
      - "host_port:container_port"
    volumes:
      - host_path:container_path
networks:
  network_name:
    driver: bridge
volumes:
  volume_name: {}
💻

Example

This example shows a simple docker-compose.yml that runs a web server using the official Nginx image. It maps port 8080 on your computer to port 80 inside the container.

yaml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
Output
Creating network "default" with the default driver Creating web_1 ... done Starting web_1 ... done Attaching to web_1 web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
⚠️

Common Pitfalls

Common mistakes when creating docker-compose.yml include incorrect indentation, missing quotes around port mappings, and using unsupported Compose file versions. YAML is sensitive to spaces, so always use consistent indentation (usually 2 spaces). Also, forgetting to map ports correctly can cause your app to be unreachable.

yaml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - 8080:80  # Wrong: missing quotes

# Correct way:
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
📊

Quick Reference

  • version: Compose file format version (e.g., '3.9')
  • services: Define containers with image, ports, volumes, etc.
  • ports: Map host to container ports as strings, e.g., "8080:80"
  • volumes: Mount host folders or named volumes
  • networks: Define custom networks if needed

Key Takeaways

Always start your docker-compose.yml with a version key to specify the file format.
Define each service with its image and necessary settings like ports and volumes.
Use quotes around port mappings to avoid YAML parsing errors.
Indentation matters: use consistent spaces (usually 2) for YAML structure.
Test your docker-compose.yml by running docker-compose up to ensure it works.