Tools: How to Install Docker Compose on Ubuntu (Step-by-Step Guide)

Tools: How to Install Docker Compose on Ubuntu (Step-by-Step Guide)

Source: DigitalOcean

By Anish Singh Walia and Vinayak Baranwal Docker simplifies the process of managing application processes in containers. While containers are similar to virtual machines in certain ways, they are more lightweight and resource-friendly. This allows developers to break down an application environment into multiple isolated services. For applications depending on several services, orchestrating all the containers to start up, communicate, and shut down together can quickly become unwieldy. Docker Compose is a tool that allows you to run multi-container application environments based on definitions set in a YAML file. It uses service definitions to build fully customizable environments with multiple containers that can share networks and data volumes. In this guide, you’ll learn how to install Docker Compose on Ubuntu and get started using this tool. Simplify deploying applications to servers with DigitalOcean App Platform. Deploy directly from GitHub in minutes. To follow this article, you will need: Note: This tutorial covers installing Docker Compose v1, which uses docker-compose. Docker Compose v2 uses the compose CLI plugin command (documented in our Ubuntu 22.04 version of this tutorial). The main difference is v2 uses docker compose (without hyphen) instead of docker-compose. For compatibility details, see the official Docker documentation. The following table compares Docker Compose v1 and v2 to help you choose the right version: For most new installations, Docker Compose v2 is recommended. However, this tutorial focuses on v1 for systems that require the standalone binary or compatibility with older Docker versions. Download Docker Compose from its official GitHub repository. First, visit the releases page and note the latest version number (e.g., 1.29.2). Replace 1.29.2 in the command below with the latest version number you found. The following command downloads the release and saves the executable to /usr/local/bin/docker-compose, making it globally accessible: Set executable permissions for the docker-compose command: Verify the installation: You’ll see output similar to this: Docker Compose is now installed. Next, you’ll set up a docker-compose.yml file and run a containerized environment. Create a web server environment using the official Nginx image from Docker Hub to demonstrate Docker Compose. This environment serves a single static HTML file. Create a new directory in your home folder and navigate into it: Create an application folder to serve as the document root for your Nginx environment: Create a new index.html file in the app folder: Place the following content into this file: Save and close the file. In nano, press CTRL+X, then Y, then ENTER. Next, create the docker-compose.yml file: Insert the following content in your docker-compose.yml file: The docker-compose.yml file starts with a version definition specifying the Compose file format version. Version '3.7' is compatible with Docker Compose v1. In newer Docker Compose versions, this field is optional, but including it ensures compatibility. The services block defines the services in this environment. Here, a single web service uses the nginx:alpine image. The ports directive maps port 8000 on the host to port 80 in the container where Nginx runs. The volumes directive creates a shared volume between the host and container, mounting the local app folder to /usr/share/nginx/html inside the container, replacing Nginx’s default document root. Save and close the file. You’ve set up a demo page and docker-compose.yml file. Next, start the containerized environment with Docker Compose. With the docker-compose.yml file in place, start the environment. This command downloads necessary Docker images, creates a container for the web service, and runs it in the background: Docker Compose checks for the image locally first, then downloads from Docker Hub if needed. You’ll see output like this: Note: If you encounter a permission error regarding the Docker socket, complete Step 2 of How To Install and Use Docker on Ubuntu to enable running Docker commands without sudo. The environment is running in the background. Verify the container is active: This shows information about running containers, their state, and port mappings: Access the demo application at localhost:8000 on a local machine, or your_server_domain_or_IP:8000 on a remote server. You’ll see a page like this: The shared volume keeps your app folder files in sync with the container’s document root. Changes to index.html are automatically reflected when you reload the page. Next, manage your containerized environment with Docker Compose commands. Use Docker Compose commands to manage and interact with your containerized environment. Check logs from your Nginx container: You’ll see output similar to this: Pause the environment without changing container state: Resume execution after pausing: Stop containers without destroying data: Remove containers, networks, and volumes: This doesn’t remove the base image (nginx:alpine). Running docker-compose up again will be faster since the image remains on your system. To remove the base image: Note: For more Docker commands, see How to Install and Use Docker on Ubuntu. This error indicates docker-compose is not in your system’s PATH, usually due to an incomplete installation. Fix by either adding the directory to your PATH or using the full path. If docker-compose is in /usr/local/bin, run: Permission issues occur when your user lacks permissions to run Docker commands. Fix by either running with sudo or adding your user to the docker group. To use sudo: To add your user to the docker group: Remember to log out and log back in for the changes to take effect. Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define multi-container applications in a YAML file and start all services with a single command. For example, you can define a service for a web server and another for a database, and Docker Compose will handle the creation and linking of these services. Check the Docker Compose releases page for the latest version. This displays the installed Docker Compose version. No, Docker Compose requires Docker to be installed and running on your system. Docker Compose is a tool that builds on top of Docker, so Docker must be installed first. Since this tutorial installs via binary download, remove it manually: If you installed via APT, use sudo apt-get purge docker-compose. In this guide, you’ve seen how to install Docker Compose and set up a containerized environment based on an Nginx web server image. You’ve also seen how to manage this environment using Compose commands. For a complete reference of all available docker-compose commands, check the official documentation. If you’re interested in more usages of Docker Compose, check out the following tutorials: Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about our products I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix Building future-ready infrastructure with Linux, Cloud, and DevOps. Full Stack Developer & System Administrator. Technical Writer @ DigitalOcean | GitHub Contributor | Passionate about Docker, PostgreSQL, and Open Source | Exploring NLP & AI-TensorFlow | Nailed over 50+ deployments across production environments. This textbox defaults to using Markdown to format your answer. You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link! sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose the version number (1.27.4) is missing a leading “v”, i.e. “v1.27.4”. Can we also use apt install docker-compose? This command worked better for me, FYI: platform=$(uname -s) ; sudo curl -L “https://github.com/docker/compose/releases/download/v2.11.2/docker-compose-${platform,,}-$(uname -m)” -o /usr/local/bin/docker-compose Please complete your information! Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. Full documentation for every DigitalOcean product. The Wave has everything you need to know about building a business, from raising funding to marketing your product. Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter. New accounts only. By submitting your email you agree to our Privacy Policy Scale up as you grow — whether you're running one virtual machine or ten thousand. Sign up and get $200 in credit for your first 60 days with DigitalOcean.* *This promotional offer applies to new accounts only.