Containerization With Docker

Docker Jan 20, 2024

What is Docker?

Docker is a containerization software that uses OS-level virtualization, a container is an isolated environment which is similar to a virtual machine(VM) . However, unlike a virtual machine, it is way less resource intensive and efficient.

Virtual Machine VS Docker

Let's say you have an app that you want to deploy on the web, it's not very efficient to use the entire server just to serve one app, now you have two options, deploy the app using Virtual Machines or deploy the app using containers.

In a VM set up you need to install a Host OS(Ubuntu) on the server, and then choose your Hypervisor of choice, a Hypervisor is a software that creates and runs Virtual Machines. Machines that run on the Hypervisor are call Virtual Machines, each Virtual Machine has it's own Operating System called the Guest Operating System which are isolated from the Host Operating System. All this can take up a lot of valuable system resources, as we need a Host Operating System, a Hypervisor, and one or many Guest Operating Systems on top of the Host Operating System.

In Docker you have the same Host Operating System, but instead of a Hypervisor we have a Runtime Engine, such as Docker Engine which can run multiple, isolated containers on the same operating system kernel. Containers are visualized at the operating system level, as all the containers share the same kernel of the host operating system, since these containers do not have a separate operating systems less resources are required to deploy the app, docker containers take up less resources to scale up than a virtual machine for the same reason.

Docker installation

Now that we've learnt about docker containers, lets see how to install docker and set up our own container, I'll be showing how to install docker on Ubuntu as it's one of the most popular Linux distribution and it's also what i use for my home lab server, the installation process is the same for all the Debian and Ubuntu based distributions.

To install Docker Engine we first need to set up Docker apt repository, and then install it from the repository.

To set up the Docker repo we need to follow these steps.

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
NOTE
If you use a derivative distro, such as Kali Linux, you may need to substitute the part of this command that's expected to print the version codename:
(. /etc/os-release && echo "$VERSION_CODENAME")
Replace this part with the codename of the corresponding Debian release

After setting up the Docker repo we can install the packages using the following the commands.

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

To verify that the installation is successful let's run a Hello-world image:

$ sudo docker run hello-world

You should get a similar output if docker is installed successfully:

 Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Setting up a container

To set up a container we first need to pull it's image from the cloud, an image is as immutable executable of a software package that includes various instructions and dependencies and information on how the container should run.

As an example let's set up an ubuntu container, ubuntu on ubuntu, I mean why not? To download the ubuntu docker image we use the following command:

$ sudo docker pull ubuntu

To run/access the container we use use the following command:

$ sudo docker run -ti --rm ubuntu /bin/bash

Here we are telling Docker to run ubuntu in an interactive mode -ti. And /bin/bash is used to tell the container to run bash terminal. The –rm flag is used to tell docker to delete the ubuntu docker container after we stop it. If everything went correctly you should see something like this.

root@08bd3b83a2b5:/#

The shell starts as a root user, it is similar to a typical Linux shell and by default every container gets a random hostname.

And there you go, we've successfully created our first docker container.

Tags

Deepak

Linux Enthusiast | Aspiring Cybersecurity Professional | Coding Enthusiast