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

Search for a command to run...
Docker and docker-compose

while installing it i am getting this, gaurav@LAPTOP-4KT5PNO7:~$ sudo service docker restart
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
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.
First, we need to uninstall the older version of 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
There are many ways to install docker, but we are going to use the recommended repository method.
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
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo service docker start
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
sudocommand. It is not recommended running docker withsudo. To run docker withoutsudowe will add our current user to docker group, that way we can run docker withoutsudo
To create the docker group and add your user:
sudo groupadd docker
sudo usermod -aG docker $USER
Now log out and log in again and try to run hello-world image without sudo.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.1is 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
We will be deploying containous/whoami container with docker and docker-compose for testing
containous/whoami containerdocker run -d -p 8080:80 --name whoami containous/whoami
docker ps
If you get something like below image then you are on right path
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"}
docker ps command.docker stop b1b7e7b79c89
docker rm b1b7e7b79c89
Change
b1b7e7b79c89with your container ID.
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 ๐๐