Installing Docker and docker-compose in Ubuntu 20.04 and WSL

Installing Docker and docker-compose in Ubuntu 20.04 and WSL

Docker and docker-compose

ยท

3 min read

Introduction

Docker is an open-source containerization platform that developers use to build, run, and package applications for deployment using containers. Unlike virtual machines, Docker containers offer: OS-level abstraction with optimum resource utilization. Interoperability

Prerequisites

  • Ubuntu with 20.04 or WSL environment

  • Nothing else ๐Ÿ˜€

Althought this guide is for Ubuntu 20.04 and WSL , it should work for all other version of Ubuntu also.

Installing Docker

First, we need to uninstall the older version of docker.

Uninstalling Docker

sudo apt-get remove docker docker-engine docker.io containerd runc

It's ok if you get output like below.

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package docker-engine

Installing Methods

There are many ways to install docker, but we are going to use the recommended repository method.

Set up repository

  • Update apt package and install necessary tools
    sudo apt-get update
    sudo apt-get install  ca-certificates  curl  gnupg  lsb-release
    
  • Add Dockerโ€™s official GPG key

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

    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
    

Install Docker engine

  • Update apt package and install docker engine
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io
    
  • Start Docker service
    sudo service docker start
    
  • Verify if Docker is installed correctly
    sudo docker run hello-world
    
    If you get something like Hello from Docker! This message shows that your installation appears to be working correctly. that's mean you have installed docker.

Notice how you have to run docker with sudo command. It is not recommended running docker with sudo. To run docker without sudo we will add our current user to docker group, that way we can run docker without sudo

To create the docker group and add your user:

  • Create the docker group.
    sudo groupadd docker
    
  • Add your user to the docker group.
    sudo usermod -aG docker $USER
    
    Now log out and log in again and try to run hello-world image without sudo.

Installing docker-compose

  • We will download docker-compose from repository and manually installing it.
    sudo curl -L "https://github.com/docker/compose/releases/download/v2.1.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    

At the time of writing docker-compose v2.1.1 is latest. You can check latest version and modify above command.

  • Add appropriate execution permission to docker compose

    sudo chmod +x /usr/local/bin/docker-compose
    
  • Verify docker-compose installation

    docker-compose version
    

Deploying sample service

We will be deploying containous/whoami container with docker and docker-compose for testing

Deploying using docker

  • Run below command to deploy containous/whoami container
    docker run -d -p 8080:80 --name whoami containous/whoami
    
  • Now check if our container is running
    docker ps
    
    If you get something like below image then you are on right path

carbon.png

  • Now we will send api request to this container
    curl  localhost:8080/api
    
    You should get something like this
    {"hostname":"b1b7e7b79c89","ip":["127.0.0.1","172.17.0.2"],"headers":{"Accept":["*/*"],"User-Agent":["curl/7.68.0"]},"url":"/api","host":"localhost:8080","method":"GET"}
    
  • Let's remove this container. First get your container id using docker ps command.
    docker stop b1b7e7b79c89
    docker rm b1b7e7b79c89
    

    Change b1b7e7b79c89 with your container ID.

Deploying using docker-compose

Now we will deploy same container using docker-compose.

  • Create docker-compose.yaml

    touch docker-compose.yaml
    
  • edit docker-compose.yaml

    nano docker-compose.yaml
    

    Now paste below content

    version: "3.3"
    services:
    whoami:
      image: "containous/whoami"
      container_name: "whoami"
      ports:
       - 8080:80
    
  • Now run docker-compose

    docker-compose up -d
    

    Verify our container is running or not using docker ps

  • Send api request using curl

    curl  localhost:8080/api
    

    You should see a reply similar to above. This means our container is running correctly.

  • Stop container.

    docker-compose down
    

That's it, you have successfully installed Docker and docker-compose ๐ŸŽŠ๐Ÿ’

ย