Docker Compose is an essential tool for defining and running multi-container Docker applications. It replaces long, tedious docker run commands with a single, declarative YAML configuration file, making your development and testing workflows significantly simpler and more repeatable.
docker-compose.yaml File: Core ConceptsThe entire application stack—services, networks, and volumes—is defined in a single file, typically named docker-compose.yaml. The structure is hierarchical, with the main configurations defined at the top level of the file.
These are the primary keys you define at the root of your docker-compose.yaml file. They define the overall environment for your application stack.
version (Deprecated but common)
version: "3.8") in legacy examples. It is safe to omit.services
web, db, api).web service can reach the db service at http://db:5432).networks
driver: Specifies which network driver to use (e.g., bridge, overlay). bridge is the default and most common for single-host development.external: true: Allows you to use a pre-existing network instead of having Compose create a new one. The network must be created manually first with docker network create.volumes
driver: Specifies a volume driver. Typically left to Docker's default.external: true: Similar to networks, this allows you to use a volume that was created outside of this Compose file.configs & secrets
configs) and sensitive data like passwords or API keys (secrets). The data is managed by Docker and mounted into the container as files.These are the keys you use inside a service definition (e.g., under services.backend).
image vs build
image: <name>:<tag>: Pulls a pre-built image from a registry (like Docker Hub). Use this for standard software like postgres:14-alpine or redis:latest.build: Builds an image from a Dockerfile. Use this for your own application code.
context: <path>: The build context—the path to the directory containing the Dockerfile and source code.dockerfile: <filename>: The name of the Dockerfile if it's not the default Dockerfile.args: Allows you to pass build-time variables to your Dockerfile.container_name: Sets a specific, static name for the container. If not set, Compose generates a name like project_service_1.ports: Exposes container ports to the host machine. Format is a list of strings: "HOST_PORT:CONTAINER_PORT".environment: Sets environment variables inside the container. Can be a list of KEY=VALUE strings or a dictionary.volumes: Mounts host paths or named volumes into the container.
my-data-volume:/var/lib/mysql./local/path:/app/codenetworks: Connects the service to one or more networks defined in the top-level networks section.depends_on: Controls the startup order of services. For example, if backend depends_on db, Compose starts db and waits for it to be running before starting backend. Note: This only waits for the container to start, not for the application inside to be ready.command: Overrides the default command (CMD) in the Dockerfile. It can be a string or a list of strings.