# Installing Docker and docker-compose in Ubuntu 20.04 and WSL

# Introduction

[Docker](https://www.docker.com/)  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


```bash
sudo apt-get remove docker docker-engine docker.io containerd runc
``` 
It's ok if you get output like below.

```bash
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**
```bash
sudo apt-get update
sudo apt-get install  ca-certificates  curl  gnupg  lsb-release
```


- **Add Docker’s official GPG key**
```bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
```

- **Add docker repository**
```bash
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**
```bash
 sudo apt-get update
 sudo apt-get install docker-ce docker-ce-cli containerd.io
```


- **Start Docker service**
```bash
sudo service docker start
```


- **Verify if Docker is installed correctly** 
```bash
 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.**
```bash
 sudo groupadd docker
```
- **Add your user to the docker group.**
```bash
 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.**
```bash
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.

%[https://github.com/docker/compose/releases/latest]

- **Add appropriate execution permission to docker compose**
```bash
sudo chmod +x /usr/local/bin/docker-compose
```

- **Verify docker-compose installation**
```bash
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**
```bash
docker run -d -p 8080:80 --name whoami containous/whoami
```
- **Now check if our container is running **
```bash
docker ps
```
If you get something like below image then you are on right path


![carbon.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1637854273256/ucJkG8qSj.png)

- **Now we will send api request to this container **
```bash
curl  localhost:8080/api
```
You should get something like this
```json
{"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.**
```bash
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`**
```bash 
touch docker-compose.yaml
```

- **edit `docker-compose.yaml`**
```bash
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**
```bash
docker-compose up -d
```
Verify our container is running or not using `docker ps`

- **Send api request using curl **
```bash
curl  localhost:8080/api
```
You should see a reply similar to above.
This means our container is running correctly.

- **Stop container.**
```bash 
docker-compose down
```

That's it, you have successfully installed Docker and docker-compose 🎊💐














