1. Overview

Docker Compose is a powerful tool for defining and running multi-container Docker applications. It allows developers to define their application’s services, networks, and volumes in a single YAML file, making it easy to deploy and manage complex applications. Container management is easier with Docker’s service profiles, which allow users to define containers’ configurations specifically.

Sometimes, we may need to run certain services based on some configurations from our Docker Compose file.

In this tutorial, we’ll learn about service profiles and their importance in Docker.

2. Understanding the Docker Compose Services

Before we use service profiles to run Docker services with specific configurations, let’s first understand the docker-compose services.

A Docker service is a logical grouping of containers that work together to provide a specific function. In Docker Compose, a service refers to a containerized application element that can be horizontally scaled. It allows multiple instances of the same service to run simultaneously.

In a docker-compose.yml file, we can define various properties of services, such as container image, network configuration, and environment variables.

To demonstrate, let’s create a docker-compose.yml file with web and db services:

version: "3.9"
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: secretpassword

Here, the web service uses the nginx image with port 8080 exposed from the host machine inside the container. Database services use the postgres image and set the POSTGRES_PASSWORD environment variable to a secretpassword.

Furthermore, we can use the docker-compose up command to run only certain services from our Docker Compose file. To demonstrate, let’s check out the command to run only the web service from the previous example:

$ docker-compose up web

Using the command above, only the web service and any services that it depends on will be started. The web service doesn’t depend on the database service, so that it won’t start. By separating with a space, we can specify more than one service. To illustrate, let’s run both the web and db service:

$ docker-compose up web db

This command will start with the web service, the database service, and any dependencies.

3. Service Profiles

Docker Compose files define specific configurations for containers that run in a Docker service based on service profiles. The service profile is an experimental feature introduced in Docker version 20.10. Service profiles allow us to define configurations for specific containers that run as part of a service. As a result, they simplify container management and also maintain the correct configurations.

Docker service profiles permit us to define specific configurations for containers inside a Docker service. Thus, it is easy to manage individual containers and manage the configurations dynamically. We can only use service profiles in Docker Compose files under the profile section. To demonstrate, let’s look at the docker-compose.yml with profiling of a service:

version: "3.9"
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: secretpassword  
    profiles:
      - dev   
  mysqldb:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: testrootpassword
      MYSQL_DATABASE: test
      MYSQL_USER: test
      MYSQL_PASSWORD: testpassword
    profiles:
      - prod
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

In Docker Compose, we’ve created three different services web, db, and mysqldb. The db service has the dev service profile, whereas mysqldb has the prod service profile. In our Docker Compose file, we can specify the service profile to use by passing the option –profile followed by the profile’s name. To illustrate, let’s check out the command to run the mysqldb service with the “prod” profile:

$ docker-compose --profile=prod up

In the above case, mysqldb and web services will start running. The mysqldb service will start due to the prod profile in the configuration. In addition, the web service will also run as services without profiles are always enabled.

4. Conclusion

In this article, we explored how to use service profiles in Docker. First, we learned the basic concept of using a service in a docker-compose file. After that, we introduced the service profiles with the docker-compose file.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.