How to install Docker Compose V2 on Ubuntu 22.04

Docker is a set of platform-as-a-service product that uses OS-level virtualization to deliver software in packages called containers. Containers are usually isolated from one another and bundled with their own software libraries and configuration files, they can communicate with each other through well-defined channels.

Docker makes it possible to get more apps running on the same old servers and also makes it easy to package and ship programs.

Docker-compose is a tool for defining and running multi-container Docker application. It uses YAML files to configure its application services.

Steps on using compose

  • First define your app’s environment with Dockerfile so that it can be reproduced anywhere.
  • Define the services that make up your app in docker-compose.yaml so they can be run together in an isolated environment.
  • Lastly run docker compose up to start and run your entire app.

Prerequisites

  • Have Docker up and running on your Ubuntu 22.04.
  • Make sure you are running on sudo privileges
  • Ubuntu 22.04 server up and running.

Installing Docker compose v2 on Ubuntu 22.04

The difference between docker compose v1 and v2 is that v1 uses hyphen when running commands. On v2 they remove that hyphen between different commands. For example is: for docker v1 docker-compose up but for v2 we use docker compose up

Update system repositories

Begin by updating and upgrading your system repositories in order to make them up to date.

sudo apt update && apt upgrade -y

Install Docker on Ubuntu 22.04

As a prerequisite, we need to have docker installed on our system before we can continue with the docker-compose installation.

Let’s first begin by installing some prerequisites on our system

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Then we need to add a GPG key into our system repository to validate the docker images.

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

Next is to add the Docker repository to apt so that docker will get to know where to access it resources files.

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

Lastly, let’s do a system update once more to refresh our repositories.

sudo apt update

we can now install docker in Ubuntu 22.04 with this command sudo apt install docker-ce but first, we can check if docker is available in our repositories with this command.

$ apt-cache policy docker-ce
docker-ce:
  Installed: (none)
  Candidate: 5:20.10.15~3-0~ubuntu-jammy
  Version table:
     5:20.10.15~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.14~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.13~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

Notice we are installing docker-ce, this is the community version not the enterprise version.

Install docker

To install docker run this command on your terminal.

sudo apt install docker-ce -y

You will see this output.

The following additional packages will be installed:
  containerd.io docker-ce-cli docker-ce-rootless-extras docker-scan-plugin
  libslirp0 pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras
  docker-scan-plugin libslirp0 pigz slirp4netns
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 101 MB of archives.
After this operation, 421 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Verify that the docker engine is running with the hello-world command.

sudo docker-run hello world

Installing Docker compose on Ubuntu 22.04

Now it’s time we install docker-compose on our system.

First, let’s create a directory in the home folder

mkdir -p ~/.docker/cli-plugins/

Next is to download the latest stable release from docker releases page.

curl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

Make sure you give the right permission so that you will be in a position to execute docker-compose. Use this command to give requisite permission.

chmod +x ~/.docker/cli-plugins/docker-compose

You can check the version of docker compose installed.

$ docker compose version
Docker Compose version v2.5.0

Create docker-compose.yml file

mkdir ~/demo
cd ~/demo
nano docker-compose.yml

Paste the following into docker-compose.yml file

version: '3.9'

services:
  redis:
    image: redis:6.2-alpine
    ports:
      - 6379:6379
    command: redis-server --save 60 1 --requirepass MDNcVb924a --loglevel warning

To start the container engine we use docker compose up -d

$ docker compose up -d 
[+] Running 7/7
 ⠿ redis Pulled                                                                12.7s
   ⠿ df9b9388f04a Pull complete                                                 3.0s
   ⠿ 192e03523482 Pull complete                                                 3.1s
   ⠿ 7151bccd2756 Pull complete                                                 3.3s
   ⠿ 16ca30b6b4df Pull complete                                                 6.3s
   ⠿ b8157a69ff12 Pull complete                                                 6.3s
   ⠿ 02fb42b94dab Pull complete                                                 6.4s
[+] Running 2/2
 ⠿ Network demo_default    Created                                              0.1s
 ⠿ Container demo-redis-1  Started 

Verify the container processes using:

$ docker compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
demo-redis-1        "docker-entrypoint.s…"   redis               running             0.0.0.0:6379->6379/tcp, :::6379->6379/tcp

Let’s login to our Redis container using the following command.

/data # redis-cli
127.0.0.1:6379> keys pattern
(error) NOAUTH Authentication required.
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth MDNcVb924a
OK
127.0.0.1:6379> keys pattern
(empty array)
127.0.0.1:6379> 

Uninstall Docker Engine

When you are done experimenting with the docker engine you can install using the following command.

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin

To delete images, containers, volumes, or customized config files you need to delete them manually by using the following commands.

$ sudo rm -rf /var/lib/docker
$ sudo rm -rf /var/lib/containerd

Conclusion

We have successfully installed Docker compose v2 on Ubuntu 22.04.

About Mason Kipward

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Mason Kipward →

Leave a Reply

Your email address will not be published.