Open In App

What does Flag do in Docker Compose?

Last Updated : 26 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Docker Compose is a tool that is mostly used to create and execute multi-container apps. It is the secret to opening the door to a more simplified and effective deployment and development process.

What is Docker Compose?

Docker Compose makes it simple to manage services, networks, and volumes in a single, understandable YAML configuration file, streamlining management over your whole application stack. Then, you can create all of the services from your configuration file with a single command. With the Compose command-line tool, you may construct and start one or more containers for each dependency with a single command (docker-compose up).

Use Cases of Docker Compose

  • Automated testing environments: Compose allows you to easily develop and delete isolated test environments for your test suite. By defining the entire environment in a Compose file, you can construct and destroy these environments in a few commands.
  • Single-host deployments: Compose typically focuses on development and testing procedures, but with each release, we make progress toward more production-oriented functionality.
  • Development environments: The Compose file allows you to document and configure all of the application's service dependencies (e.g., databases, queues, caches, web service APIs).

Basic Docker Compose Commands

docker-compose up

The complete application specified in docker-compose.yml is launched and executed.

$ docker-compose up

Output:

$ docker-compose up

docker-compose restart

It can restart the running containers for the Docker.

$ docker-compose restart

Output:

docker-compose restart

docker-compose logs

It mainly views output from the containers.

$ docker-compose logs

Output:

docker-compose logs

docker-compose start

It launches the service's already-started containers.

$ docker-compose start

Output:

docker-compose start

Overview of Docker Compose Flags

--force-recreate

It recreates containers even when their image and settings haven't changed.

$ docker-compose up --force-recreate

Output:

--force-recreate

-f, --file

It gives the location of a backup compose file.

$ docker-compose -f docker-compose.custom.yml up

Output:

-f, --file

-p, --project-name

It can decide on a project name.

$ docker-compose -p myproject up

Output:

-p, --project-name

-d, --detach

In this command, containers are run in the background.

$ docker-compose up -d

Output:

-d, --detach

Examples of Using Flags with Docker Compose

Use '-f' or --file

First, The Compose file to be used is specified by the -f or --file parameter. By default, the current directory is where Docker Compose searches for a docker-compose.yml file. On the other hand, you can provide a different file using this option.

$ docker-compose -f docker-compose.prod.yml up

Output:

Use '-f' or --file
Output

Specifying multiple Compose files

You can specify several -f configuration files. When you provide numerous files, Compose will integrate them into a single configuration. Compose creates the configuration in the sequence in which you specify the files. Subsequent files modify and extend their predecessors.

$ docker compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db

Output:

Specifying multiple Compose files
Output

Specify a path to a single Compose file

You can use the -f flag to indicate a path to a Compose file that is not in the current directory, either from the command line or by configuring a COMPOSE_FILE environment variable in your shell or an environment file. Assume you're running the Compose Rails sample and there's a compose.yaml file in the sandbox/rails directory.

$ docker compose -f ~/sandbox/rails/compose.yaml pull db

Output:

Specify a path to a single Compose file
Output

Dry Run mode to test your command

The --dry-run flag allows to testing of a command without affecting the state of the application stack. Dry Run mode can display all the steps. Compose is used while running a command. For example:

$  docker compose --dry-run up --build -d

Output:

Dry Run mode to test your command
Output

Specifying --force-recreate

Containers must be recreated when the --force-recreate switch is present, even if their settings and image remain unchanged.

$ docker-compose up --force-recreate

Output:

Specifying  --force-recreate
Output

Use -p to specify a project name

At last, Every configuration has a project name. Compose determines the project name using the following techniques, in order of precedence.

$  docker compose -p my_project ps -a

Output:

Use -p to specify a project name
Output

Advanced Usage of Docker Compose Flags

--build

Before launching the containers, this flag requires a rebuild of the images.

$ docker-compose up --build

Output:

--build

--abort-on-container-exit

If a container is stopped, this flag stops all of the containers. Beneficial for one-time jobs.

$ docker-compose up --abort-on-container-exit

Output:

--abort-on-container-exit

--scale

You can scale the number of instances for a service with this flag. The number of replicas specified in your Compose file is overridden by it.

$ docker-compose up --scale web=3

Output:

--scale

Conclusion

In this article, we have learned about flags in Docker Compose. The -f flag is optional and if you do not specify this flag on the command line, Compose searches the working directory and its parent directories for a compose. yaml or docker-compose.yaml file.


Next Article
Article Tags :

Similar Reads