월. 8월 11th, 2025

G: 🚀 Are you a developer looking to streamline your workflow, eliminate “it works on my machine” issues, and create consistent development environments? Look no further! Docker is your answer. And when it comes to managing multi-container applications, Docker Compose is your best friend.

In this comprehensive guide, we’ll walk you through installing both Docker and Docker Compose on your Ubuntu system, ensuring you have a robust and ready-to-use development setup. Let’s dive in! 🐳


What Are Docker and Docker Compose? Why Do You Need Them?

Before we jump into the installation steps, let’s quickly understand what these powerful tools are and why they’re indispensable for modern development.

  • Docker: Imagine a shipping container, but for your software. Docker allows you to package an application and all its dependencies (libraries, frameworks, configurations) into a standardized unit called a “container.” This container runs consistently across any environment – your laptop, a testing server, or a production cloud. Say goodbye to “works on my machine”! 👋

    • Benefits:
      • Isolation: Your app runs in its own isolated environment, preventing conflicts with other apps or your host system.
      • Portability: Containers run identically everywhere.
      • Consistency: Consistent environments from development to production.
      • Efficiency: Faster setup, easier scaling, and efficient resource usage.
  • Docker Compose: While Docker is great for single containers, most real-world applications consist of multiple services (e.g., a web server, a database, a caching layer). Docker Compose is a tool for defining and running multi-container Docker applications. With a single YAML file, you can configure all your application’s services, networks, and volumes, and then launch them all with a single command. 🪄

    • Benefits:
      • Simplified Orchestration: Define complex multi-service apps in one file.
      • Easy Management: Start, stop, and rebuild all services with simple commands.
      • Reproducibility: Share your docker-compose.yml and others can spin up your entire dev environment instantly.

Prerequisites 📋

Before we begin, make sure you have:

  1. An Ubuntu System: This guide is written for Ubuntu (20.04 LTS, 22.04 LTS, or newer versions).
  2. Sudo Privileges: You’ll need sudo access to run administrative commands.
  3. Internet Connection: To download packages and dependencies.

Part 1: Installing Docker Engine on Ubuntu 🐧

We’ll install Docker from its official repository to ensure you get the latest stable version. This is the recommended approach over using Ubuntu’s default apt repository, which often provides older versions.

Step 1: Update Your System & Install Dependencies

First, update your apt package index and install necessary packages that allow apt to use a repository over HTTPS.

sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release -y
  • ca-certificates: Allows web browsers and other programs to check the authenticity of SSL/TLS certificates.
  • curl: A command-line tool for transferring data with URLs. We’ll use it to download Docker’s GPG key.
  • gnupg: Enables GnuPG support for apt to verify packages.
  • lsb-release: Utility to identify the Linux Standard Base and specific distribution information.

Step 2: Add Docker’s Official GPG Key

Docker’s packages are signed, and we need to add their GPG key to your system’s keyring to verify the authenticity of the packages you download.

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
  • sudo mkdir -p /etc/apt/keyrings: Creates the directory for apt keyrings if it doesn’t exist.
  • curl -fsSL ... | sudo gpg --dearmor -o ...: Downloads the GPG key and pipes it to gpg to dearmor it (convert to a binary format) and save it to the specified file.
  • sudo chmod a+r ...: Sets appropriate permissions for the key file.

Step 3: Add the Docker Repository to APT Sources

Now, add the Docker stable repository to your system’s apt sources.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

This command automatically detects your system’s architecture and Ubuntu version ($(lsb_release -cs)) and adds the correct repository entry.

Step 4: Install Docker Engine

Finally, update your apt package index again to include the new Docker repository, and then install Docker Engine, CLI, and containerd.

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y
  • docker-ce: The Docker Community Edition engine.
  • docker-ce-cli: The command-line interface for Docker.
  • containerd.io: A high-level container runtime that Docker uses.

Step 5: Verify Docker Installation

To confirm Docker is installed and running, check its status and run the hello-world container.

sudo systemctl status docker

You should see output indicating active (running).

Now, let’s run a test container:

sudo docker run hello-world

If successful, you’ll see a message similar to:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

🎉 Congratulations! Docker is now installed!

Step 6: Post-installation Steps (Running Docker as a Non-Root User) – IMPORTANT!

By default, the Docker daemon binds to a Unix socket owned by the root user. This means you must use sudo to run Docker commands. To run Docker commands without sudo (which is highly recommended for convenience and typical development workflows), you need to add your user to the docker group.

  1. Add your user to the docker group:

    sudo usermod -aG docker $USER
    • Replace $USER with your actual username, or just leave it as is; it’s a shell variable that expands to your current username.
  2. Activate the changes: For the changes to take effect, you need to either log out and log back in, or simply run:

    newgrp docker

    This command temporarily switches your current shell session’s primary group to docker.

  3. Verify again (without sudo):

    docker run hello-world

    If it runs without a permission denied error, you’ve successfully configured Docker for your user! ✨


Part 2: Installing Docker Compose on Ubuntu 🛠️

While Docker offers a docker-compose-plugin that comes with recent Docker Engine installations, many developers still prefer the standalone docker-compose executable for clarity and backward compatibility. We will install the standalone version directly from GitHub, which is the most common and up-to-date method.

Step 1: Download Docker Compose

First, check the latest stable release version of Docker Compose from its GitHub repository: https://github.com/docker/compose/releases. Look for the latest stable release (e.g., v2.24.5).

Then, use curl to download the executable to /usr/local/bin/.

