Best Guide For Docker Compose Installation on Ubuntu 24.04

Posted on

Best Guide For Docker Compose Installation on Ubuntu 24.04

This guide aims to walk you through the Docker Compose Installation on Ubuntu 24.04. Docker Compose is a powerful tool that simplifies the process of defining and managing multi-container Docker applications using a user-friendly YAML file.

The Docker Compose Installation on Ubuntu 24.04 streamlines configuration management, making it easier to orchestrate complex applications. Follow the step-by-step instructions provided by the Orcacore team to successfully set up Docker Compose on your Ubuntu 24.04 system.

Simplifying Development with Docker Compose Installation on Ubuntu 24.04

Before diving into the Docker Compose Installation on Ubuntu 24.04, ensure that Docker is already installed on your server. Refer to this helpful guide on Setting up Docker CE For Ubuntu 24.04 for detailed instructions.

Once Docker is successfully installed, you can proceed with the following steps to complete the Docker Compose Installation on Ubuntu 24.04.

You can also follow this video tutorial for installing Docker and Docker Compose:

Step 1 – Download and Build Docker Compose on Ubuntu 24.04

First, visit the GitHub Releases Page for Docker Compose and use the following commands to download the latest Docker Compose version on Ubuntu 24.04.

As of the current time, the latest version of Docker Compose is v2.29.7. Use the following curl command to download it:

# mkdir -p ~/.docker/cli-plugins/

# curl -SL https://github.com/docker/compose/releases/download/v2.29.7/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

Next, set the correct permissions for the Docker Compose path on Ubuntu 24.04 using the following command:

sudo chmod +x ~/.docker/cli-plugins/docker-compose

Now, your Docker Compose Installation on Ubuntu 24.04 should be complete. To verify the installation, check the Docker Compose version:

docker compose version

The output should resemble the following:

**Output**
Docker Compose version v2.29.7

Note: Docker Compose uses a YAML file to define and manage Docker containers. The next step will demonstrate how to create and use a Docker Compose YAML file on Ubuntu 24.04.

Step 2 – Docker Compose yml File Example on Ubuntu 24.04

Now that you have completed the Docker Compose Installation on Ubuntu 24.04, you can learn how to create a docker-compose.yml file.

This example demonstrates creating a web server environment using the official Nginx image from Docker Hub.

First, create a new directory in your home folder and navigate into it:

# mkdir ~/compose-demo
# cd ~/compose-demo

Then, create a folder to serve as the document root for the Nginx environment:

mkdir app

Create a new index.html file in the document root using your favorite text editor, such as Vi Editor or Nano Editor:

vi app/index.html

Paste the following content into your file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Docker Compose Demo</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
</head>
<body>

    <h1>This is a Docker Compose Demo Page.</h1>
    <p>This content is being served by an Nginx container.</p>

</body>
</html>

Save and close the file.

Next, create your Docker Compose YML file:

vi docker-compose.yml

Paste the following content into the file:

version: '3.8'
services:
  web:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./app:/usr/share/nginx/html

Save and close the file.

Note: The version specified at the top defines the syntax and features available for that configuration. For example, version: '3.8' indicates that the file is using version 3.8 of the Compose file format, which includes features like networks, volumes, and services.

Now that you have learned Docker Compose Installation Ubuntu 24.04 and how to create an example YAML file, you can use Docker Compose to run the container.

Step 3 – Run Container with Docker Compose on Ubuntu 24.04

At this point, you can easily run the environment you created in the previous step. Use the following Docker Compose command:

docker compose up -d

The output should resemble the following:

Docker Compose Installation on Ubuntu 24.04 - Run Containers

As you can see, the container is now up and running.

To verify that your container is active, run the following command:

docker compose ps

The output should resemble the following:


Now you can access the Docker Compose demo page on Ubuntu 24.04 by typing your server’s IP address in your web browser followed by :8000:

http://your-IP-address:8000

Step 4 – Essential Docker Compose Commands

Here are some essential Docker Compose commands that can be helpful for managing your containers:

docker compose logs
docker compose pause
docker compose unpause
docker compose stop
# docker compose down
# docker image rm nginx:alpine

Step 5 – Uninstall Docker Compose From Ubuntu 24.04

You have now seen how to complete the Docker Compose Installation on Ubuntu 24.04, create a YAML file, and use Docker Compose.

If you want to remove and uninstall Docker Compose, run the following command:

rm $DOCKER_CONFIG/cli-plugins/docker-compose

Alternatively, if you installed Docker Compose for all users, run this command:

rm /usr/local/lib/docker/cli-plugins/docker-compose

Note: To check where Compose is installed, run the following command:

