A beginner’s guide to Docker : what, why and how to start
Let’s say that a member of your team works on a code in system A and another member that will test the code works in system B. There’s a chance that the code that was made in system A isn’t working in system B due to different environments. Now, how do we solve this problem? The most typical solution would be to use a virtual machine. However, why use virtual machine when there’s a better solution?
In this article, we will discuss about Docker and how I use it in my group project.
What is Docker?
Docker is a container-based tool made to ease developers in creating, running, and deploying applications. Docker uses container which isolates application execution environment from one another yet allows them to use the same kernel. This means that you can run two containers on one computer at the same time and they are completely isolated from each other. It is extremely useful for me who uses different environment (in this case macOS) than my friend (windows).
Let’s talk more about the container, since it’s Docker’s MVP. As mentioned above, container is an isolated environment to package and run an application. It is also can be considered as necessitating three categories of software :
- Builder — technology used to build a container.
- Engine — technology used to run a container.
- Orchestration — technology used to manage many containers.
Docker container can die gracefully and comes back immediately when called. The cost to start a docker container is also cheap with quick boot-up time.
How does Docker work?
First, let’s take a look on Docker engine, which is an engine installed on your computer to build and run containers. As you can see, docker engine uses client-server architecture. The docker client will continuously communicates to the server through REST API. Then, the Docker Daemon will check the client’s request and interact with the operating system in order to create and manage the containers.
It looks pretty simple, doesn’t it? But there lies more than what meets the eye, especially in the REST API to server-side part. If we take a closer look, there are more components of Docker that takes part in this communication.
- Docker File — A file to build Docker image
- Docker Image — A template that contains the project’s code and responsible to create containers
- Docker Registry — An open source in server-side to store images and distribute them
So according to the picture above, the docker workflow would go like this : Docker File will build Docker image. Once Docker image is established, it will be uploaded to Docker hub where it can be shared to the entire team. From Docker hub, you can then access the docker image and build new containers.
Docker VS Virtual Machine
As you can see from the picture below, docker is almost similar to Virtual Machine (VM). Unlike VM, docker doesn’t have guest OS. This is a big advantage of Docker since having an OS on each virtual machine takes up heavy disk space usage oftentimes measured in GigaBytes (GB). On the other hand, containers are often measured in MegaBytes (MB). With guest OS replaced with container, Docker becomes an option with lighter weight than VM. Furthermore, Docker is open source so anyone can contribute to Docker and meet their needs.
Another major points that differ VM and Docker would be:
- OS Space — Docker occupies less space than VM since it allows application to share the same kernel
- Boot-up time — Do you know that it takes less than a second to start a Docker container? Now you know! Docker takes shorter boot-up time than VM.
- Performance — In terms of performance, Docker has the better performance since everything is centered in one Docker engine. Meanwhile, running multiple VM may result in unstable perfomance.
- Scaling — Docker is easy to scale up, VM is not.
- Portability — When porting ini different platforms, VMs may have some compatibility issues, while docker is easily portable.
How do I use Docker in real life?
In my PPL project, my team and I use docker to ease our work. To get started, first we create a Dockerfile which creates Docker Image. Here’s a little capture on the Dockerfile used in my project.
The command beside basically tells docker to :
FROM
— Create an image from Python 3.9 imageWORKDIR
— Change the working directory to /appENV
— Set the environment variables as listedRUN
— Install dependenciesCOPY
— copy requirements.txt and then install the dependencies withRUN
command.
After that, we begin to configure docker by creating a YAML file named docker-compose.yml. This file focuses on defining the services needed. In this project we listed several services such as rabbitmq, ms, db, web, and worker. Here’s a screenshot of my docker-compose.yml file.
Once we have the file above, we can start building and run the application. To do that, you can simply type in docker-compose up
on your terminal.
Now one of the criteria about Docker that I mentioned above is its use of disk space. To check about the size of image, you can use the command docker image ls
:
As you can see, the size of the image are all in MB and doesn’t even reach 1 GigaByte which could happen if we use VM.
To see the ports and which containers are running, you can check by using the command docker ps
But uh.. it’s a little bit confusing, isn’t it? To help you see it easier, you can install Docker application for desktop.
This way, you can see which containers are running and on which ports in a clearer and neater way!
Conclusion
Docker is a tool based on containers that can help developers to create, build, and run their applications in different environments. In Docker, multiple containers can run at the same time on the same hardware and completely isolated with each other. Configuring Docker is easy and takes little time. By using Docker, developers can boost application performance, productivity and efficiency.
This article is solely written based on my experience in using Docker. I hope the information above can help anyone who wants to get started with Docker. Cheers! 🥂