Linux

Install Docker Compose on Ubuntu 20.04 and 22.04

Introduction

Docker Compose is a vital tool for developers and IT professionals managing applications with multiple containers. It enables you to define all containers, networks, and volumes required for your project in a single YAML file. This makes deployment and management faster and less error-prone. Employing Docker Compose, you can begin all services with one command, reducing complexity.

This guide focuses on installing Docker Compose on Ubuntu 20.04 and 22.04, providing compatibility for different system environments. You’ll also learn key commands to simplify everyday tasks. A test setup will demonstrate its utility, and step-by-step instructions for uninstallation will ensure complete system control. 

How to Install Docker Compose on Ubuntu

How to Install Docker Compose on Ubuntu

Installing Docker Compose on Ubuntu involves a few simple steps. Go with these comprehensive instructions to guarantee a hassle-free setup.

1. Prepare Your System

Docker Compose relies on Docker to run. First, update your system’s package list:

sudo apt update

Then, install Docker if it is not on your machine:

sudo apt install docker.io

Once installed, verify that Docker is running properly. Check its status with:

sudo systemctl status docker

If Docker is not operating, begin it:

sudo systemctl start docker

You may also want to enable Docker to begin automatically on boot:

sudo systemctl enable docker

2. Download Docker Compose

Next, download the latest version of Docker Compose from its recognized GitHub repository. Use the following curl command to fetch the binary for your system:

sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This command ensures you get the most recent release compatible with your operating system. 

3. Set Permissions

Once the binary is downloaded, make it executable. This ensures it can be run directly from the command line:

sudo chmod +x /usr/local/bin/docker-compose

To make Docker Compose accessible system-wide, create a symbolic link (optional):

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

4. Verify Installation

Confirm that Docker Compose is installed successfully by checking its version:

docker-compose --version

You should see the version number printed. This confirms that Docker Compose is ready to employ.

5. (Optional) Enable Non-Root Access

By default, Docker needs sudo privileges. To operate Docker Compose as a non-root user, add your user to the docker group:

sudo usermod -aG docker $USER

Log out and log back in for the modifications to be functioning. You can now run Docker Compose without sudo.

Test Run Docker Compose

Test Run Docker Compose

Once Docker Compose is installed, it’s essential to test its functionality. Following is an extensive guide to constructing , running, and managing a simple application.

1. Create a YAML File

Docker Compose uses a YAML file to define multi-container setups. Begin by constructing a file named docker-compose.yml:

nano docker-compose.yml

Add the following configuration to define a simple web service:

version: '3'

services:

  web:

    image: nginx

    ports:

      - "80:80"
  • version: ‘3’: Specifies the Docker Compose file version. Version 3 is widely supported.
  • services: Defines the application’s services. In this situation, we possess one service named web.
  • image: nginx: Pulls the Nginx image from Docker Hub.
  • ports: “80:80”: Maps port 80 of the container to port 80 on the host system.

This simple setup runs an Nginx web server that is accessible via http://localhost.

2. Run the Application

Start the containers using Docker Compose:

docker-compose up -d
  • up: Launches the application as defined in the docker-compose.yml file.
  • -d: Runs the application in detached mode, permitting you to continue employing the terminal.

3. Check Running Containers

Verify the status of the containers:

docker-compose ps

This command shows the list of running containers, their names, state, and exposed ports. For example, you must look for the web service with port 80 mapped.

4. Access the Application

Open a browser and navigate to http://localhost. You should see the default Nginx welcome page, confirming the container is running correctly.

5. Stop the Application

When you’re done testing, stop and eliminate the containers:

docker-compose down
  • down: Stops the running containers and removes associated networks.
  • Persistent Data: Any volumes created for persistent data will remain unless explicitly removed.

6. Clean Up (Optional)

If you no longer need the Docker images or volumes:

Remove images:

docker rmi nginx

Prune unused resources:

docker system prune -a

Docker Compose Basic Commands

Docker Compose Basic Commands

