Docker Compose on Ubuntu 20.04: Installation and Usage Guide


7 min read 14-11-2024
Docker Compose on Ubuntu 20.04: Installation and Usage Guide

Docker has revolutionized the way we approach software development and deployment. With its ability to containerize applications, developers can create, manage, and scale applications with unprecedented ease. To enhance this powerful technology, Docker Compose comes into play. It allows developers to define and run multi-container Docker applications seamlessly. This comprehensive guide will walk you through the installation and usage of Docker Compose on Ubuntu 20.04, providing you with everything you need to get started.

What is Docker Compose?

Docker Compose is a tool that simplifies the management of multi-container Docker applications. With Docker Compose, you can define all your app's services, networks, and volumes in a single file called docker-compose.yml. This file is not just a recipe for your application but a clear blueprint that streamlines the entire development process.

Imagine you have a web application that requires a web server, a database, and a caching service. Instead of managing each container individually, Docker Compose allows you to configure everything in one concise file. You can then use simple commands to start, stop, and scale your services with ease.

Why Use Docker Compose?

  1. Simplified Configuration: With Docker Compose, you can define your entire application's configuration in one YAML file. This makes it easy to manage complex applications with multiple services.

  2. Effortless Scaling: Need more instances of your service? Docker Compose makes it as simple as running a command to scale your containers horizontally.

  3. Consistent Environments: By using Docker Compose, you ensure that your application runs in a consistent environment, reducing the "it works on my machine" problem.

  4. Version Control: Since your application configuration is defined in a YAML file, you can easily version control it using Git or other version control systems.

  5. Multi-host Deployment: With Docker Compose, you can deploy your application across different machines or hosts seamlessly.

Prerequisites for Installation

Before we proceed with the installation of Docker Compose on Ubuntu 20.04, ensure that you meet the following prerequisites:

  • Ubuntu 20.04 Operating System: Make sure you're running Ubuntu 20.04, as this guide is tailored for this version.

  • Docker Installed: You need Docker installed on your machine. If you haven’t done this yet, follow our installation steps in the next section.

  • Terminal Access: Ensure you have terminal access with sudo privileges.

Installing Docker on Ubuntu 20.04

Before we can use Docker Compose, we first need to have Docker installed. Here are the steps to install Docker on Ubuntu 20.04:

1. Update the Package Index

Open your terminal and update the package index:

sudo apt update

2. Install Required Packages

Next, install some necessary packages that allow apt to use packages over HTTPS:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

3. Add Docker’s Official GPG Key

Add Docker’s official GPG key to your system:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4. Set Up the Stable Repository

Add the Docker stable repository to your system:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

5. Install Docker

After adding the Docker repository, update the package index again and install Docker:

sudo apt update
sudo apt install docker-ce

6. Verify Docker Installation

To verify that Docker has been installed successfully, run the following command:

sudo systemctl status docker

You should see an output indicating that Docker is active and running.

7. Run Docker without sudo (Optional)

To run Docker commands without needing to prefix them with sudo, add your user to the Docker group:

sudo usermod -aG docker $USER

Log out and back in for this change to take effect.

Installing Docker Compose

Now that Docker is installed, let’s move on to installing Docker Compose.

1. Download the Latest Version

Check the Docker Compose releases page for the latest version number. As of this writing, the latest version is 1.29.2. Use the command below to download it:

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

2. Apply Executable Permissions

Next, apply executable permissions to the Docker Compose binary:

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

3. Verify the Installation

To confirm that Docker Compose was installed correctly, check its version:

docker-compose --version

You should see the installed version of Docker Compose displayed.

Creating a Docker Compose File

Now that we have Docker Compose installed, let's create a simple Docker Compose application to understand its usage better. We will create a basic web application with Nginx.

1. Create a Project Directory

Start by creating a new directory for your project:

mkdir myapp
cd myapp

2. Create a docker-compose.yml File

In your project directory, create a file named docker-compose.yml:

touch docker-compose.yml

Open the file in your favorite text editor (e.g., nano, vim, code, etc.) and add the following content:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"

3. Understanding the File Structure

In this docker-compose.yml file:

  • version: Specifies the Docker Compose file format version.
  • services: Defines the services that will be part of the application. Here we define a single service named web.
  • image: Specifies the Docker image to use. In our case, we are using the latest version of the Nginx image.
  • ports: Maps port 8080 on the host to port 80 on the container.