docker info --format '{{range .ClientInfo.Plugins}}{{if eq .Name "compose"}}{{.Path}}{{end}}{{end}}'

That’s it! You are done with Docker Compose Installation on Ubuntu 24.04. For more information, check the Official Docker Docs.

Conclusion

As demonstrated, the Docker Compose Installation on Ubuntu 24.04 is a straightforward process. Simply install Docker CE, download, and build the latest version of Compose. Then, you can easily use docker-compose to manage your containers.

Hope you found this guide helpful! You might also be interested in these articles:

Run Nextcloud on Ubuntu 24.04

LAMP Stack Installation on Ubuntu 24.04

Enable NTP Service on Ubuntu 24.04

Install WineHQ on Ubuntu 24.04

FAQs

How do I create a Docker Compose file?

Create a `docker-compose.yml` file in your project directory and define your services, networks, and volumes in YAML format. The example of creating a docker-compose YAML file is explained in the above guide on Docker Compose installation on Ubuntu 24.04.

How do I start my application with Docker Compose?

You can use the docker compose up command.

Can I run Docker Compose in detached mode?

Yes, use the `-d` flag to run it in detached mode: docker compose up -d

Alternative Solutions for Managing Multi-Container Applications

While Docker Compose is a great tool, there are alternative methods for managing multi-container applications. Here are two options:

1. Kubernetes (with Minikube for Local Development)

Kubernetes is a container orchestration platform designed for automating deployment, scaling, and management of containerized applications. While it’s more complex than Docker Compose, it offers significantly more features, especially for production environments.

Explanation:

  • Kubernetes: A production-grade container orchestration system.
  • Minikube: A lightweight Kubernetes distribution ideal for local development and testing. It allows you to run a single-node Kubernetes cluster on your machine.

How it solves the problem:

Instead of defining your application in a docker-compose.yml file, you define it using Kubernetes YAML manifests (e.g., Deployments, Services, Pods). These manifests specify the desired state of your application, and Kubernetes automatically manages the containers to achieve that state.

Code Example (Simplified Kubernetes Deployment Manifest):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
      - name: web-app
        image: nginx:alpine
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  selector:
    app: web-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer # Or NodePort for local minikube

Steps to Use:

  1. Install Minikube and kubectl: Follow the instructions on the Kubernetes website to install Minikube and kubectl (the Kubernetes command-line tool).
  2. Start Minikube: minikube start
  3. Apply the Manifest: kubectl apply -f your-manifest.yaml
  4. Access the Application: Use minikube service web-app-service --url to get the URL to access your application.

Pros:

  • Scalability and high availability.
  • Advanced features like rolling updates, self-healing, and service discovery.
  • Industry-standard for production deployments.

Cons:

  • Steeper learning curve than Docker Compose.
  • More complex configuration.
  • More resource-intensive, even with Minikube.

2. Docker Swarm

Docker Swarm is Docker’s native clustering solution. It allows you to manage a cluster of Docker engines as a single virtual system. While simpler than Kubernetes, it still offers some orchestration capabilities beyond Docker Compose.

Explanation:

  • Docker Swarm: Docker’s built-in orchestration solution.
  • Swarm Mode: A feature of the Docker Engine that enables clustering and orchestration.

How it solves the problem:

Similar to Docker Compose, Docker Swarm uses a YAML file (often very similar to a docker-compose.yml) to define your application. However, Swarm extends this functionality to allow you to deploy and manage the application across multiple Docker hosts.

Code Example (Docker Swarm YAML – largely compatible with Docker Compose):

version: "3.9"
services:
  web:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./app:/usr/share/nginx/html
    deploy:
      replicas: 3  # Run three instances of the web service
      restart_policy:
        condition: on-failure # Restart if the container fails

Steps to Use:

  1. Initialize the Swarm: On one of your Docker hosts, run docker swarm init to initialize the Swarm.
  2. Join Worker Nodes: On other Docker hosts, run the command provided by docker swarm init to join the Swarm as worker nodes.
  3. Deploy the Stack: docker stack deploy -c your-swarm-file.yaml your_app_name
  4. Access the Application: Docker Swarm will handle load balancing across the replicas. Access the application via the IP address of any of the nodes on port 8000.

Pros:

  • Relatively easy to set up and use, especially if you’re already familiar with Docker.
  • Integrated with Docker Engine.
  • Provides basic orchestration features like service scaling and rolling updates.

Cons:

  • Less feature-rich than Kubernetes.
  • Smaller community and ecosystem compared to Kubernetes.
  • Not as well-suited for complex deployments.

These alternative solutions, Kubernetes (via Minikube) and Docker Swarm, offer different trade-offs between complexity, features, and resource requirements compared to Docker Compose. Choosing the right tool depends on the specific needs of your project.