Docker, Say Hello To Easy Development!

Muhfathurh
6 min readMay 5, 2021
horizontal-logo-monochromatic-white.png (1202×309) (docker.com)

Imagine that you have developed an app with a specific environment, that is your personal computer’s environment. You explain your app source code to colleagues proudly. However, some of your colleagues that try to clone your source code in his personal computer tells you that your code does not run in his environment. Another colleague that manage to run it also complain how hard it is to configure his environment for your app to run. Upon receiving this massive complaint, you try to find a solution for this. Suddenly a whale app logo comes to your mind, yes, it’s docker time!

Introducing Docker

In this article, I will try to introduce you the basic of docker, considering I am not an expert myself. But first, let us familiarize ourselves first with docker and learn its terminology.

What is Docker?

Docker is a tool that utilized container to create, deploy, and run application with ease. By utilizing container, we can separate our app from our infrastructure. Separating our app means that any configuration in our app will not affect our infrastructure, vice versa. This is a blessing, considering every app has its own configuration and environment. If you try to install it directly in your infrastructure, you may has to change your infrastructure according to your app. Docker also allow you to setup environment in a way you setup your applications.

Why use Docker?

So why should we use docker? Isn’t it just an added difficulty in your already difficult application development? Well, docker arrived as a solution for problems that keep recurring in our software development, that is:

  1. Configuring our machine for app environment is hard and took a lot of time, especially when you have multiple app with different environments. You had to install each of them, and setup your machine every time you want to run different apps, such a pain, isn’t it?
  2. Docker allows you to test your code in development and move between development, production, and local with ease.
  3. Docker has many packages that can be installed within minutes, it will saves you a lot of time than configuring your pc’s environment.
  4. Docker has private and public registry that can be used to collaborate in your teams. In this way, your team will only need to pull docker images whenever we have changes.
  5. Last and foremost, docker is open source!

Upon learning about what docker is and its advantages, let’s identify some concept in docker. This maybe will not help you directly, but having understanding of it will help you adapting into docker’s way of life.

Docker vs Virtual Machines

Well, way long before docker appear, virtual machine already developed as an answer to problems that I mentioned above. However there are subtle differences between them which can be considered as an argument why docker is needed even though we can use virtual machine. First, let’s take a look into VM first. Virtual machine (VM) is a computer file, typically called an image, that behaves like an actual computer. It can run in a window as a separate computing environment, often to run a different operating system or even to function as the user’s entire computer experience . Just like docker, VM also separate itself from our computer’s infrastructure. It also borrow constant computing power from your computer. It has some differences with docker:

  1. VM performs just like an actual computer, while docker is not. Because of that, VM image is a lot bigger than docker images.
  2. Each VM has its own operating system, while docker share the host operating system. Thus, we can conclude that VM use hardware-virtualization, while docker use OS-virtualization.
A Practical Guide to Choosing between Docker Containers and VMs (weave.works)

So when should we use docker? Quoting from Nick Janetakist, a docker trainer in Docker vs. Virtual Machine: Where are the differences? — DevOps Conference, he says that “You can use Docker to isolate individual applications, and use Virtual Machines to isolate entire systems. They are operating at different levels of abstraction.”. I prefer this explanation to determine whether to use docker or virtual machine because of its conciseness and clear separation.

How does docker works?

Before we deep dive into how docker works, we need to understand docker components first. There are 5 of them which is:

  1. Docker CLI: docker command line interface that will receive command from user and send it to docker daemon.
  2. Docker Daemon: Docker components that will interact with operating systems to create or manage container
  3. Docker Registry: Docker registry is an open/private source server side service that is responsible for hosting and distributing images. Our defailt docker registry is docker hub, an open server side service that is provided by docker.
  4. Containers: Container is a executable software package which includes applications and their dependencies.
  5. Image: Image is an immutable file that contains the source code, libraries, dependencies, tools, and other files needed for an application to run

In order to works with docker, we need to have docker engine. Docker engine or usually called docker is the base engine installed in our machine that can build and run containers using docker components and services, such as docker CLI and docker daemon. Docker use client-server architecture, with docker CLI as its client and docker daemon as its server to work, communicated through REST API. This REST API will translate command from docker server before being sent to docker daemon. Optionally, we can push our image from docker daemon into docker registry or pull any image from docker registry and build a container. And that is, how docker works.

Docker Implementation

Finally, it’s time for us to implement docker in our code. In this case I will mostly use dockerfile in my software development class’s project, although I will not explain docker compose due to limited amount of time I have.

Dockerfile is a file that contains all the commands a user could call on the command line to assemble an image. When we run this image, it will be create container. Furthermore, we will also introduced to docker compose, which is a YAML script file that will configure your application services. With this, you can create multiple containers that act as a service without building it one by one. Now we cover some basic definition about them, let’s get into it.

Setup Dockerfile for Backend

To setup dockerfile for your backend, you need to create a dockerfile in backend project. In our dockerfile, we will list all of required commands for our applications to run. An example of it, as follow:

An example of Dockerfile

In this Dockerfile there are several steps that we do:

  1. We separate our build into two stages, build-python and our result image
  2. In build-python stages, we install our requirements.txt and store it into our wheels
  3. After that we proceed into next stages and specify environment variables with ENV and run required command (just like command in virtual machine) with RUN command. Our run command will prepare environment for our app and create a folder that will be used to place our source code folder.
  4. We specify our workdir by using WORKDIR command, this will tells docker that all of our command will be called from this directory
  5. Then, we copy our source code folder into our work directory by COPY command.
  6. And lastly, we can use CMD command to instruct our container to run this command (starting our server) as soon as we finished building our image.

And voila, your dockerfile is ready to go. To build your docker file you can go to work directory and specify this command:

docker build --tag $IMAGE:production --file ./Dockerfile "."

With image can be replaced to any name that you like. Optionally, you can also push your build image by using this command:

docker push $IMAGE:production

While pulling docker image from docker registry can be done with this: docker pull $IMAGE:production || true

And voila, you had finished configuring your docker. Now, your application is ready to go.

Finally, we arrived at the end of our article, hopefully, with this you can understand how docker works and implemented in your project. Now you can say good bye to hard time of configuring your infrastructure for an app, Cheers!

--

--