Note: Replace v2.24.5 in the URL below with the actual latest version you found on the GitHub releases page!

LATEST_COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -Po '"tag_name": "\K[^"]*')
sudo curl -L "https://github.com/docker/compose/releases/download/${LATEST_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • LATEST_COMPOSE_VERSION: This command automatically fetches the latest stable release tag from GitHub, making the installation script more robust.
  • curl -L: Downloads the file. -L follows redirects.
  • $(uname -s): Outputs your operating system name (e.g., Linux).
  • $(uname -m): Outputs your machine hardware name (e.g., x86_64).
  • -o /usr/local/bin/docker-compose: Specifies the output file path and name. /usr/local/bin is a common directory for user-installed executables.

Step 2: Apply Executable Permissions

Make the downloaded file executable:

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

This command adds execute permissions to the docker-compose file.

Step 3: Verify Docker Compose Installation

Check the installed version to confirm everything is set up correctly:

docker-compose --version

You should see output similar to:

Docker Compose version v2.24.5

🥳 Awesome! Docker Compose is now ready to roll!


Part 3: Verifying Your Setup with an Example Project 🚀

Let’s create a simple docker-compose.yml file to demonstrate that both Docker and Docker Compose are working seamlessly together. We’ll deploy a basic Nginx web server.

  1. Create a new directory for your project:

    mkdir my-nginx-app
    cd my-nginx-app
  2. Create a docker-compose.yml file:

    Open a text editor (like nano or vim) and paste the following content:

    nano docker-compose.yml

    Paste this into the file:

    version: '3.8'
    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
        volumes:
          - ./nginx.conf:/etc/nginx/nginx.conf:ro
        restart: always

    Save and exit (Ctrl+O, Enter, Ctrl+X for nano).

    This docker-compose.yml file defines a single service named web that uses the nginx:latest Docker image, maps port 80 of your host to port 80 of the container, and mounts a local nginx.conf file.

  3. Create a dummy nginx.conf file:

    Let’s create a very basic Nginx configuration file for our example.

    nano nginx.conf

    Paste this content:

    events {
        worker_connections 1024;
    }
    
    http {
        server {
            listen 80;
            server_name localhost;
    
            location / {
                default_type text/plain;
                return 200 'Hello from Nginx in Docker Compose! 🎉\n';
            }
        }
    }

    Save and exit.

  4. Launch your application with Docker Compose:

    Now, from within your my-nginx-app directory, run:

    docker-compose up -d
    • up: Builds, creates, starts, and attaches to containers for a service.
    • -d: Runs containers in detached mode (in the background).

    You’ll see output indicating Docker Compose pulling the Nginx image and creating the container.

  5. Verify the running services:

    Check if your container is running:

    docker-compose ps

    You should see web service with Up status.

  6. Access the Nginx server:

    Open your web browser and go to http://localhost. You should see the message: “Hello from Nginx in Docker Compose! 🎉”.

    Alternatively, use curl from your terminal:

    curl http://localhost

    You should get the same output.

  7. Stop and remove the application:

    When you’re done, you can stop and remove the containers, networks, and volumes defined in your docker-compose.yml file:

    docker-compose down

    This command cleans up all the resources created by your Docker Compose setup.


Common Issues & Troubleshooting 🐛

Here are a few common issues you might encounter and how to resolve them:

  1. docker: command not found or docker-compose: command not found:

    • Reason: The executable might not be in your system’s PATH, or the installation was incomplete.
    • Solution:
      • For Docker, ensure you followed all installation steps, especially the repository setup.
      • For Docker Compose, double-check that /usr/local/bin is in your PATH (it usually is by default). Verify the download path and filename. Re-run sudo chmod +x /usr/local/bin/docker-compose.
  2. Got permission denied while trying to connect to the Docker daemon socket:

    • Reason: Your user isn’t part of the docker group, or the group membership hasn’t taken effect.
    • Solution: Re-run sudo usermod -aG docker $USER. Then, log out and log back in, or run newgrp docker in your current terminal session.
  3. Containers not starting or showing errors:

    • Reason: Incorrect configuration, insufficient resources, or conflicts.
    • Solution:
      • Check Docker logs: `docker logs ` (find container ID/name with `docker ps -a`).
      • Check Docker Compose logs: `docker-compose logs `.
      • Try running in foreground (without -d): docker-compose up. This will show logs directly in your terminal.
      • Ensure no other service is already using the ports you’re trying to map.
  4. Network connectivity issues (e.g., localhost doesn’t work):

    • Reason: Firewall blocking traffic, or incorrect port mapping.
    • Solution:
      • Check your firewall (UFW on Ubuntu). If UFW is enabled, you might need to allow traffic on the ports your containers are using. For example, sudo ufw allow 80/tcp.
      • Verify the ports mapping in your docker-compose.yml ("HOST_PORT:CONTAINER_PORT").

Conclusion 🎉

You’ve done it! You now have Docker and Docker Compose successfully installed on your Ubuntu system, giving you the power to create, manage, and deploy containerized applications with ease. This setup is a cornerstone of modern development workflows, promoting consistency, collaboration, and efficiency.

What’s next?

  • Explore Docker Hub: Discover thousands of ready-to-use Docker images.
  • Learn Dockerfile: Write your own custom Docker images.
  • Dive deeper into Docker Compose: Master advanced features like networking, volumes, and environment variables for complex applications.

Happy containerizing! If you found this guide helpful, feel free to share it with your fellow developers! 🚀👩‍💻👨‍💻

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다