Install Docker and Docker Compose on Ubuntu 22.04

Install Docker and Docker Compose on Ubuntu 22.04

Spread the love

This post will talk you through the process of installing Docker and Docker Compose on Ubuntu 22.04. See our Ubuntu 22.04 section for instructions on how to install Ubuntu 22.04 if you have not already done so. Docker/ Docker Compose can be installed on either Ubuntu 22.04 Desktop or Ubuntu 22.04 Server the installation process is the same.

Refresh Repositiores

The first step is to refresh the the apt repositories by running:

sudo apt update
sudo apt upgrade -y

Install Dependencies

To install the prerequisite’s needed to install Docker run the following command:

sudo apt install -y ca-certificates curl gnupg lsb-release

Install Docker on Ubuntu 22.04

With the requirements installed, the next step is to install Docker. We will install the Docker Community Edition ( Docker  CE ) which is opensource and free to download and use.

To do so, we will add the GPG key by running:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Now we can install the docker repository by running:

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

Run the following command to install docker:

 sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

Verify the installation by running:

sudo docker run hello-world

If all components were configured correctly and the setup proceeded without issues, you should observe the following output:

Install Docker and Docker Compose on Ubuntu 22.04
Install Docker and Docker Compose on Ubuntu 22.04 1

Configure Docker to run as a non-root user

In order to execute Docker commands, root privileges are required, which is why you must prefix your commands with “sudo.” However, if you prefer to run Docker as a user without root privileges, you’ll need to establish a Docker group. This group will allow specified users to interact with Docker without the need for elevated permissions.

Create a group named “docker” that you can assign users to with the following command:

sudo groupadd docker

The group will probably already exist and you will recieve the following message:

Install Docker and Docker Compose on Ubuntu 22.04
Install Docker and Docker Compose on Ubuntu 22.04 2

Add any user you need to the group by running the following command: Change ith2 to your users name:

sudo usermod -aG docker ith2

Install Docker Compose

For more information on what Docker Compose is and why you would want it take a look at the Docker Website.

To install Docker Compose run the following commands:

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

You will get an output similar to:

Install Docker and Docker Compose on Ubuntu 22.04
Install Docker and Docker Compose on Ubuntu 22.04 3

Verify that the Docker Compose install was successful by running:

docker-compose --version

This will give you an output similar to

Install Docker and Docker Compose on Ubuntu 22.04
Install Docker and Docker Compose on Ubuntu 22.04 4

Config Before using

Before using the VM I like to make the file structure used for Docker Compose as easy as possible to sync to GitHub when it is setup so I make a folder in / with the machines name then I create all docker compose container folders within that folder so that you can use git to keep all container configs synced with GitHub easily and if you need to redeploy to a different VM or automate it with Ansible this can easily be done (Posts on this to come later)

To create the folder go to the / folder by running:

cd /

Make new folder by running this command swapping SERVERNAME for the actual server name:

mkdir SERVERNAME

So for my server I ran:

mkdir ith2-dkr-01

And I now have a folder called /ith2-dkr-01:

Install Docker and Docker Compose on Ubuntu 22.04
Install Docker and Docker Compose on Ubuntu 22.04 5

Docker and Docker Compose are now installed and ready to go.

To find ideas of what to install using Docker and Docker Compose go to our Docker/DockerCompose section.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top