Equivalent of Docker option --config in docker-compose


Overview

If you are using Docker Compose to define and run multi-container Docker applications, you may want to specify custom configuration files or directories for your application. Fortunately, Docker Compose provides the --config option, which allows you to do just that.

The --config option is used to specify a single configuration file or a directory containing multiple configuration files for Docker Compose. To use it, you can pass it as an argument to the docker-compose command. For example −

$ docker-compose --config /path/to/custom/config.yml up 

This command would tell Docker Compose to use the configuration file located at /path/to/custom/config.yml as the configuration for the application.

Alternatively, you can specify a directory containing multiple configuration files using the --config option −

$ docker-compose --config /path/to/custom/config/dir up 

In this case, Docker Compose will look for a file named docker-compose.yml in the specified directory and use it as the configuration file.

Config Vs File option

It's worth noting that the --config option is similar to the --file option in Docker Compose, which is used to specify specific configuration files, regardless of their location or name. However, --config is used to specify a single configuration file or a directory containing multiple configuration files, while --file is used to specify specific configuration files, regardless of their location or name.

Example of --file option −

$ docker-compose --file file1.yml --file file2.yml up

Use Cases

Using the --config option can be a useful way to specify custom configuration files or directories for your Docker Compose application. It can be particularly useful if you want to test out different configurations without modifying the default configuration files or if you want to use a different configuration file or directory on different systems.

To give you an idea of how the --config option can be used in practice, here are a couple of examples −

# Use a custom configuration file 
docker-compose --config /path/to/custom/config.yml up 
# Use a directory containing multiple configuration files 
docker-compose --config /path/to/custom/config/dir up 

In both cases, Docker Compose will use the specified configuration file or directory as the configuration for the application.

It's worth noting that the --config option is only used when starting the Docker Compose application and cannot be used with other Docker Compose commands. If you want to specify custom configuration options for other Docker Compose commands, you will need to use other options or environment variables.

Implementation

Here are two examples of Docker Compose configuration files, docker-compose.yml and docker-compose-test.yml, that can be used with the --config option −

docker-compose.yml

version: '3.9'
services:
   web1:
      image: nginx
      ports:
      - 81:80
   db1:
      image: postgres
      environment:
         POSTGRES_USER: user
         POSTGRES_PASSWORD: password

docker-compose-test.yml

version: '3.9'
services:
   web2:
      image: nginx
      ports:
      - 82:80
   db2:
      image: mysql
      environment:
         MYSQL_USER: testuser
         MYSQL_PASSWORD: testpassword

To start the services defined in these configuration files, you can use the --config option like this −

# Start services using docker-compose.yml
docker-compose --config docker-compose.yml up

# Start services using docker-compose-test.yml
docker-compose --config docker-compose-test.yml up

In the first case, Docker Compose will use the docker-compose.yml file to start the web1 and db1 services using the Nginx and PostgreSQL images, respectively. In the second case, Docker Compose will use the docker-compose-test.yml file to start the web2 and db2 services using the Nginx and MySQL images, respectively.

Conclusion

In conclusion, the --config option is useful for specifying custom configuration files or directories for your Docker Compose application. It can be used to test out different configurations or a different configuration file or directory on different systems. By using the --config option, you can have more control over how your Docker Compose application is configured and make it easier to run your application consistently on different systems.

Updated on: 30-Jan-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements