Autumn Sale - up to 36% OFF

Docker Compose Cheat Sheet: Key Commands Guide

Docker Compose Cheat Sheet: Key Commands Guide
Published on Sep 12, 2025 Updated on Sep 12, 2025

Docker Compose simplifies the process of developing and deploying multi-container applications by defining services inside a configuration file written in a human-readable language called YAML. Its streamlined workflow makes it the perfect choice for setting up and replicating entire application stacks in only a few steps.

#Why use Docker Compose

#Multi-container applications

Docker Compose can be used to ease the process of managing multiple Docker containers simultaneously. Taking into consideration the needs and dependencies of modern applications you can take advantage of Docker Compose and set up an entire application’s tech stack in a few minutes.

#Scalability

Performance and reliability are extremely important in real-world applications. With Docker Compose, you can effortlessly scale services to handle various workloads. You can balance the load by spinning multiple containers for a specific service in Docker Compose.

#Single-file environment

Docker Compose allows you to define your application’s stack in a single, human-readable file. You can replicate it on any machine you want with a single, simple command. Ideal for bundling with source code repositories, a single-file configuration is also easier to manage and encourages consistency across developer teams.

#Local development and testing

Version clashing is a common problem in local development. With Docker Compose, each service of your application stack runs in its container, and there is no need to worry about a local install mess. Due to the flexibility offered by the declaration of the environment in a single file, everyone on your team can easily spin up development or testing environments with one command.

Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.

#Prerequisites

You need Docker Compose installed on your Linux instance. Read our tutorial to install Docker Compose on Ubuntu.

#How to use Docker Compose

#Step 1: How to write a docker-compose.yaml file

#Docker Compose YAML file

The docker-compose.yaml file is used to define services, networks, and volumes required by your application stack in a human-readable language. Docker Compose then uses this YAML configuration file to build and start all the containers you have defined.

The following is a basic Docker Compose configuration file of a LAMP stack to run WordPress on Docker.

---
services:
  wordpress:
    image: wordpress
    container_name: wordpress
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpresspass
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress_data:/var/www/html
    depends_on:
      - db

  db:
    image: mariadb:latest
    container_name: mariadb
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpresspass
      MYSQL_ROOT_PASSWORD: rootpass
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:

Copy and paste the above code inside a text file and save it as docker-compose.yaml.

#Docker Compose service

Simply put, a service in Docker Compose represents a container. Each service can have parameters such as image, build, environment variables, and ports. The above example defines two services, respectively named wordpress and db.

#Docker Compose network

In complex modern applications, containers need to communicate with each other. A network in Docker Compose makes it possible for services to talk with each other. Docker Compose provides a default network for all services defined in your YAML configuration file. It also allows for customization and attachment of external networks.

#Docker Compose volume

Volumes in Docker Compose are used to persist data outside containers. They help to deploy stateful Docker containers, ensuring data persistence in case the container stops or restarts. At the moment you stop a container in Docker Compose, any data inside it is lost. This issue is solved by storing the data outside the container’s writable layer through volumes.

#Docker Compose port

A port in Docker Compose is used to expose a service listening inside the container to your localhost. In the above Docker Compose configuration file, we forward traffic from localhost:8000 to port 80 inside the WordPress container. By doing this, we can later access our Docker running services through the http://localhost:8000 address.

#Docker Compose environment variables

Environment variables in Docker Compose enable the flexible and secure configuration of various settings in containers. They help avoid hardcoding values inside the Docker Compose configuration file, such as the database settings in our example.

    MYSQL_DATABASE: wordpress
  	MYSQL_USER: wordpress
  	MYSQL_PASSWORD: wordpresspass
  	MYSQL_ROOT_PASSWORD: rootpass

We can define the above key value pairs inside a .env file in the same directory and refer to them inside the docker-compose.yaml.

#Step 2: How to start services in Docker Compose

Now that you have learned the basic concepts of Docker Compose, you can easily build and run the containers in the above configuration file. Make sure you are in the same directory as docker-compose.yaml is located. I have placed the docker-compose.yaml file inside ~/wp_docker folder.

