How to install Docker and docker-compose on Ubuntu

Posted on 4 Feb 2021

Cargo shipping containers at sea during the day

Photo by Ian Taylor

Installing Docker Engine

Prerequisites

To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:

  • Ubuntu 20.04 LTS (Focal Fossa)
  • Ubuntu 18.04 LTS (Bionic Beaver)
  • Ubuntu 16.04 LTS (Xenial Xerus)

Setup The Repository

  1. 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 \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common -y
    
  2. Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  3. Add the Docker repository to APT sources:

    sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    

Install Docker Engine

Update the apt package index, and install the latest version of Docker Engine and containerd.

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

Verify Docker Engine Installation

Run docker command to verify Docker Engine installation.

# List all available command
docker

# View installed Docker Engine version
docker --version

Manage Docker as a non-root user

By default docker can only run using the sudo command. If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it.

  1. Create the docker group.

    sudo groupadd docker
    

    You can ignore if there is a message showing that group docker already exists.

  2. Add your user to the docker group.

    sudo usermod -aG docker $USER
    
  3. Re-login to evaluate group membership.

    You can also run the following command to activate the changes to groups:

    newgrp docker
    
  4. Verify that you can run docker commands without sudo.

    docker run --name hello-world hello-world
    

    You’ll see the message Hello from Docker!

  5. Delete the hello-world image.

    docker container stop hello-world
    docker container rm hello-world
    docker image rm hello-world
    

Installing Docker Compose

Download the current stable release of Docker Compose:

The current stable version of docker-compose is 1.28.2

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary:

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

Verify Docker Engine Installation

Run the docker-compose command to verify Docker Compose installation.

# List all available command
docker-compose

# View installed Docker Engine version
docker-compose --version