Docker Compose Cheat Sheet
Docker Compose is a tool for defining and running multi-container Docker applications. It uses a configuration file to provide all the necessary details about the services, networks, and volumes. Let's dive into the essential aspects of Docker Compose for programmers.
Building images is an essential step in the Docker Compose workflow. You can define the build context, Dockerfile path, and other parameters in the
docker-compose.yml file for each service.
This defines a service named
webapp that uses the build context from
./path_to_code and a Dockerfile named
Once images are built, you can run them as containers using Docker Compose. The image to run can be specified in the
webapp service runs the image
my_custom_image with the
Docker Compose comes with a suite of commands to manage the lifecycle of your containers.
This command starts all the services defined in your
When run, it reads the
docker-compose.yml file and starts the services accordingly. You can also use
-d flag to run services in detached mode.
Stops and removes all the services that are running.
This command stops the services and also removes the containers, networks, and volumes defined in the
Displays the logs of your services.
docker-compose logs webapp
In this case, it will show the logs for the service named
Builds the service images.
docker-compose build webapp
It builds the
webapp service using the specified build context and Dockerfile in the
List the services.
This command provides an overview of the current state of the services.
docker-compose.yml file is a YAML file that defines the services, networks, and volumes for your application.
Services are the heart of your application, representing the containers that will run.
This defines a service
webapp that runs the image
my_custom_image:latest and maps port 5000 inside the container to port 5000 on the host.
Networks facilitate communication between containers.
This configuration sets up a custom network named
my_custom_network and associates the
webapp service with this network.
There are multiple keys in the
docker-compose.yml file that dictate the behavior of services, networks, and volumes.
For services, some common keys include:
image: The Docker image to use for the service.
build: Instructions to build the Docker image.
ports: Ports mapping between the host and the container.
volumes: Mount paths for data persistence.
Volumes are used for data persistence and sharing data among containers.
Here, a volume named
my_data is defined and mounted to
/app/data in the
webapp service container.
Secrets are used to manage sensitive data.
In this configuration, a secret named
my_secret is defined, and its value is taken from the file at
webapp service can then access this secret.
With this cheat sheet, you now have a comprehensive overview of the core components and functionalities of Docker Compose. Happy containerizing!