How to install Docker and Docker Compose on Ubuntu

Chao Geng
4 min readMar 25, 2024

--

What is Docker

Docker is a software platform that allows you to build, test, and deploy applications quickly using containers.

Install Docker

Prerequisites

Before we can start installing docker and docker-compose we need to make sure that Ubuntu machine is installed and up to date. We are using Ubuntu 22.04.3 LTS.

# lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy

In order to run Docker commands, root privileges are required. Here we prefer to run Docker as a user with root privileges. SSH into your Ubuntu machine and sign in as root. Or add “sudo” when running command.

sudo -i

Update your system by running the following commands:

apt update
apt upgrade -y

Step 1 — Run installation script

Install script is for a convenience for quickly installing the latest Docker-CE releases on the supported systems. You can do this:

curl https://get.docker.com | sh

Like the following output:

# curl https://get.docker.com | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21927 100 21927 0 0 33399 0 --:--:-- --:--:-- --:--:-- 33374
...
+ sh -c apt-get update -qq >/dev/null
W: https://packages.gitlab.cn/repository/ubuntu-jammy/dists/jammy/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sh -c install -m 0755 -d /etc/apt/keyrings
...
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin >/dev/null
+ sh -c docker version
Client: Docker Engine - Community
Version: 26.0.0
API version: 1.45
Go version: go1.21.8
Git commit: 2ae903e
Built: Wed Mar 20 15:17:48 2024
OS/Arch: linux/amd64
Context: default

Server: Docker Engine - Community
Engine:
Version: 26.0.0
API version: 1.45 (minimum version 1.24)
Go version: go1.21.8
Git commit: 8b79278
Built: Wed Mar 20 15:17:48 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.28
GitCommit: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0

================================================================================
...

Step 2— Start service

You can start docker service by the following:

# systemctl start docker
# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2024-03-24 18:39:02 CST; 10min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 3613665 (dockerd)
Tasks: 38
Memory: 35.5M
CPU: 890ms
CGroup: /system.slice/docker.service
└─3613665 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Step 3— Enable service

Enabling a service means it will start at boot. You can do this:

# systemctl enable docker
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker

Install Docker Compose

Docker Compose is a tool you can use to define and share multi-container applications. Why use Compose?

Using Docker Compose offers several benefits that streamline the development, deployment, and management of containerized applications:

  • Simplified control: Docker Compose allows you to define and manage multi-container applications in a single YAML file. This simplifies the complex task of orchestrating and coordinating various services, making it easier to manage and replicate your application environment.
  • Efficient collaboration: Docker Compose configuration files are easy to share, facilitating collaboration among developers, operations teams, and other stakeholders. This collaborative approach leads to smoother workflows, faster issue resolution, and increased overall efficiency.
  • Rapid application development: Compose caches the configuration used to create a container. When you restart a service that has not changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to your environment very quickly.
  • Portability across environments: Compose supports variables in the Compose file. You can use these variables to customize your composition for different environments, or different users.
  • Extensive community and support: Docker Compose benefits from a vibrant and active community, which means abundant resources, tutorials, and support. This community-driven ecosystem contributes to the continuous improvement of Docker Compose and helps users troubleshoot issues effectively.

Successful to install docker, you will have function compose.

# docker compose version
Docker Compose version v2.21.0

But, it is not the latest version. You can install the latest version by running the following:

# curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o ./docker-compose
# chmod +x ./docker-compose
# ./docker-compose version
Docker Compose version v2.26.0

So you already have the latest docker and docker-compose.

--

--