TABLE OF CONTENTS

Managing Docker Compose with CLI

Docker Compose Commands

Docker Compose Commands

In the previous guide, we explored writing a Docker Compose file to define services, networks, and volumes for multi-container applications. Building on that foundation, this guide focuses on managing Docker Compose with CLI commands, allowing you to efficiently deploy, monitor, and scale applications. Whether you’re starting services, debugging configurations, or scaling containers, mastering these commands is key to streamlined container orchestration.

In this section, we’ll cover the following topics:

  • Managing Docker Compose with CLI
  • Summary of Docker Compose Commands

Managing Docker Compose with CLI

Docker Compose provides a powerful command-line interface (CLI) to manage multi-container applications efficiently. Below is a comprehensive guide to the most commonly used docker-compose commands, categorized by their functionality.

Building and Running Services

1. docker-compose build

Builds or rebuilds the services defined in the docker-compose.yml file.

Command Syntax:

docker-compose build [options]

Common Options:

  • --no-cache: Forces a fresh build without using cached layers.
  • --pull: Pulls the latest version of the base image.

Example:

docker-compose build --no-cache

Explanation:
Builds all services from the docker-compose.yml file, forcing a fresh rebuild without using cached layers.

2. docker-compose up

Starts containers for the services defined in the docker-compose.yml file, creating them if necessary.

Command Syntax:

docker-compose up [options]

Common Options:

  • -d: Runs containers in detached mode (background).
  • --build: Forces a build before starting the containers.

Example:

docker-compose up -d

Explanation:
Starts all services in the background and keeps them running.

Service and Container Management

3. docker-compose ps

Displays the status of the running containers in the project.

Command Syntax:

docker-compose ps

Common Options:

  • -a : Shows all containers, including stopped ones.

Example:

FAQ: Docker Compose Commands

What is the purpose of the `docker-compose build` command?

The `docker-compose build` command is used to build or rebuild services defined in the `docker-compose.yml` file, allowing you to create Docker images for your services.

How does the `docker-compose up` command function?

The `docker-compose up` command starts containers for the services defined in the `docker-compose.yml` file, creating them if necessary, and can run them in detached mode.

What does the `docker-compose ps` command do?

The `docker-compose ps` command displays the status of running containers in the project, showing details like container names, IDs, ports, and statuses.

How can you scale services using Docker Compose?

You can scale services using the `docker-compose up --scale` command, which allows you to specify the number of container instances for a service.

What is the function of the `docker-compose down` command?

The `docker-compose down` command stops all running services and removes containers, networks, and volumes associated with the project.