To start the services in Docker Compose, type:

sudo docker compose up -d

how to start docker compose

The docker compose up command reads the whole content of the docker-compose.yaml configuration file and, based on it, pulls the images for each one of the containers defined, and creates the volumes and networks specified.

Once you execute the above command, Docker Compose builds and starts up the services as shown in the image below.

how to start services in docker compose

Then, on your browser, go to http://127.0.0.1:8000, and the WordPress installation wizard will show up like in the image below.

how to install WordPress with Docker Compose

#Step 3: How to stop running services in Docker Compose

To stop running services in Docker Compose, while in the same directory, type:

sudo docker compose down

how to stop services in docker compose

The docker compose down command not only stops the running services, but it also removes the containers. If you try to visit http://127.0.0.1:8000 now, you will get the message Unable to connect. The reason for that is that the above Docker Compose command stopped all the running services.

To stop a specific running service in Docker Compose, type:


sudo docker compose down wordpress 

Or

sudo docker compose down db 

You can also stop multiple running services in Docker Compose by listing their names in a sequence, as shown in the image below.

how to stop running services in docker compose

Note: The names wordpress and db are defined in the YAML configuration file during Step 1.

#Step 4: How to restart services only in Docker Compose

To restart services only without stopping them in Docker Compose, run:

sudo docker compose restart

The above Docker Compose command restarts all running services.

how to restart services in docker compose

You can also restart a specific service in Docker Compose by typing:

sudo docker compose restart wordpress

how to restart a specific service in docker compose

As you can see from the above command, you specify the service you want to restart by its name. In case you want to restart the database service type:

sudo docker compose restart db

To restart multiple running services in Docker Compose, specify them by name in a sequence, the same way as in Step 3.

how to restart multiple services in docker compose

#Step 5: How to view the status of services in Docker Compose

If you want information on the status of your running services that you have in your Docker Compose setup configuration file, type:

sudo docker compose ps

As you can see from the image below, the above command provides information on the name of the container, service name, its status, and the exposed ports.

how to find information about services in docker compose

As you can see from the above image, information on the uptime of each service is also provided.

To get information on the status of a specific container in Docker Compose, type:

sudo docker compose ps db

how to find information about a specific service in docker compose

Or

sudo docker compose ps wordpress

If you want to find information on another service, just type its name as defined in the YAML configuration file after the sudo docker-compose ps command.

#Step 6: How to view logs in Docker Compose

Docker Compose ships with the necessary built-in commands that can help you diagnose any type of problem with your Docker containers. By providing logs of all the containers defined inside the YAML configuration file, it becomes very useful in troubleshooting and maintaining your containerized applications.

To view the logs of your containers in Docker Compose, type:

sudo docker compose logs 

#Final thoughts

Docker Compose is a go-to tool for anyone who wants to effortlessly deploy the tech stack for running modern applications. Widely used to simplify the management of multiple Docker containers, it is very useful for full-stack apps where front-end and back-end components need to work together.

Cloud VPS Hosting

Starting at just $3.24 / month, get virtual servers with top-tier performance.

Share this article

Related Articles

Published on Oct 1, 2024 Updated on Jan 24, 2025

Docker Network: How to Create and Manage Docker Networks

Learn how to create and manage Docker networks effectively. Discover key network drivers, commands, and tips for troubleshooting Docker networking issues.

Read More
Published on Nov 6, 2024 Updated on Feb 4, 2025

How to Start a Docker Daemon? Complete Guide with Troubleshooting Tips

Learn about Docker daemon (dockerd), the core of Docker. Explore how to start, troubleshoot, and optimize it to manage containers, networks, storage, and system resources.

Read More
Published on Oct 13, 2021 Updated on Feb 25, 2025

How to Install Docker on Ubuntu 20.04? | Step-by-step Tutorial

Docker allows you to package and run applications in self-sufficient software units. Learn how to install and start using Docker on Ubuntu 20.04

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: b11bac4a6.1383