{"id":8104,"date":"2021-04-01T07:28:51","date_gmt":"2021-04-01T07:28:51","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=8104"},"modified":"2024-06-25T11:08:32","modified_gmt":"2024-06-25T11:08:32","slug":"how-to-dockerize-flask-application","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/how-to-dockerize-flask-application","title":{"rendered":"How to Dockerize Flask Application?"},"content":{"rendered":"<p>An application developer can tell you that, Docker is an open-source application allowing administrators to create, manage, deploy, and duplicate applications using containers. Using docker you can make your python project portable by placing them inside a container.<\/p>\n<p>Let us extend our learning about Docker and lay a foundation on containers. An earlier blog on Docker can shed more light on containers. <a href=\"https:\/\/blog.cloudthat.com\/docker-lightweight-containers-in-limelight\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=docker-lightweight-containers-in-limelight\/\" target=\"_blank\" rel=\"noopener\"><span style=\"text-decoration: underline;\">Click here to read more.\u00a0<\/span><\/a><\/p>\n<p>Now we will discuss how you can Dockerize a Flask Application. So, if you are not familiar with Flask, then Flask is nothing but a web development framework of Python. Python has two popular web development frameworks; the first one is Django and the other one is Flask. Django is quite heavier than Flask as it has many dependencies. So, we will see how you can deploy a simple Flask Application on Docker.<\/p>\n<h2><strong>Prerequisites:<\/strong><\/h2>\n<p>1. Install Python on your machine.<\/p>\n<p>For installing Python on Windows, visit this URL <a href=\"https:\/\/www.python.org\/downloads\/\">https:\/\/www.python.org\/downloads\/<\/a> and download the latest Python version.<\/p>\n<p>If you are using an Ubuntu machine, then use the following command.<\/p>\n<pre class=\"lang:default decode:true\">sudo apt update -y\r\nsudo apt-get install python3-pip -y\r\n<\/pre>\n<p>2. Install Flask library. You can run the below command to install Flask.<\/p>\n<pre class=\"lang:default decode:true \">pip install flask<\/pre>\n<p>Make sure that <strong>pip<\/strong> is also installed as well.<\/p>\n<h2><strong>Guide to set up Flask Application:<\/strong><\/h2>\n<p>Now, you need to create a python file with the name <strong>app.py<\/strong> inside a folder. And paste the following code in that file.<\/p>\n<pre class=\"lang:default decode:true \">from flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef index():\r\n    return \"&lt;h2&gt;This is just a basic Flask Application&lt;\/h2&gt;\"\r\n\r\nif __name__ == \"__main__\":\r\n    app.run(host ='0.0.0.0', port = 8080, debug = True)\r\n<\/pre>\n<p>This a simple Flask Application to print some message on the browser. It will create a server and run that on port number 8080. You can choose any port as per your requirement. Now, save that file and run <strong>python app.py<\/strong> in the command prompt.<\/p>\n<p>You will notice that a development server gets started.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8105\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA1.png\" alt=\"\" width=\"658\" height=\"141\" \/><\/a><\/p>\n<p>Now, when you browse this URL: <a href=\"http:\/\/localhost:8080\">http:\/\/localhost:8080<\/a>, the content that was written inside the <strong>return<\/strong> keyword will get displayed there.<\/p>\n<p>It means that your Flask Application is working perfectly fine.<\/p>\n<p>The next thing that you need to do is to create a file with the name <strong>requirements.txt <\/strong>and write a\u00a0<strong>flask <\/strong>inside that file. This is just to install the flask library.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8106\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA2.png\" alt=\"\" width=\"638\" height=\"126\" \/><\/a><\/p>\n<p>After that create a Dockerfile with the name <strong>Dockerfile<\/strong>. And make sure that you are not putting any extension at the end of the file name. And paste the below command in that.<\/p>\n<pre class=\"lang:default decode:true \">FROM python:alpine3.7\r\nCOPY . \/myapp\r\nWORKDIR \/myapp\r\nRUN pip install -r requirements.txt\r\nEXPOSE 8080\r\nENTRYPOINT [ \"python\" ]\r\nCMD [ \"app.py\" ]\r\n<\/pre>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8107\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA3.png\" alt=\"\" width=\"650\" height=\"171\" \/><\/a><\/p>\n<p><strong>FROM<\/strong> keyword is used to download the base image, which is of <strong>python<\/strong> and <strong>alpine3.7<\/strong> is its version. Next, we are copying all the file in the current directory in the directory to <b>my app<\/b>\u00a0which is inside the container. <strong>WORKDIR<\/strong> is used to define the working directory. If you will not use that keyword, then it will treat root as the working directory. <strong>RUN<\/strong> is used to run the command that we want to execute. <strong>pip install -r requirements.txt<\/strong> will install all the module which is written inside the file. Port 8080 will get exposed by <strong>EXPOSE<\/strong> command. In the code we have mentioned that the development server should run at port 8080. That\u2019s why we need to expose that port. The last two lines are used to run the application automatically once you create a container.<\/p>\n<p>So, this is about the commands which are written in Dockerfile.<\/p>\n<p>The next thing that you need to do is to install Docker on your machine. Or you can launch a virtual machine and install Docker in it. The command to install Docker in an ubuntu machine is:<\/p>\n<pre class=\"lang:default decode:true\">sudo apt-get install docker.io -y<\/pre>\n<p>Make sure that Docker is properly installed on your system. You can verify that by running the below command.<\/p>\n<pre class=\"lang:default decode:true \">docker  --version<\/pre>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8108\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA4.png\" alt=\"\" width=\"650\" height=\"165\" \/><\/a><\/p>\n<p>If you are using a virtual machine, then you need to copy all the files in that VM. And run the following command to build the Dockerfile.<\/p>\n<pre class=\"lang:default decode:true \">docker build -t flask-image .<\/pre>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8109\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA5.png\" alt=\"\" width=\"652\" height=\"273\" \/><\/a><\/p>\n<p>It will create an image with the name <strong>flask-image<\/strong>. After creating the image, you need to create a container from that image. So, you can run the below command for that.<\/p>\n<pre class=\"lang:default decode:true \">docker images<\/pre>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8110\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA6.png\" alt=\"\" width=\"650\" height=\"109\" \/><\/a><\/p>\n<pre class=\"lang:default decode:true \">docker run -d -p 80:8080 --name flask-container flask-image<\/pre>\n<p>It will launch a container, and when you browse your public IP of your VM along with the port number, then you will see that your Flask Application is running.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8111\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/JA7.png\" alt=\"\" width=\"634\" height=\"217\" \/><\/a><\/p>\n<h2><strong>Conclusion:<\/strong><\/h2>\n<p>In this guide, you have learned to create and deploy a Flask application to a Docker container. You can scale the application with the help of Docker with ease.\u00a0 If you do not want to deploy directly to a virtual machine, then you can use this method to containerize the application using Docker.\u00a0To attain a deep knowledge of Docker, Operating System, Virtualisation or Containerisation concepts\u00a0and the use of containers for deploying &amp; managing applications on Docker get certified through the foundation and Docker Essentials courses.<\/p>\n<p><strong>CloudThat<\/strong> offers extensive online and classroom training on <a href=\"https:\/\/cloudthat.in\/courses\/course\/docker-course\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=docker-course\" target=\"_blank\" rel=\"noopener\">Docker Essentials \u2013 Level 2<\/a> for beginners and <a href=\"https:\/\/cloudthat.in\/courses\/course\/kubernetes-essentials\/#Description\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=kubernetes-essentials\/#Description\/\" target=\"_blank\" rel=\"noopener\"><span style=\"text-decoration: underline;\"><strong>Kubernetes Essentials\u00a0<\/strong><\/span><\/a>for foundation-level courses.\u00a0After completing this course, you will understand the best practices to use Docker.<\/p>\n<p>Drop a comment for feedback or any queries you have relating to Docker, containers or Kubernetes and we will get in touch with you.<\/p>\n<p>Stay tuned for more updates.<\/p>\n","protected":false},"author":219,"featured_media":8112,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3608,3607],"user_email":"prarthitm@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/8104"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=8104"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/8104\/revisions"}],"predecessor-version":[{"id":45939,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/8104\/revisions\/45939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=8104"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=8104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}