How to Set Up and Use Docker Compose on Ubuntu 22.04 LTS

Docker Compose is an invaluable tool that streamlines the management of multi-container applications, making deployments easier for DevOps engineers. By setting up Docker Compose on Ubuntu 22.04 LTS, you can define, run, and manage applications with ease, simplifying development, testing, and production processes. This guide will help you install and use Docker Compose, allowing you to orchestrate container services efficiently.

Prerequisites

Ensure you have:

  • Administrative access to complete the installation and configuration tasks.
  • A basic understanding of Docker and containerization concepts.
  • Docker installed on your system. If not, install Docker using:
sudo apt update && sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

Step-by-Step Implementation

Step 1: Install Docker Compose

Begin by updating your package list and installing Docker Compose:

sudo apt update && sudo apt install docker-compose -y

Step 2: Verify the Installation

Check that Docker Compose has been successfully installed:

docker-compose --version

This command should return the version of Docker Compose, confirming the installation was successful.

Step 3: Create a Sample Project Directory

Create a directory for your project and navigate into it:

mkdir my-app
cd my-app

Step 4: Create a docker-compose.yml File

Define the services for your application in a docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:80"
    depends_on:
      - db
  db:
    image: postgres:12
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydatabase

This configuration defines two services:

  • web: Builds from the current directory and maps port 8080 on the host to port 80 in the container.
  • db: Uses a postgres:12 image with environment variables to set up the database user, password, and name.

Step 5: Create a Dockerfile

Create a Dockerfile to define how the web service should be built:

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

This Dockerfile sets up a Python environment, installs the necessary dependencies, and defines how the application should run.

Step 6: Build and Start Your Services

Run the following command to build the Docker images:

docker-compose build

Once the build completes, start your services:

docker-compose up -d

The -d flag runs the services in detached mode, allowing them to run in the background.

Step 7: Verify Running Containers

Ensure your containers are running by using:

docker-compose ps

This will display the status of the services defined in your docker-compose.yml.

Best Practices

  • Environment Variables: Use a .env file to manage sensitive data, such as database passwords and API keys.
  • Service Dependencies: Properly configure depends_on in docker-compose.yml to ensure services start in the correct order.
  • Resource Management: Define resource limits for containers to ensure optimal performance.
  • Regular Updates: Keep Docker and Docker Compose updated for security patches and new features.

Troubleshooting

Common Issues

  1. Service Dependencies: If services fail to start, ensure that depends_on is correctly configured.
  2. Environment Variables: Missing or misconfigured variables can prevent services from functioning correctly.
  3. Port Conflicts: Ensure that the ports defined in docker-compose.yml do not conflict with other services running on your system.

Check logs using:

docker-compose logs

For more information, refer to the official Docker Compose documentation.

Conclusion

In this guide, we covered how to set up and use Docker Compose on Ubuntu 22.04 LTS. By following these steps, you can orchestrate and manage complex applications with ease. Docker Compose is a powerful tool that helps automate and simplify your DevOps workflow, allowing you to focus on building and deploying high-quality applications. Start experimenting with Docker Compose today and unlock its full potential in your development and production environments.