How to Set Up Docker and Compose on Ubuntu 24.04 in a Simple Way

Introduction

Recently, more and more open-source tools require Docker as a prerequisite for installation. By using Docker, it becomes easy to recreate environments and set them up efficiently, making it a convenient choice for many engineers.

This guide will show you how to install Docker and Docker Compose on Ubuntu 24.04. The article is based on the official installation procedure, and by following the necessary commands, you can easily complete the setup.

However, Docker and Docker Compose are frequently updated, so it’s always recommended to check the official documentation for the latest instructions.

Latest Installation Instructions

The method to install Docker is explained in detail on the official documentation. The following page includes installation instructions for various Linux distributions.

Official Docker Installation Guide

For this guide, we’ll be focusing on installing Docker on Ubuntu. Please refer to the following page:

Ubuntu-specific Docker Installation Instructions

Adding Docker’s Official GPG Key and Setting up the Repository

To install Docker, you first need to add the official GPG key and set up Docker’s repository on Ubuntu. Doing this ensures you are able to obtain the latest version of Docker.

To install Docker, you’ll need to prepare your system. First, update the system and install the necessary tools.

Update your system and install ca-certificates and curl.

sudo apt-get update
sudo apt-get install ca-certificates curl

Next, add Docker’s official GPG key. This allows you to safely install Docker from the official repository.

sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Finally, add Docker’s repository to your system. This will allow you to download Docker packages.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Installing Docker

Now, install Docker along with the necessary components. This will install Docker itself, the command-line tools, container runtime, build plugins, and Docker Compose plugin all at once.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Once the installation is complete, you can verify that Docker has been installed correctly by running the following command:

sudo docker --version

Running Docker Commands Without sudo

It can be inconvenient to type sudo before every Docker command. Follow these steps to configure Docker so you can run it without sudo.

  1. Add the current user to the Docker group
    This will allow you to run Docker commands without needing sudo.
sudo gpasswd -a $(whoami) docker
  1. Change the permissions of Docker’s socket file
    This step allows the current user to access the file used to communicate with Docker.
sudo chown $(whoami):$(whoami) /var/run/docker.sock

Now you can run Docker commands such as docker ps without using sudo. However, adding a user to the Docker group grants them extensive permissions, so be cautious from a security perspective.

Installation Checklist

  • [ ] Installed necessary packages.
  • [ ] Added Docker’s repository.
  • [ ] Installed Docker and related tools.

The Evolution of Installation Methods

The process for installing Docker has become simpler and faster over time. As technology evolves, the installation steps have been continuously refined to allow developers to work more efficiently.

This section includes older installation methods for reference, but we strongly recommend using the latest instructions. The newer methods offer greater security and reliability.

Why Use the Latest Instructions?

Technology is constantly advancing, and Docker installation procedures are no exception. Using the latest instructions ensures you benefit from enhanced security and access to new features. While older methods are available for reference, they are typically only useful in specific cases, so it’s best to stick with the current methods suited to your environment.

Tips for Using Older Methods

Older installation methods can be useful in specific environments or when working with older versions. For example, if you need to use an older version of Docker or replicate a specific setup process, the historical methods can serve as a reference.

However, keep in mind that these methods are often deprecated and no longer recommended.

Docker Compose is Included

With the current installation method, Docker Compose is installed along with Docker itself. This means you can immediately start managing multiple containers without additional steps.

Here’s the Past Installation Method

When trying to install useful open-source tools, Docker is increasingly becoming a prerequisite. So, let’s install Docker and Docker Compose on Ubuntu 20.04. However, this guide is based on simply copying the official instructions.

Official Docker Installation Guide (Japanese)

Installing Necessary Packages for Docker

To install Docker, first, install the necessary packages.

sudo apt-get update
sudo apt-get install ca-certificates curl

Next, add the GPG key for the official Docker repository.

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Now, set up the stable repository.

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

Update the system and install Docker.

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

The Docker repository has been added through the update.

root@ip-172-31-7-75:~# apt-get update
Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 https://packages.grafana.com/oss/deb stable InRelease
Get:5 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [15.5 kB]
Fetched 187 kB in 1s (306 kB/s)
Reading package lists… Done

