Building Images with Docker Compose
Docker Compose builds images from a `compose.yml` file, turning a Dockerfile into a runnable service within a multi-container app. It's for local dev where you need a database and backend to start together. The footgun: `up` won't rebuild without `--build`.
WHY IT EXISTS: Manually building, tagging, and running multiple containers for an application is tedious and error-prone. Remembering all the docker build and docker run flags, network settings, and volume mounts for each service makes development inconsistent across a team. Docker Compose was created to solve this by defining the entire multi-container application in a single, declarative YAML file.
THE MENTAL MODEL: Think of compose.yml as the complete recipe for your application stack. Instead of being a line cook who builds one image, runs one container, then figures out how to link it to the next, you act as the executive chef. You write down all the services (database, API, frontend), their build instructions (which Dockerfile to use), and how they connect. Then, the command docker-compose up executes the entire recipe at once.
HOW IT WORKS: Inside your compose.yml file, you define a set of services. For any service that requires a custom image, you replace the image: key with a build: key. This build: key points to the directory containing your Dockerfile. When you run docker-compose build or docker-compose up --build, Compose locates each service with a build: instruction. It then runs the equivalent of docker build on the specified directory, automatically tagging the resulting image with a predictable name (e.g., myproject_api). Finally, it uses these freshly built images to create and start your service containers.
WHEN TO USE IT: Docker Compose is the standard for local development environments. It's perfect for spinning up a complete application stack—like a web server, database, and caching layer—with a single command. It's also widely used in CI/CD pipelines for running integration tests against a realistic, multi-service environment.
WHEN NOT TO USE IT: Compose is not a production-grade container orchestrator for multi-machine clusters. It lacks features for high availability, scaling across nodes, and self-healing that tools like Kubernetes or Docker Swarm provide. While you can use it for simple single-host deployments, it is not designed for managing complex, distributed systems in production.
ONE CANONICAL EXAMPLE: A common compose.yml file might define two services. A webapp service would use build: ./webapp to instruct Compose to build an image from the Dockerfile located in the ./webapp directory. A second database service might use image: postgres:13 to pull a pre-built image from Docker Hub. Running docker-compose up --build would first build the webapp image and then start both the webapp and database containers, connecting them on a shared network.
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.