Install Docker and docker-compose

Published: Aug. 19, 2022

Docker is an open-source project for containerising applications to be portable and self-sufficient.

Install Docker

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add Docker's official GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Use the following command to set up the stable repository

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

Install the latest version of Docker Engine and containerd

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

Verify that Docker Engine is installed correctly by running the hello-world image.

sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.

Manage Docker as a non-root user

The Docker daemon always runs as the root user i.e., you can access it using sudo. If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it.

Create the docker group,

sudo groupadd docker

Add your user to the docker group,

sudo usermod -aG docker $USER

Log out and log back in for the changes to take effect.

Verify that you can run docker commands without sudo.

docker run hello-world

Reference: Official documentation

Docker-compose

Compose is a tool for managing multi-container Docker applications using a YAML configuration file.

You can install Compose V2 by downloading the appropriate binary for your system and copying it into $HOME/.docker/cli-plugins as docker-compose.

Download the current stable release of Docker compose.

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
 $ mkdir -p $DOCKER_CONFIG/cli-plugins
 $ curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose

Apply executable permissions to the binary,

chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

Test your installation

docker compose version

Reference: Official documentation

Old version (compose v1.x): Official documentation

Last updated: Aug. 19, 2022, 4:01 p.m.