Docker Compose is a popular tool for managing multiple applications in a container. It allows you to create custom stacks, which are collections of applications that run together in the same container. You can use this tool to manage your application’s dependencies, schedule tasks, and more. One common use for stacks is to create a single application that does many different tasks. For example, you might want to create an application that prints text documents and then send them to a printer. You would also want to create an application that stores data in a database and then sends it over the network to another application. To make creating stacks easier, you can use Service Profiles. Service Profiles are files that contain descriptions of services that should be used by your applications. When you add a service profile to your Docker Compose file, Docker will start using the service from the profile instead of from the default services list. This makes it easier to manage your applications and makes it possible to run multiple services side-by-side without having to worry about which service is used when something goes wrong. To add a new service profile:
- Open up your docker-compose file and add the following line at the bottom:
- Save your file and exit out of docker-compose .
- Type into Terminal:
- Navigate to /etc/docker/compose/profiles/mynewprofile and paste in the following content:
- Save your file and exit out of Terminal .
- Type into another Terminal:
- Navigate to /usr/local/bin/docker-compose up -d && docker-compose down -d && docker-compose status -t && docker-comose up -p && docker-compose down -p
Docker Compose now supports profiles for selective use of services. Services in your docker-compose.yml can be linked to one or more named profiles. Passing a profile name to docker-compose up will start just the services in that profile, letting you create variants of your stack for specific environments and configurations.
Compose has previously focused on defining a single stack that’s a canonical distribution of your application. Profiles add more room for customizing which parts of the stack to use, making complex sets of services more modular and configurable.
Why Use Profiles?
Use of profiles is entirely optional. Your existing Docker Compose files will continue to function and there’s no need to adopt profiles straightaway.
Service profiles solve several common frustrations with Docker Compose development and testing flows. You might have services which you only want to use in development, such as a debug container or logging service. When you’re in production, you don’t need those services and want to avoid starting them.
Previously, achieving this required splitting your service definitions across multiple files. You’d then need an unwieldy up command to start everything in development:
Profiles let you combine both service definitions into one file. You can use a flag or environment variable to select a specific profile, without manually typing out file paths. This creates a more convenient experience that’s less hassle to document, write, and run.
Defining Profiles
Profiles are created by setting the profiles field on services in your docker-compose.yml. Profiles are specified as a list. Each service can be given one or many profiles.
Profile instances are created implicitly from the names given to your profiles fields. Services which share a profile are automatically joined.
To start the services included in a profile, add the –profile flag to docker-compose up:
This command would start both the app and debug services from the above Compose file. If you ran docker-compose up, omitting the –profile flag, only the app service would start.
You can start multiple profiles simultaneously by repeating the –profile flag. Compose also supports the COMPOSE_PROFILES environment variable as an alternative to –profile. This accepts a comma-separated list of profile names.
Services with no profiles field will always be started, irrespective of any requested profile. Once a service has been given a profile, it will only start if that profile has been requested. For services with multiple profiles, requesting any one of them will allow the service to start.
Implicit Profile Starts
Profiles will always be ignored if you manually start a service using docker-compose run. In this case, Compose will also start any services which the requested service depends upon, if they share a profile or have no profile assigned.
Here, running docker-compose run debug would start the debug-utils service, even though the dev profile hasn’t been explicitly selected:
Implicit starts only apply to direct dependents of the specified service. If debug-utils also had a depends_on, and that service didn’t share the dev profile, it would not start up correctly.
For dependency resolution to work properly with docker-compose run, all services in the tree must share a profile of the top-most service, or be permanently enabled. If neither of those conditions hold, you’ll need to add the –profile flag to explicitly activate any additional required profiles.
Summary
Service profiles are a convenient Compose feature that make it easier to manage different combinations of services. By using profiles, you can avoid splitting services into multiple Compose files. Adding –profile usually feels more natural than merging multiple YAML files together.
Profiles let you build out sub-stacks within your main Compose application. Their introduction as part of the Compose spec is a recognition that stacks in development often incorporate extra services beyond those used in production.
More generally, profiles make Compose more versatile by facilitating stack customization. While envisioned as an approach to environment management, profiles could also help the community create different variations of popular images. Think of a WordPress docker-compose.yml with mysql and mariadb profiles: now you can easily switch between pre-configured database services to select the engine that matches your preference.
Docker Compose 1.28 introduced profiles earlier this year. As long as you’ve got a recent version of the Compose binary, or Docker Desktop for Windows and Mac, you can add profiles to your Compose files to start selectively enabling services.