How to Install and Use Docker on Ubuntu 20.04 / 20.10

Docker is a free and open source tool designed to build, deploy, and run applications inside containers. Host on which docker is installed is known docker engine. To work the docker engine smoothly, docker daemon service must always be running. For the applications where multiple containers are used then with the help of docker compose these containers are spin up as a service.

In this guide, we will demonstrate how to install and use Docker on Ubuntu 20.04 /20.10 and will also learn about docker compose installation and its usage.

Prerequisites

  • Ubuntu 20.04 / 20.10 along with ssh access
  • Sudo user with privilege rights
  • Stable Internet Connection

Let’s dive into Docker installation steps on Ubuntu 20.04 /20.10

Step 1) Install prerequisites packages for docker

Login to Ubuntu 20.04 /20.10 system and run the following apt commands to install docker dependencies,

$ sudo apt update
$ sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Step 2) Setup docker official repository

Though the docker packages are available in default Ubuntu 20.04 /20.10 repositories but it is recommended to use docker official repository. To enable docker repository run below commands,

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable"

Step 3) Install docker with apt command

Now, we are all set to install latest and stable version of docker from its official repository. Run the beneath to install it

$ sudo apt-get update
$ sudo apt install docker-ce -y

Once the docker package is installed, add your local user to docker group by running following command:

$ sudo usermod -aG docker pkumar

Note: Make sure logout and login again after adding local user to docker group

Verify the Docker version by executing following,

$ docker version

Output of above command would be:

Check-Docker-Version-Ubuntu

Verify whether docker daemon service is running or not by executing below systemctl command,

$ sudo systemctl status docker

docker-service-status-ubuntu

Above output confirms that docker daemon service is up and running.

Step 4) Verify docker Installation

To test and verify docker installation, spin up a ‘hello-world’ container using below docker command.

$ docker run hello-world

This docker command will download ‘hello-world’ container image and then will spin up a container. If container displays the informational message, then we can say docker installation is successful.  Output of above ‘docker run’ would look like below.

docker-run-hello-world-container-ubuntu

Installation of Docker Compose on Ubuntu 20.04 / 20.10

To install docker compose on Ubuntu Linux, execute the following commands one after the another

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.28.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

Check the docker-compose version by running following command,

$ docker-compose --version
docker-compose version 1.28.4, build cabd5cfb
$

Perfect, above output confirms that docker compose of version 1.28.4 is installed.

Test Docker Compose Installation

To test docker compose, let’s try to deploy WordPress using compose file. Create a project directory ‘wordpress’ using mkdir command.

$ mkdir wordpress ; cd wordpress

Create a docker-compose.yaml file with following contents.

$ vi docker-compose.yaml
version: '3.3'

services:
   db:
     image: mysql:latest
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: [email protected]#
       MYSQL_DATABASE: wordpress_db
       MYSQL_USER: dbuser
       MYSQL_PASSWORD: [email protected]#
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: dbuser
       WORDPRESS_DB_PASSWORD: [email protected]#
       WORDPRESS_DB_NAME: wordpress_db
volumes:
    db_data: {}

Save and close the file.

docker-compose-file-wordpress

As we can see, we have used two containers one for WordPress web and other one is for Database. We are also creating the persistent volume for DB container and WordPress GUI is exposed on ‘8000’ port.

To deploy the WordPress, run the below command from your project’s directory

$ docker-compose up -d

Output of above command would like below:

docker-compose-wordpress-ubuntu

Above confirms that two containers are created successfully. Now try to access WordPress from the Web Browser by typing URL:

http://<Server-IP-Address>:8000

Wordpress-installation-via-docker-compose-ubuntu

Great, above confirms that WordPress installation is started via docker-compose. Click on Continue and follow screen instructions to finish the installation.

That’s all from this guide. I hope you found this guide informative, please don’t hesitate to share your feedback and comments.

For more documentation on docker please refer : Docker Documentation

Also Read : How to Setup Local APT Repository Server on Ubuntu 20.04

Also Read : How to Setup Traefik for Docker Containers on Ubuntu 20.04

Tags:

2 thoughts on “How to Install and Use Docker on Ubuntu 20.04 / 20.10”

  1. What about security after this installation? Docker will change iptables rules as he likes, and ufw will not work anymore.

    1. Hi Andrei,

      UFW will work with Docker iptables but for that you need modify ‘/etc/ufw/after.rules’.

      Add the following lines at end of file and replace the interface that suits to your setup and restart ufw service

      :DOCKER-USER – [0:0]
      :ufw-user-input – [0:0]

      -A DOCKER-USER -m conntrack –ctstate RELATED,ESTABLISHED -j ACCEPT
      -A DOCKER-USER -m conntrack –ctstate INVALID -j DROP
      -A DOCKER-USER -i enp0s3 -j ufw-user-input
      -A DOCKER-USER -i enp0s3-j DROP
      COMMIT

Leave a Reply

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