The most important Docker Compose properties
Do you want to have the most important properties of docker-compose in one place? Here you get a short overview of the properties and additionally a simple cheat sheet that you can use!
In this post, I will explain the different sections and properties of the cheat sheet that you can download here!
This shows the basic structure of every docker-compose file. With the basic settings that can be a docker-compose.yml or a docker-compose.override.yml.
services: container1: properties: values container2: properties: values networks: network: volumes: volume:
Each compose property has one or multiple possible types. The different possibilities are a basic key:value, an array, or a dictionary.
key: - value - value
master: key: value key: value
There are a few possible properties that you can use in a container. The most important ones are listed here with a short description, the type, and an example of how to use it.
Build an image with the specified name by using the Dockerfile in the specified directory.
container: build: ./path image: image-name
Use an image that is on the system or that can be downloaded from the docker hub.
Specify the container name, that the container will be accessible with.
Create container volumes to persist data and to make certain directories available on the host system.
volumes: - /path/to/host-dir:/path/to/container-dir
Overwrite the start command of the container.
Define environment variables to use inside of the container.
environment: KEY: VALUE --- environment: - KEY=VALUE
Define environment variables inside a .env file to use inside of the container. The environment variables specified in the file will override the variables specified in the environment property.
env_file: .env --- env_file: - .env
Specify a restart rule for the container. (no, always, on-failure, unless-stopped)
Define the networks a container should be attached to.
networks: - network-name
Define ports to expose to other containers and the host system.
ports: - "9999:9999"
Define ports to expose only to other containers.
expose: - "9999"
Set the network driver for the container. (bridge, host, none, service:[service name], container:[container name/id])
Specify the build, start and stop order of the container.
depends_on: - container-name
Define the command, to make the container do nothing! The container will not stop and it is possible to enter it without problems. Mostly only needed for debugging.
command: tail -f /dev/null
Create named volumes that can be used in the volumes property. More information.
services: container: image: image-name volumes: - data-volume:/path/to/dir volumes: data-volume:
Create new networks that can be used in the network’s property. It will specify the name and the network driver. More information.
networks: frontend: driver: bridge
I hope you can make use of this collection of the most important docker-compose properties. If you think I missed something, you have some questions, or just feedback comment below or send me an email at firstname.lastname@example.org!