1- Docker: Installation on Ubuntu - Docker Images and Containers

I believe that it will be the best approach to explain what and how we are going to build in these DevOps series. These articles will be mostly practical, hands on experience rather than reading terminologies and explanation of concepts. We will create a Django (Python Web Framework) application with Postgresql database and serve it thru Nginx. Our ultimate goal is to run our application on Kubernetes and most importantly when a developer changes his /her code on GitHub, the updated version ofthe application should be automatically deployed on Kubernetes. 

The order of my articles will be as follows:

- Docker containers

- Docker Compose Usage (Using Nginx, Django and Postgresql)

- GitHub

- Jenkins Pipelines

- Kubernetes

- End To End CI-CD Demo using ArgoCD

When we complete all these articles, we will have a working Kubernetes Cluster on DigitalOcean Cloud Platform and when a code change is detected on GitHub repository by ArgoCD, the whole CI-CD automation will takes place. It will be a long journey I guess, let's begin.

 

Docker Containers:

Docker containers are packages of software that contain all required elements (dependencies and configuration) to run in any environment. We know that a Virtual Machine has its own OS. On the contrary, docker containers do not boot up their own Operating System, they run on top the host operating system by using docker run time. Docker virtualize the application layer. It uses host’s OS kernel because it does not have its own kernel. VMs virtualize the Application and OS layer. It has its own OS Kernel. So no matter it is Linux, Windows or Mac, our containers will run as long as there is docker runtime installed on the host machine. 

Docker images are created by using and defining dockerfile. Docker images have several layers based on the dockerfile instructions. You can roughly think each instruction in the dockerfile is another layer in a docker image (see the image below). 

Docker Hub (https://www.docker.com) is a service provided by Docker for finding and sharing container images. See the dockerfile below, the first line instructs to go and download and use golang:1.20-alpine docker image from dockerhub. Then other instructions follows and we simply create our docker image in the way we require. We can add different packages and customize the container anyway we like.

 

To run containers on our computer, we need to install docker runtime first. Docker can be installed on Windows, Linux or Mac. I will be using Ubuntu 20.04 LTS as my host computer. 

The Docker installation package available in the official Ubuntu repository may not be the latest version. To ensure we get the latest version, we’ll install Docker from the official Docker repository. To do that, we’ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package. First update your existing list of packages:

sudo apt update

 

Next, install a few prerequisite packages which let apt use packages over HTTPS:

sudo apt-get install ca-certificates curl gnupg lsb-release

 

Then add the GPG key for the official Docker repository to your system:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

 

Use the following command to set up the repository:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

 

Update your existing list of packages

sudo apt update

 

Install Docker

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

 

Verify that the Docker Engine installation is successful by running the hello-world image. The command below first downloads the image and then run. 

sudo docker run hello-world

 

DOWNLOADING DOCKER IMAGES TO YOUR HOST MACHINE:

As an example we will install Postgres 13.9 from https://hub.docker.com. This command will NOT find this docker image on local machine and then will start downloading but it will not initialize the container because postgresql requires password to be set in the command.

sudo docker run postgres:13.9

 

The correct command we should have used is the one below but how can we know that? 

sudo docker run --name postgres -e POSTGRES_PASSWORD=yourpassword -d postgres:13.9

 

We just simply log in to dockerhub and search for the image name(for example postgres) and read its documentaton. You can find all the details here about the image that you are working on.

To see the docker images on our host machine

sudo docker images

 

To see the docker containers currently running our host machine

sudo docker ps

We can see that a container named postgres is running at port 5432, that container is created from image postgres:13.9. Some people say that they get confused about the difference of a docker image and a docker container. I think you have already seen the difference. Docker image is the actual package (postgres). It is not running on our system. Container on the otherhand is created when the developer pull that docker image, starts the application and this action creates the container environment. Container is a running environment for the IMAGE. We will see more about it later on.

 

RUNNING DOCKER WITHOUT SUDO:

Create a docker group named docker and add your user to this group. 

sudo groupadd docker
sudo usermod -aG docker $USER

 

Just restart your server or Log out and log back in so that your group membership is re-evaluated.