To use docker compose, you need to install it on your system. You can follow this installation guide to install and setup docker compose. You also need to have Docker Engine installed and running.
The docker compose need to use a YAML format that describes your services, networks, volumes, and other configurations. You can name your file
docker-compose.yaml and place it in the root directory of your project. You can also use any other name you wish.
docker compose Vs docker-compose Commands
docker compose command (with space) is a new command which is introduced along with the docker project docker/compose repo as a plugin. To install the v2 docker compose as a CLI plugin on Linux, you can use the below command. Make sure you have docker engine installed and setup already.
sudo apt install docker-compose-plugin
Now you can use the command
docker compose up. This will create and start your services in the background. You can use the command
docker compose down to stop and remove your services.
Specific Docker compose File
Sometimes, you may want to use a specific or more than one docker compose file for different purposes. For example, you may have a base file that defines your common services, and another file that adds some additional configurations for development or testing.
To specify the name and path of one or more compose files, you can use the
-f flag with the
docker compose subcommand. For example, if you have a file named
docker-compose.admin.yml that adds an admin service to your application, you can use this command:
docker compose -f docker-compose.yml -f docker-compose.admin.yml up
When you supply multiple files, docker compose combines them into a single configuration. Subsequent files override and add to their predecessors. For example, if you have another file named
docker-compose.override.yml that changes some settings of your web service, such as the image tag or the port mapping, you can use this command:
docker compose -f docker-compose.yml -f docker-compose.override.yml up
This will use both files as input and create all services mentioned in them.
You can also specify a project name for your configuration with the
-p flag. This will help you distinguish between different instances of your application. For example, if you want to run two versions of your application on the same machine, one for production and one for staging, you can use these commands:
docker compose -p prod up # for production docker compose -p stage up # for staging
This will create two sets of services, each with a different prefix. You can use the command
docker compose ps to see the status of your services.
Extend Docker Compose file
Another way to use specific docker compose files is to extend them by referring to another compose file and selecting the bits you want to use in your own application. This is useful for reusing common configurations or inheriting from a base template.
To extend a compose file, you need to use the
extends keyword in your service definition. You also need to specify the
file and the
service that you want to extend from. Here is an example of a docker compose file that extends from another file named
version: '3' services: web: extends: file: common.yml service: web ports: - "80:80" db: extends: file: common.yml service: db environment: - MYSQL_PASSWORD=secret
This file tells docker compose to use the service definitions from the
common.yml file, and add some additional configurations for the web and db services. The
common.yml file may look something like this:
version: '3' services: web: image: nginx:alpine db: image: mysql:latest
Merge Docker Compose Files
You can also merge a set of compose files together to create a composite compose file. This is useful for combining different aspects of your application into a single file. To merge a set of compose files, you can use the command
docker compose config.
For example, if you have three files named
test.yml, you can use this command:
docker compose -f base.yml -f dev.yml -f test.yml config > composite.yml
This will merge the three files into one and output the result to a file named
composite.yml. You can then use this file as input for your docker compose commands.
You can also include other compose files directly in your compose file with the
include keyword. This is useful for modularizing your configuration and avoiding duplication. To include another compose file, you need to specify the
file that you want to include. Here is an example of a docker compose file that includes another file named
version: '3' include: - file: networks.yml services: web: image: nginx:alpine networks: - frontend db: image: postgres:13-alpine networks: - backend
This file tells docker compose to include the network definitions from the
networks.yml file, and assign them to the web and db services. The
networks.yml file may look something like this:
version: '3' networks: frontend: driver: bridge backend: driver: bridge
This file defines two networks named frontend and backend to your main file.
I hope this article helped you understand how to use specific docker compose files. If you want to learn more about docker compose, you can check out our tutorials related to docker and docker compose. You can also try out some sample applications that use docker compose to see how it works in practice.