root@ip-172-31-7-75:~# apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
docker-ce-rootless-extras docker-scan-plugin pigz slirp4netns
Suggested packages:
aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras
docker-scan-plugin pigz slirp4netns
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 96.4 MB of archives.
After this operation, 405 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 slirp4netns amd64 0.4.3-1 [74.3 kB]
Get:3 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.5.11-1 [22.9 MB]
Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:20.10.14~3-0~ubuntu-focal [41.0 MB]
Get:5 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:20.10.14~3-0~ubuntu-focal [20.9 MB]
Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-rootless-extras amd64 5:20.10.14~3-0~ubuntu-focal [7932 kB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-scan-plugin amd64 0.17.0~ubuntu-focal [3521 kB]
Fetched 96.4 MB in 2s (40.1 MB/s)
Selecting previously unselected package pigz.
(Reading database … 127271 files and directories currently installed.)
Preparing to unpack …/0-pigz_2.4-1_amd64.deb …
Unpacking pigz (2.4-1) …
Selecting previously unselected package containerd.io.
Preparing to unpack …/1-containerd.io_1.5.11-1_amd64.deb …
Unpacking containerd.io (1.5.11-1) …
Selecting previously unselected package docker-ce-cli.
Preparing to unpack …/2-docker-ce-cli_5%3a20.10.14~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce-cli (5:20.10.14~3-0~ubuntu-focal) …
Selecting previously unselected package docker-ce.
Preparing to unpack …/3-docker-ce_5%3a20.10.14~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce (5:20.10.14~3-0~ubuntu-focal) …
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack …/4-docker-ce-rootless-extras_5%3a20.10.14~3-0~ubuntu-focal_amd64.deb …
Unpacking docker-ce-rootless-extras (5:20.10.14~3-0~ubuntu-focal) …
Selecting previously unselected package docker-scan-plugin.
Preparing to unpack …/5-docker-scan-plugin_0.17.0~ubuntu-focal_amd64.deb …
Unpacking docker-scan-plugin (0.17.0~ubuntu-focal) …
Selecting previously unselected package slirp4netns.
Preparing to unpack …/6-slirp4netns_0.4.3-1_amd64.deb …
Unpacking slirp4netns (0.4.3-1) …
Setting up slirp4netns (0.4.3-1) …
Setting up docker-scan-plugin (0.17.0~ubuntu-focal) …
Setting up containerd.io (1.5.11-1) …
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-ce-cli (5:20.10.14~3-0~ubuntu-focal) …
Setting up pigz (2.4-1) …
Setting up docker-ce-rootless-extras (5:20.10.14~3-0~ubuntu-focal) …
Setting up docker-ce (5:20.10.14~3-0~ubuntu-focal) …
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for man-db (2.9.1-1) …
Processing triggers for systemd (245.4-4ubuntu3.15) …

Managing Docker as a Non-root User and Setting Docker to Start Automatically

If you want to manage Docker as a non-root user, you need to create a group and add the user to it. However, since we are running as root, we will skip this step.

To enable Docker to start automatically when the system boots, run the following commands:

systemctl enable docker.service
systemctl enable containerd.service

Installing Docker Compose

Next, we will install Docker Compose. Refer to the official installation guide here:

Docker Compose Installation Guide

You can install Docker Compose by downloading the latest version to the specified directory:

curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Here is an example of the download process:

root@ip-172-31-7-75:~# curl -L https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 5398 0 --:--:-- --:--:-- --:--:-- 5398
100 25.2M 100 25.2M 0 0 41.4M 0 --:--:-- --:--:-- --:--:-- 41.4M

The latest version can always be found on GitHub. Make sure to check the link and update the version number accordingly.

Docker Compose Releases on GitHub

Granting Execution Permission to the Binary

To grant execution permissions to the Docker Compose binary, run the following command:

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

Checking the Version

To check the installed version of Docker Compose, use the following command:

docker-compose --version

Example output:

root@ip-172-31-7-75:~# docker-compose --version
Docker Compose version v2.4.1
Please share if you like it!
TOC