How Docker works and what are its secrets
Docker is the leading public and hosted container registry (by usage). It is an efficient solution to build and deploy applications that require different technologies. Between the years 2011 and 2021, Docker has raised a total of $330.9 million.
In the last few years, containerization has become very popular. A survey made by the Cloud Native Computing Foundation revealed that 84% of the respondents were already using containers in production, with an increase of 15% from the previous year.
This article will learn what Docker can do for you and your company in terms of efficient resource utilization and flexibility. I will also show you step by step how to install Docker and deploy an Apache server on a Docker container.
Table of Contents
Docker is a virtualization platform that allows users to:
- Run an application in an isolated environment, separated from the hardware. This is extremely useful because you can have different technologies on the Docker container and successfully run an application without having those technologies on the host.
- Deploy code quicker and reduce the time between writing code and running it in production
- Create templates from Docker images and use those templates to replicate environments without having to re-install tools and technologies
- Increase flexibility by being able to move containers or apply patches without interrupting production
It is important to note that Docker is a free solution if used for personal use, and it costs $5/month for a Pro. Other pricing options are available for business use.
In the next chapter, you will see how to install it on your Linux machine and how you can use it to host your own Apache webserver.
I will show you how to install Docker on Linux.
Add the Docker PGP key:
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/docker-archive-keyring.gpg >/dev/null
Configure the Linux APT repository so we can install using apt-get:
echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian buster stable' | sudo tee /etc/apt/sources.list.d/docker.list
Make sure to update the newly changed repository:
sudo apt-get update
Now, remove older versions of Docker if you have any (like docker-engine or docker.io):
sudo apt-get remove -y docker docker-engine docker.io
Finally, install Docker:
sudo apt-get install -y docker-ce
Type “Y” when prompted if you want to keep downloading.
After the installation is complete, make sure the install was successful by typing the following command:
If you have Docker, you will get the path where it is installed. Now that you have Docker, you can set up a container and start deploying applications.
Now, we will set up an Apache webserver on our Docker container.
Firstly, let’s create a directory for the server and change our location to that directory (you can name it anything):
And start Docker:
sudo systemctl start docker
After this, we can host the Apache server on Docker. In the case in which you do not already have the Apache image, like me, it will be automatically downloaded):
docker run -dit --name my-apache-app -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4
Let’s check if it worked. For this, visit the localhost in your browser. It is ok if the page is empty if you don’t have any files in the directory where you started docker.
Now we can create a page (I named it docker.html) with a straightforward script.
We will be able to see it in the browser.
And browse to it by typing http://localhost:8080/docker.html.
The web server will be named my-apache-app. You can stop with the following command:
sudo docker container stop my-apache-app
And restart it with the following command:
sudo docker container stop my-apache-app
On Linux, you can also host your own Apache server very quickly. To start it, just type:
sudo systemctl start apache2
If you want your webserver to start every time you boot the machine, you can type:
sudo systemctl enable apache2
Now, you can browse to http://localhost:80 to see your newly made Apache web page.
This time, as you can see, we are greeted with the default Apache2 web page.
This simple web server does not use a lot of resources. However, having a virtual machine dedicated to a web server would take up more CPU and memory resources than a Docker container.
This is because Docker efficiently uses the operating system functions and doesn’t require their duplication. In the typical environment, deploying and running an app can waste resources.
In the next chapter, you will see a few of the advantages of using Docker containerization.
- Your applications can run anywhere – it does not matter what your host OS is if you have containerization
- Docker is cost-effective – fast deployment and resource-sharing between applications make the whole process of releasing applications smoother
- Containers are flexible – you can test out new patches and updates in containers and deploy new ones without affecting your production
- You can reuse containers – this includes technologies you’re using on them. You can create templates and reuse their images.
- Containers are easier to maintain – if a bug is introduced outside the container, the isolated application is not affected. If a bug is introduced inside the container, its source can be found easier. It will not affect other components on the host machine.
- You only pay what you use – the pricing allows you to use Docker for a set period (there are also free subscriptions).
- Docker allows running both Windows and Linux programs and executables and can be used on Windows, Linux, and macOS.
Kubernetes is an open-source container orchestration service.
There is no reason to put Kubernetes and Docker against each other – these two technologies work very well together. Compared to Docker, Kubernetes runs over a cluster and not a single node, like Docker.
Therefore, Kubernetes can help manage multiple Docker containers simultaneously, over multiple hosts, using a control pane from where nodes are controlled.
Using this orchestration technology, multiple containers can easily be:
In Kubernetes, network traffic can also be load-balanced so that development is efficient.
Below, you can see an infographic with a few statistics regarding Docker and containerization in general. Overall, using containers has become popular, and more and more companies are considering using containerization in their app deployment and production.
Considering all the advantages Docker brings, you should consider incorporating containerization into your strategy if you are a developer.
It is more effective regarding resource utilization, flexible and reusable.
I hope my step-by-step guide was helpful and if you had any trouble installing Docker, please let me know!