Docker Compose provides several commands to simplify container management. These commands help you control services, view logs, and clean up resources efficiently. Here’s an expanded overview of the most commonly used commands.

1. Start Services

Start the services defined in your docker-compose.yml file:

docker-compose up

Run in Detached Mode: To run containers in the background:

docker-compose up -d

Rebuild Images: Use the –build flag to force rebuilding images before starting:

docker-compose up --build

2. Stop Services

Stop running services without removing containers:

docker-compose stop

Pause Services: Temporarily pause containers:

docker-compose pause

Resume Paused Services: To resume paused containers:

docker-compose unpause

3. View Logs

Display logs for all services in real-time:

docker-compose logs

Follow Logs: Continuously stream logs as they are generated:

docker-compose logs -f

Filter Logs: View logs for a specific service:

docker-compose logs <service_name>

4. Inspect Running Containers

List and inspect running services:

docker-compose ps

Detailed Output: For more details about a specific service:

docker inspect <container_id>

5. Scale Services

Adjust the number of instances for a service:

docker-compose up --scale <service_name>=<number>

For example, to scale a web service to 3 instances:

docker-compose up --scale web=3

6. Remove Containers and Networks

Stop and remove all containers, networks, and related resources:

docker-compose down

Remove Volumes: Include the -v flag to delete persistent data volumes:

docker-compose down -v

Remove Images: Add the –rmi flag to delete all associated images:

docker-compose down --rmi all

7. Restart Services

Restart services to apply changes or fix issues:

docker-compose restart

8. View Configuration

Check the effective configuration of your services:

docker-compose config

Validate your configuration file:

docker-compose config --quiet

Uninstall Docker Compose on Ubuntu

Uninstall Docker Compose on Ubuntu

If you does not require Docker Compose anymore, you can discard it from your system. The process involves deleting the binary and optionally cleaning up configurations and cached data.

1. Remove the Binary

The primary step in uninstalling Docker Compose is to delete its executable file:

sudo rm /usr/local/bin/docker-compose
  • This removes the Docker Compose binary from your system.
  • Use docker-compose –version to confirm it has been uninstalled. The command should return an error indicating it is no longer available.

2. Clean Up Configurations (Optional)

Docker Compose creates configurations and files during its operation. These files are often stored in directories where Docker Compose projects were executed. To ensure a complete cleanup:

Locate Configuration Files
Check your project directories for docker-compose.yml and .env files. Discard them in case they are no more required:

rm -f /path/to/project/docker-compose.yml
rm -f /path/to/project/.env

Remove Docker Volumes
Persistent volumes created by Docker Compose can take up disk space. List and delete unused volumes:

docker volume ls
docker volume prune

Delete Docker Networks
Networks specific to Docker Compose projects can also be removed:

docker network ls

docker network prune

3. Uninstall Docker (If Needed)

If Docker itself is no longer required, you can remove it too:

sudo apt remove --purge docker.io

Clean up any remaining Docker-related directories:

sudo rm -rf /var/lib/docker

sudo rm -rf /etc/docker

sudo rm -rf ~/.docker

4. Verify Cleanup

Run the following commands to ensure all traces of Docker Compose and Docker are removed:

which docker-compose

which docker

docker ps

These commands should return errors or empty outputs, confirming the removal.

Also Read: How to Install Docker Portainer on Linux?

Conclusion

Docker Compose revolutionizes container management, making multi-container applications easier to handle. Its YAML-based configuration is intuitive, allowing you to define services, networks, and volumes in one place. This ensures consistency across environments, from local development to production.

Installing Docker Compose on Ubuntu 20.04 and 22.04 is hassle-free. With the setup complete, you can use its powerful commands to manage containerized applications efficiently. By deploying and scaling services quickly, you save time and resources. It does not matter if you are a system administrator or developer, Docker Compose simplifies your workflow and enhances productivity. Follow the steps outlined here to unlock its full potential in your projects.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

[sc name="footer"][/sc]