Computer scienceSystem administration and DevOpsDockerDocker Compose

Docker compose volumes, configs, secrets

9 minutes read

Docker Compose is a magical tool that allows you to define and manage multi-container applications. It provides a way to define services, networks, and volumes in a single YAML file, making it easier to orchestrate and deploy complex applications with multiple components.

Volumes

Volumes in Docker Compose create persistent data and share them across containers or between containers and the host machine. A volume is a directory or a file in the host machine's filesystem that you can mount into a container at a specific path. By using volumes, you can separate data from the container itself, making it easier to manage and update your application without losing important data.

Think of volumes as backpacks that containers carry with them. These backpacks allow containers to store and retrieve important items, even if they are moved or replaced. Let's look at an example of volumes in Docker Compose.

version: "3"
services:
  app:
    image: myapp
    volumes:
      - ./data:/app/data

In this example, a service called app uses the myapp image. The example defines a mount volume by specifying the host machine's path and the path inside the container where the volume should be mounted. In this case, the host machine's path is ./data and the path for container volume is /app/data.

With this configuration, the ./data directory on the host machine will be mounted into the /app/data directory inside the "app" container. Any files or changes made in the /app/data directory inside the container are reflected in the ./data directory on the host machine and vice versa.

You can create and manage data storage that stays even when containers are restarted or replaced. This is handy for sharing read-only data among containers or setting up a container with vital configuration files stored in a volume. The following snippet shows how you can attach one volume to two different containers:

version: "3"
services:
  app1:
    image: nginx
    volumes:
      - shared_volume:/app/data
  app2:
    image: busybox
    command: ["tail", "-f", "/dev/null"]
    volumes:
      - shared_volume:/app/data

volumes:
  shared_volume:
    driver: local
    driver_opts:
      type: nfs
      o: addr=192.168.1.10,nolock,soft,rw
      device: ":/path/to/shared_folder"

In the above example, two services named "app1" and "app2" are sharing the same volume — shared_volume.

When you apply the same volume to different containers, you ensure smooth data access and effective communication between components. Docker Compose streamlines data management by allowing you to specify data-sharing paths. This enhances application upkeep while minimizing the risk of data loss.

Volume drivers and driver_opts in Docker Compose provide flexibility in managing volumes. A driver determines data storage and access with options for different systems. driver_opts allows customization to optimize volume management for your app's needs and storage setup.

If you don't provide driver_opts for a volume, docker implements the default options which depend on the chosen driver. For the "local" driver, which utilizes the host's filesystem, the default settings are basic and suitable for local storage. These defaults work well for local storage but might differ for remote storage.

Configs

Configs in Docker Compose allow you to provide configuration files to your services. They act as user manuals that guide containers on how to operate correctly. Configs are especially useful when you need to parameterize your services or share common configurations across multiple instances.

To understand configs, let's imagine a cooking class. Each student (container) receives a user manual (config) that contains detailed instructions on how to prepare a specific dish. The manual ensures consistency across all students' cooking processes and allows them to customize certain aspects, like spiciness or the choice of ingredients. The following example shows how you can use configs in your container:

version: "3"
services:
  app:
    image: myapp
    configs:
      - app_config

configs:
  app_config:
    file: ./config.ini

The example defines the app service using the myapp image. It uses a config named app_config to provide configuration settings to the myapp application. The config.ini file stores the actual configuration data and the file itself is located in the same directory as the Docker Compose file. You can easily manage and customize the application's settings without modifying the container.

Configs are ideal for customizing services or sharing settings across instances. Using configs in the Compose file helps you manage app settings without altering containers, boosting flexibility and upkeep.

The configs key enables defining configurations that a developer can link to other services. This streamlines adjusting settings across different parts of your application.

Secrets

Secrets enable secure management of sensitive data, like passwords and API keys within Compose files. They keep sensitive information separate from the main file. This enhances security and eases sensitive data management across diverse environments. Unlike regular environment variables, secrets offer a hidden vault for containers to access when required. Moreover, secrets do not show up in logs to reduce the risk of exposing confidential information during debugging or troubleshooting. Additionally, you can link secrets with environment variables to add another layer of security to your application's configuration.

To better understand secrets, imagine a team of spies (containers) on a mission. Each spy carries a secret document (secret) containing confidential information. The document is carefully encrypted and stored within a hidden vault. When needed, the spies retrieve the document, decrypt it, and utilize the information to accomplish their objectives without exposing it to potential adversaries.

version: "3.1"
services:
  db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db_root_password
      - MYSQL_USER=myuser
      - DB_USER_PASSWORD_FILE=/run/secrets/db_user_password
    secrets:
      - db_root_password
      - db_user_password

secrets:
  db_root_password:
    file: ./db_root_password.txt
  db_user_password:
    environment: "DB_USER_PASSWORD"

This Docker Compose code establishes a service named db running a MySQL database utilizing the mysql image. The root password for the database is securely handled using Docker Compose secrets. This password is stored within the external file "db_root_password.txt." In the secrets section, the secret named "db_root_password" is linked to the service. This setup permits the application to access sensitive data, all the while maintaining separation from the Compose file.

This strategy bolsters security and adaptability and also guarantees the safeguarding of the root password. Furthermore, it enables seamless updates and management across various environments.

Conclusion

To sum up, here are a few key points to remember:

  • Volumes in Docker Compose provide data persistence and sharing between containers and the host machine.
  • Volumes separate data from the container, making it easier to manage and update applications without data loss.
  • Volumes enable data sharing between containers and host, promoting easy updates.
  • Configs in Docker Compose act as user manuals that guide containers on how to operate correctly.
  • Configs are useful when you want to parameterize services and share common configurations across instances.
  • Volume drivers and driver_opts customize volume management.
  • Secrets in Docker Compose securely manage sensitive data like passwords and certificates.
  • Secrets keep sensitive information externalized and encrypted, providing an extra layer of security.
17 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo