DEV Community

abbazs
abbazs

Posted on

How to install the latest Docker and Docker Compose on Debian-based Linux systems?

How to install the latest Docker and Docker Compose on Debian-based Linux systems

In this tutorial, we will install Docker and Docker Compose on Ubuntu. We will first install Docker and then install Docker Compose.

Installing Docker

  1. Update the apt package index and install aptitude:
   sudo apt-get update
   sudo apt-get install aptitude
Enter fullscreen mode Exit fullscreen mode
  1. Install required packages for apt to use HTTPS:
   sudo aptitude update
   sudo aptitude install \
   ca-certificates \
   curl \
   gnupg
Enter fullscreen mode Exit fullscreen mode
  1. Add the Docker GPG key to the keyring:
   curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
   sudo chmod a+r /usr/share/keyrings/docker.gpg
Enter fullscreen mode Exit fullscreen mode
  1. Add the Docker repository to the apt sources list:
   echo \
   "deb [arch="$(dpkg --print-architecture)" signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
   $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
   sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Enter fullscreen mode Exit fullscreen mode
  1. Update the apt package index again and install Docker:
   sudo aptitude update
   sudo aptitude install docker-ce docker-ce-cli containerd.io
Enter fullscreen mode Exit fullscreen mode

Adding user to the docker group

  1. Create the docker group:
   sudo groupadd docker
Enter fullscreen mode Exit fullscreen mode
  1. Add your user to the docker group:
   sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode
  1. Activate the changes to groups:
   newgrp docker
Enter fullscreen mode Exit fullscreen mode
  1. Verify that you can run Docker commands without sudo:
   docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Installing Docker Compose

  1. Download the Docker Compose binary:
   sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode
  1. Apply executable permissions to the binary:
   sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation by checking the version of Docker Compose:
   docker-compose --version
Enter fullscreen mode Exit fullscreen mode

This should output the version number of the latest Docker Compose.

You have now installed Docker and Docker Compose on Debian-based Linux systems.

Automatic installation

Here's a script that automates the installation of Docker and Docker Compose on Debian-based Linux systems, including adding the current user to the docker group:

Copy the following script into a file, for example, docker-install.sh, make it executable with chmod +x docker-install.sh, and then run it with ./docker-install.sh. This will install Docker and Docker Compose, add the current user to the docker group, and verify the installation of both tools.

#!/bin/bash

# Install Docker and Docker Compose on Debian-based Linux systems
# Include the current user in the docker group

# Install Docker
sudo apt-get update
sudo apt-get install aptitude -y
sudo aptitude update
sudo aptitude install -y \
    ca-certificates \
    curl \
    gnupg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
sudo chmod a+r /usr/share/keyrings/docker.gpg
echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
    $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo aptitude update
sudo aptitude install -y docker-ce docker-ce-cli containerd.io

# Add the current user to the docker group
sudo groupadd docker
sudo usermod -aG docker $USER

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \
    -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Verify the installation
docker --version
docker compose --version
Enter fullscreen mode Exit fullscreen mode

Top comments (0)