Running the Application with Docker Compose

Now, let’s run our application using Docker Compose.

1. Start the Application

With your terminal still in the myapp directory, run the following command:

docker-compose up

This command will pull the Nginx image (if it's not already on your system), create the container, and start it.

2. Accessing the Application

Open your web browser and navigate to http://localhost:8080. You should see the default Nginx welcome page, indicating that your service is running.

3. Stopping the Application

To stop the application, simply press CTRL + C in the terminal where Docker Compose is running. If you want to stop and remove the containers, use:

docker-compose down

Using Docker Compose Commands

Docker Compose provides various commands to manage your containers effectively. Let’s explore some commonly used commands:

  1. Starting Services:

    • docker-compose up (Starts the services defined in the YAML file)
    • docker-compose up -d (Starts the services in detached mode)
  2. Stopping Services:

    • docker-compose stop (Stops running services)
    • docker-compose down (Stops and removes all services)
  3. Viewing Logs:

    • docker-compose logs (Displays logs for all services)
    • docker-compose logs <service> (Displays logs for a specific service)
  4. Scaling Services:

    • docker-compose up --scale web=3 (Starts three instances of the web service)
  5. Executing Commands in Containers:

    • docker-compose exec <service> <command> (Executes a command in a running service’s container)

Best Practices for Docker Compose

  1. Use Version Control: Always keep your docker-compose.yml file in version control to track changes.

  2. Environment Variables: Utilize environment variables to avoid hardcoding sensitive information like passwords in your docker-compose.yml.

  3. Build Context: For applications that require custom images, define a build context in your Docker Compose file.

  4. Document Your Configuration: Include comments in your YAML file to explain service definitions, ports, and other configurations.

  5. Network Configuration: Consider configuring custom networks to enhance communication between services.

Troubleshooting Common Issues

Even with a simple tool like Docker Compose, you might encounter issues. Here are some common problems and their solutions:

1. Service Not Starting

If a service fails to start, use docker-compose logs to check for errors in the logs. Common issues include network conflicts or missing environment variables.

2. Port Already in Use

If you encounter a message indicating that a port is already in use, modify the ports mapping in your docker-compose.yml file to use a different host port.

3. Image Pull Failures

Sometimes, images might fail to pull due to network issues. Ensure your internet connection is stable, or retry pulling the image directly with docker pull <image-name>.

Advanced Features of Docker Compose

Once you are comfortable with the basics, you can explore advanced features of Docker Compose to improve your workflow.

1. Docker Compose Override File

You can create an override file named docker-compose.override.yml to customize your setup without modifying the main docker-compose.yml. This is especially useful for development and testing environments.

2. Using Multiple Compose Files

You can use multiple Compose files to manage different environments (e.g., staging, production) by specifying the -f option:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up

3. Defining Networks

To optimize communication between containers, define custom networks in your docker-compose.yml file:

networks:
  app-network:
services:
  web:
    image: nginx:latest
    networks:
      - app-network

Conclusion

Docker Compose on Ubuntu 20.04 provides an efficient way to manage multi-container applications. With a single YAML file, you can define services, networks, and volumes, leading to a smoother development experience. We’ve walked through the installation steps, basic usage, common commands, and even some advanced features.

Incorporating Docker Compose into your development workflow not only simplifies container management but also enhances collaboration within your team. As applications become more complex, leveraging tools like Docker Compose ensures you maintain an organized and scalable architecture.

So, are you ready to dive deeper into the world of container orchestration? With the power of Docker Compose, your development possibilities are limitless!

Frequently Asked Questions

1. What is Docker Compose?
Docker Compose is a tool that allows you to define and run multi-container applications with Docker using a simple YAML configuration file.

2. How do I install Docker on Ubuntu 20.04?
You can install Docker by updating your package index, installing necessary packages, adding the Docker GPG key, setting up the stable repository, and finally installing Docker with apt.

3. Can I run Docker Compose without Docker?
No, Docker Compose depends on Docker to manage and orchestrate containers.

4. What is a docker-compose.yml file?
It is a YAML file that defines all the services, networks, and volumes required for a Docker application.

5. How do I scale services with Docker Compose?
You can scale services using the --scale flag when running docker-compose up, e.g., docker-compose up --scale web=3 to run three instances of the web service.