{"id":13540,"date":"2020-09-18T03:25:52","date_gmt":"2020-09-18T07:25:52","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=13540"},"modified":"2020-09-18T03:25:52","modified_gmt":"2020-09-18T07:25:52","slug":"docker-container-for-hosting-flask-app","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/docker-container-for-hosting-flask-app\/","title":{"rendered":"Hosting cars-api Flask app using Docker"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"> This blog talks about hosting <a href=\"https:\/\/github.com\/qxf2\/cars-api\">cars-api<\/a> Flask app using Docker. Docker is a set of platform as a service to deliver software as packages, called Docker containers. These are the standalone, lightweight, executable packages that could include necessary packages to run an application. Docker Images would turn to containers when they run on the Docker Engine.<\/p>\n<p>In the below steps, we are showing how to host the Flask app and launch the application with the help of Docker containers.<\/p>\n<p>Steps :<br \/>\n1. Creating the Docker image<br \/>\n2. Run the Docker image<br \/>\n3. Launch the app hosted in Docker <\/p>\n<p><\/span><\/p>\n<hr \/>\n<h3>1. Creating the Docker Image :<\/h3>\n<p>This section describes how to build the docker image.<\/p>\n<p>a)Create the Docker file from scratch and then build the Docker Image.<\/p>\n<p>Create a Dockerfile in your working directory and add the necessary line of codes as required for your project. <\/p>\n<p>Dockerfile may look like this :<\/p>\n<pre lang=\"python\">#Dockerfile to build an image\/container to host cars-api\r\n#Pull python Image\r\nFROM python\r\nLABEL maintainer = \"Qxf2 Services\"\r\n\r\n#Clone cars-api repository for Docker Image creation\r\nRUN git clone https:\/\/github.com\/qxf2\/cars-api.git\r\n\r\n#Set working directory\r\nWORKDIR \/cars-api\r\n\r\n#Install packages listed in requirements.txt file\r\nRUN pip install -r requirements.txt\r\n\r\n#Make port 5000 available to the container\r\nEXPOSE 5000\r\n\r\n#Execute command\r\nENTRYPOINT [ \"python\" ]\r\nCMD [ \"cars_app.py\" ]\r\n<\/pre>\n<p>Build the Docker Image. Make sure you are in the root directory of the project and run the following command.<\/p>\n<pre lang=\"python\">docker build --tag cars-api-docker .<\/pre>\n<p>You can continue with section 2 if you build a docker image now.<\/p>\n<p>b)Build the docker image from the GitHub followed by the branch. Provide the name that you would like to give as the tag.<\/p>\n<pre lang=\"python\">docker build --tag cars-api-docker https:\/\/github.com\/qxf2\/cars-api.git#master<\/pre>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_image_successfull-1.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_image_successfull-1.png\" alt=\"dockerimagebuild\" width=\"1024\" height=\"491\" class=\"aligncenter size-full wp-image-13715\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_image_successfull-1.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_image_successfull-1-300x144.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_image_successfull-1-768x368.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>Note: You should have the Dockerfile in your GitHub repository to create the Image. <\/p>\n<hr \/>\n<h3>2. Run the Docker Image into containers:<\/h3>\n<p>Now run the container from the Docker image that you just created. There are 2 ways you can run the Docker container. <\/p>\n<p>a) Foreground mode will be helpful to check if any errors while running as the output would be displayed.<\/p>\n<pre lang=\"python\">docker run -p 5000:5000 cars-api-docker<\/pre>\n<p>You can continue with section 3b, launch the app in the browser. If you want to try out in the terminal, open another terminal and follow the steps in section 3a. <\/p>\n<p>b) In detached mode, the Docker container would be running in the background of your terminal, and inputs\/outputs will not be displayed.<\/p>\n<pre lang=\"python\">docker run -d -p 5000:5000 cars-api-docker<\/pre>\n<p>Note: This would help in porting the host( your laptop) to the containers. This would be helpful in accessing the container from the external environment. <\/p>\n<p>You can list the running containers using the command below. Kindly note the container id for your reference.<\/p>\n<pre lang=\"python\">docker ps<\/pre>\n<hr \/>\n<h3>3. Launch the app hosted in Docker container<\/h3>\n<p>We can launch the Flask app in 2 ways<\/p>\n<p>a) Launch the app inside the container :<\/p>\n<p>This is the command to execute an interactive bash on the container<\/p>\n<pre lang=\"python\">docker exec -it [container-id] \/bin\/bash<\/pre>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_execution.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_execution.png\" alt=\"docker execution in terminal\" width=\"1227\" height=\"146\" class=\"aligncenter size-full wp-image-13716\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_execution.png 1227w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_execution-300x36.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_execution-768x91.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/docker_execution-1024x122.png 1024w\" sizes=\"auto, (max-width: 1227px) 100vw, 1227px\" \/><\/a><\/p>\n<p>You would get the root user as in the terminal then verify the Flask app. <\/p>\n<pre lang=\"python\">curl http:\/\/127.0.0.1:5000<\/pre>\n<p>This would display the HTML content of the index page. Kindly refer to the screenshot of the below code. You can see the list of cars. <\/p>\n<pre lang=\"python\">curl -u qxf2:qxf2 http:\/\/127.0.0.1:5000\/cars<\/pre>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/curl-to-cars-app-1.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/curl-to-cars-app-1.png\" alt=\"cars_list_inside_docker\" width=\"1298\" height=\"118\" class=\"aligncenter size-full wp-image-13717\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/curl-to-cars-app-1.png 1298w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/curl-to-cars-app-1-300x27.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/curl-to-cars-app-1-768x70.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/curl-to-cars-app-1-1024x93.png 1024w\" sizes=\"auto, (max-width: 1298px) 100vw, 1298px\" \/><\/a><\/p>\n<p>b) Launch the app in the host machine by launching the URL in the browser.<br \/>\n<a href=\"http:\/\/localhost:5000\">http:\/\/localhost:5000<\/a><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/localhost-1.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/localhost-1.png\" alt=\"url_launched_in_browser\" width=\"1118\" height=\"408\" class=\"aligncenter size-full wp-image-13718\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/localhost-1.png 1118w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/localhost-1-300x109.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/localhost-1-768x280.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/09\/localhost-1-1024x374.png 1024w\" sizes=\"auto, (max-width: 1118px) 100vw, 1118px\" \/><\/a><\/p>\n<p>You can stop the container after all the tests.<\/p>\n<pre lang=\"python\">docker stop [container-id]<\/pre>\n<hr \/>\n<p><span> Enhancements:\u00a0 You can create a GitHub action that would build the Docker image and run the container after the code change. For each and every push\/pull request the new build would be created and pushed into the Docker Image.<br \/>\n<\/span><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>This blog talks about hosting cars-api Flask app using Docker. Docker is a set of platform as a service to deliver software as packages, called Docker containers. These are the standalone, lightweight, executable packages that could include necessary packages to run an application. Docker Images would turn to containers when they run on the Docker Engine. In the below steps, [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,140,172],"tags":[],"class_list":["post-13540","post","type-post","status-publish","format-standard","hentry","category-api-testing","category-docker","category-flask"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/13540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=13540"}],"version-history":[{"count":69,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/13540\/revisions"}],"predecessor-version":[{"id":13885,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/13540\/revisions\/13885"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=13540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=13540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=13540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}