Docker Compose has two parts:
- YAML file which describes the configuration of the containers
- CLI tool which is used for local testing/ automation using the YAML file
Compose is not meant for production usage its meant for quick local environment development
An new network interface is always created for containers created from a config file
Compose File Structure
The docker-compose file has 3 main parts:
- Services
- Volumes (Optional)
- Networks (Optional)
The first line in the file always needs to be the version of compose to use
It is recommended to use version 2 at the very least
If this line is not specified it defaults to version 1
Inside services we can specify multiple “servicename”
Each service name refers to a container
The service name need not be the same as the container name but it will be the name used as hostname on the network
Inside a compose file we can build custom containers as well
It will be build when docker compose up
is run and the image does not exist in cache
docker compose build
can also be used to build images
Whenever compose creates any asset (image, volume, network, etc.) it will prepend the current working directory name to the name that we specify
If we have files called docker-compose.yaml
and docker-compose.override.yaml
in the folder when we run docker compose up
the override file will overwrite conflicting options from the base compose file
If we want to specify override files with using the default names it can be specified in the docker compose up
command using the -f
flag
The first file specified needs to be the base file
Options
-f
: Alternate Compose File
-p
: Alternate Project Name (Default : Directory name)