January 8, 2024 5 minutes minutes read Admin

Install Docker and Docker Compose on Ubuntu 22.04 with Just a Few Command Lines

A simplified guide to installing Docker and Docker Compose on Ubuntu 22.04.

TL;DR

sudo -v
sudo apt-get -y install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] 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 -y update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER
exec su -l $USER
docker --version
docker run hello-world

Steps

First, let’s get Docker itself installed. Here’s the quick and dirty way:

  1. Prepare Your System Open your terminal and ensure you have permission to run commands with sudo.

    sudo -v
  2. Install Prerequisites Before you can install Docker, you need a few prerequisites. Run:

    sudo apt-get -y install ca-certificates curl gnupg
  3. Add Docker’s Official GPG Key This step ensures the software you're about to install is authentic:

    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
  4. Add the Docker Repository This snippet uses your system architecture and version to add the right repository:

    echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  5. Install Docker Engine Update your package index and install Docker Engine, along with some useful plugins:

    sudo apt-get -y update
    sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  6. Add User to Docker Group Avoid needing sudo every time you run a Docker command:

    sudo usermod -aG docker $USER
    exec su -l $USER
  7. Verify Installation Check that Docker is installed correctly by running:

    docker --version
    docker run hello-world

    If you see a message from Docker, you’re good to go!

Installing Docker Compose on Ubuntu 22.04

TL;DR

Check the Docker Compose GitHub page for the latest release and update the version number in your command accordingly.

mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
docker compose version

Steps

Now, for Docker Compose, which lets you manage multi-container Docker applications:

  1. Create Directory for Docker CLI Plugins

    mkdir -p ~/.docker/cli-plugins/
  2. Download and Install Docker Compose Check the Docker Compose GitHub page for the latest release and update the version number in your command accordingly. As of now, the command is:

    curl -SL https://github.com/docker/compose/releases/download/v2.26.1/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
    chmod +x ~/.docker/cli-plugins/docker-compose
  3. Verify Installation Check that Docker Compose is correctly installed:

    docker compose version

    If it spits out a version number, you’ve mastered the art of installation!

And there you have it! Docker and Docker Compose are installed, and you’re ready to containerize everything from your pet projects to your breakfast (note: do not attempt to containerize your breakfast). Keep tinkering, keep exploring, and remember—the command line is your gateway to greatness… or at least to your next Docker adventure.