Containerize Your Application With Docker | Towards Data Science

Let’s say that you want to deploy an application to a server. In your local system, the application works just fine without any problem. But once you’ve deployed the application into a server, boom! Your application doesn’t work.
Many factors can make this happen. It could be the operating system compatibility or different library versions. Therefore, your application is never being deployed, and you will get a headache just because of that.
How can we remove the headache just because of the incompatibility problem? Docker comes to save you!
This article will show you how to use Docker to containerize your application so you can run them on any servers regardless of their operating system inside of them. Without further, let’s get started!
Before we get into the implementation, let me explain to you about Docker. Docker is a platform for deploying applications. It works by isolating the application into an image. The image will be run by a container, where it contains the operating system and supporting software.
Because of that isolation, we don’t have to worry about software installation and its version. You can run the application through a container regardless of the operating system and the software inside your computer. Therefore, you can focus on developing your applications and deploy them straightly.
After you understand the concept of Docker, now let’s get into the implementation. There are several steps that we will do:
Install DockerCreate a file called DockerfileBuild the imageRun the image
Install Docker
Create a file called Dockerfile
Build the image
Run the image
The first step that we can do is to install the docker application. You can download Docker here and make sure to pick the right one based on your operating system.
After you install Docker, now let’s test whether the Docker is already installed on your computer. You can run command ‘docker’ like this preview below:
If your computer shows a prompt like above, we can assure you that Docker is already installed on your computer. Now let’s get into the next step.
After you install Docker, the next step is to create a file called Dockerfile. Dockerfile is a script for building a docker image. It consists of instructions to set up the image ranging from installing libraries to run our project.
In this case, I’ve created an API that is based on the Flask library. This API will be used as the connector between the user and the machine learning model to return a prediction result. Here is the code of the API:
For creating the Dockerfile, make sure that the file is located at the same location as your application and the machine learning model folder. In my case, the folder location looks like this:
|   app.py|   Dockerfile|   requirements.txt|+---resnet50_food_model|   |   saved_model.pb|   ||   +---assets|   +---variables|           variables.data-00000-of-00001|           variables.index
Inside the folder, we have app.py for running my application, requirements.txt that contains the list of packages that we will use, the model folder that consists of the pre-trained model, and the Dockerfile for building a docker image.
Please remember that when you create the Dockerfile, make sure that you don’t give any extension after the file name. You must write ‘Dockerfile’ only.
Now let’s write our Dockerfile script. Here is the script inside the Dockerfile:
Let me explain each line of the script:
The ‘FROM’ instruction will take a docker image from a docker registry. The image will contain the operating system and also the Python.
The ‘WORKDIR’ instruction will set the default working directory that we will use to run the application.
The ‘COPY’ instruction will copy every single file inside the current directory into the new working directory.
The ‘RUN’ instruction will run a command. In this case, the ‘pip install’ command.
The ‘EXPOSE’ instruction will expose port 5000 as the connector to the docker image.
Finally, the ‘CMD’ instruction will run a command. In this case, the ‘flask run’ command.
Side Note: The ‘CMD’ instruction has the same function as the ‘RUN’ instruction, but they are different based on their purpose.
The ‘CMD’ instruction is used for running a command when you start running the docker image.
Meanwhile, the ‘RUN’ instruction is used for running a command while you are building the docker image.
After you create the docker image, the next step is to build the image. To build the image, it’s really simple. All you need to do is to run the ‘docker build’ command like this:
docker build -t my-image .
As you can see above, I give the ‘-t’ tag beside the ‘docker build’ command. The ‘-t’ tag takes two arguments. They are the image name and also the dot character, which represents all files inside the folder.
Here is the preview when we are building the docker image (It will take a long time, so be patient) :
Let’s run the ‘docker images’ command to display the existing images that are already built. Here is the preview of the result:
As you can see from above, the image already exists. Now the next step is to run the docker image on our computer. To run the image, you can use the command ‘docker run’ like this,
docker run -dp 5000:5000 my-image
Let’s wait for a while for 3 to 5 minutes. To know whether our image is already running, we can use the ‘docker ps -a’ command to check all running images. Here is the preview:
As you can see from above, the image is already up about a minute. It means that the image is already running at port 5000. Now let’s access the image using localhost:5000 or 127.0.0.1:5000. Here is the preview:
As you can see from above, our container has run successfully on our computer. Because you set the ‘-d’ parameter, you cannot show the log of the container.
To check activities on a container, you can use ‘docker logs’ for doing that. Also, please set the container id beside it. You can see the container id from the ‘docker ps -a’ command. The command looks like this:
docker logs bff6d3c8e2da
Here is what the log preview looks like:
If you want to stop the container, you can use the ‘docker kill’ command and set the container id beside it. Here is the command to stop the docker command:
docker kill bff6d3c8e2da
After you stop the container, it will look like this,
Yes, our container has already terminated.
Well done! You have learned how to containerize your application as a docker image. I hope this article helps you to build your own docker image on your project.
If you are interested in this article, you can follow me on Medium to get articles like this. If you have any questions or want to say hi, you can connect with me on LinkedIn.
Thank you for reading my article!
[1] https://docs.docker.com/[2] https://nickjanetakis.com/blog/docker-tip-7-the-difference-between-run-and-cmd