The docker-compose.yml File: Your App's Blueprint
The `docker-compose.yml` file is a blueprint for defining and running multi-container Docker applications. Use it to spin up a local dev environment with a database, backend, and frontend with one command.
WHY IT EXISTS: Manually running multiple Docker containers with individual docker run commands is tedious and error-prone. You have to manage networks, link containers, and remember complex port mapping and volume flags, which doesn't scale as an application grows.
THE MENTAL MODEL: docker-compose.yml is a declarative blueprint for your application's architecture. You describe the desired end state—which services to run, what images they use, and how they connect—and Docker Compose handles the imperative steps to make it happen. It's like giving an architect a blueprint instead of telling a construction crew where to lay each brick.
HOW IT WORKS: The file is written in YAML. The main top-level key is services, under which you define each container your application needs (e.g., web, db, cache). For each service, you specify the image to use, ports to expose, environment variables, and volumes for persistent data. When you run docker-compose up, Compose reads this file, creates a shared network for all services to communicate, and starts the containers in the correct dependency order.
WHEN TO USE IT: It is the standard for setting up local development environments. It allows any developer to get a full, complex application stack running with a single command. It is also frequently used for running integration tests in a CI/CD pipeline, where a temporary environment mirroring production is needed.
WHEN NOT TO USE IT: Avoid using Docker Compose alone for production deployments. It lacks the high-availability, fault tolerance, load balancing, and auto-scaling capabilities of a true container orchestrator. For production, you should use tools like Kubernetes, Docker Swarm, or a cloud provider's service like Amazon ECS.
ONE CANONICAL EXAMPLE: A typical web app setup includes a webapp service and a db service. The webapp service builds from a local Dockerfile, exposes a port (e.g., 8080:80), and uses depends_on to ensure the database starts first. The db service uses a standard postgres image, sets the password via an environment variable, and mounts a named volume to ensure the database's data persists across restarts.
Read the original → docs.docker.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.