How to use the command 'docker compose' (with examples)

How to use the command 'docker compose' (with examples)

The docker compose command is used to run and manage multi-container Docker applications. It allows you to define and manage all the services required for your application in a docker-compose.yml file and then start, stop, and manage these containers using simple commands.

Use case 1: List all running containers

Code:

docker compose ps

Motivation:

By using this command, you can quickly see a list of all the running containers in your multi-container application. This can be helpful for troubleshooting and monitoring purposes.

Explanation:

The docker compose ps command lists all the running containers in your multi-container application.

Example Output:

       Name                    Command               State     Ports
-------------------------------------------------------------------------
wordpress_db       docker-entrypoint.sh --def ...   Up       3306/tcp
wordpress_wordpress_1   docker-entrypoint.sh apach ...   Up       0.0.0.0:8080->80/tcp

Use case 2: Create and start all containers in the background using a docker-compose.yml file from the current directory

Code:

docker compose up --detach

Motivation:

This command allows you to quickly start all the containers defined in your docker-compose.yml file in the background. This is useful when you want to start your application without having to monitor the log output.

Explanation:

The docker compose up command starts all the containers defined in your docker-compose.yml file. The --detach flag is used to run the containers in the background.

Example Output:

Starting wordpress_db      ... done
Starting wordpress_wordpress_1 ... done

Use case 3: Start all containers, rebuild if necessary

Code:

docker compose up --build

Motivation:

This command is useful when you have made changes to your application’s code or configuration and want to rebuild the containers before starting them.

Explanation:

The docker compose up command starts all the containers defined in your docker-compose.yml file. The --build flag is used to rebuild the containers if necessary.

Example Output:

Building wordpress
Step 1/6 : FROM wordpress:latest
...
Successfully built abcd1234
Successfully tagged wordpress_wordpress:latest
Creating wordpress_db      ... done
Creating wordpress_wordpress_1 ... done

Use case 4: Start all containers by specifying a project name and using an alternate compose file

Code:

docker compose -p project_name --file path/to/file up

Motivation:

This command is useful when you have multiple projects with their own docker-compose.yml files and want to start a specific project by specifying its project name and the path to its compose file.

Explanation:

The -p flag is used to specify the project name for your containers. The --file flag is used to specify the path to the alternate compose file.

Example Output:

Starting project_name_db      ... done
Starting project_name_app     ... done

Use case 5: Stop all running containers

Code:

docker compose stop

Motivation:

This command allows you to quickly stop all running containers in your multi-container application.

Explanation:

The docker compose stop command stops all running containers in your multi-container application.

Example Output:

Stopping wordpress_db      ... done
Stopping wordpress_wordpress_1 ... done

Use case 6: Stop and remove all containers, networks, images, and volumes

Code:

docker compose down --rmi all --volumes

Motivation:

This command is useful when you want to completely remove all the containers, networks, images, and volumes associated with your multi-container application.

Explanation:

The docker compose down command stops and removes all the containers, networks, images, and volumes associated with your multi-container application. The --rmi all flag removes all the images, and the --volumes flag removes all the volumes.

Example Output:

Stopping wordpress_db      ... done
Stopping wordpress_wordpress_1 ... done
Removing wordpress_db      ... done
Removing wordpress_wordpress_1 ... done
Removing network wordpress_default

Use case 7: Follow logs for all containers

Code:

docker compose logs --follow

Motivation:

This command allows you to monitor the log output of all the containers in your multi-container application in real-time.

Explanation:

The docker compose logs command displays the log output of all the containers in your multi-container application. The --follow flag is used to continuously display the log output as it is being generated.

Example Output:

wordpress_wordpress_1  | [Thu Oct 28 11:36:00.698902 2021] [mpm_event:notice] [pid 63:tid 140295530136384] AH00489: Apache/2.4.38 (Unix) OpenSSL/1.1.1c configured -- resuming normal operations
wordpress_wordpress_1  | [Thu Oct 28 11:36:00.698972 2021] [core:notice] [pid 63:tid 140295530136384] AH00094: Command line: 'httpd -D FOREGROUND'

Use case 8: Follow logs for a specific container

Code:

docker compose logs --follow container_name

Motivation:

This command allows you to monitor the log output of a specific container in your multi-container application in real-time.

Explanation:

The docker compose logs command displays the log output of a specific container in your multi-container application. The --follow flag is used to continuously display the log output as it is being generated. Specify the container name after the --follow flag to choose the specific container.

Example Output:

[Thu Oct 28 11:36:00.698902 2021] [mpm_event:notice] [pid 63:tid 140295530136384] AH00489: Apache/2.4.38 (Unix) OpenSSL/1.1.1c configured -- resuming normal operations
[Thu Oct 28 11:36:00.698972 2021] [core:notice] [pid 63:tid 140295530136384] AH00094: Command line: 'httpd -D FOREGROUND'

Conclusion:

The docker compose command is a powerful tool for managing multi-container Docker applications. With the ability to start, stop, rebuild, and monitor multiple containers with a single command, it simplifies the management of complex applications. Understanding the various use cases and examples provided in this article will help you effectively utilize the docker compose command in your Docker workflows.

Related Posts

# How to use the command 'git for-each-repo' (with examples)

# How to use the command 'git for-each-repo' (with examples)

Git for-each-repo is an experimental command that allows you to run a Git command on a list of repositories.

Read More
How to use the command `fsck` (with examples)

How to use the command `fsck` (with examples)

The fsck command is used to check the integrity of a filesystem and, if needed, repair it.

Read More
Create a Laravel application using lambo new (with examples)

Create a Laravel application using lambo new (with examples)

1: Create a new Laravel application To create a new Laravel application using the lambo new command, you can simply run the following command:

Read More