Avatar

Blogging tips

Avatar

Python, Django and Flask

Avatar

Latest tech posts

Saturday, December 7, 2019

Dockerize flask app

codesimple.info
Docker is a container management service. The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers that can then be deployed anywhere.
Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Imagine you’re trying to deploy the following Python code, contained in index.py. The application is a simple, “Hello World” app that uses Flask, a small HTTP server for Python apps.


To do so, create a text file called Dockerfile in your application’s root and paste in the following code.

Dockerfile


Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.





Each instruction creates one layer:
  • FROM creates a layer from the alpine Docker image with python version as 3.7
  • COPY adds files from your Docker client’s current directory.
  • RUN builds your application with make.
  • CMD specifies what command to run within the container.
When you run an image and generate a container, you add a new writable layer (the “container layer”) on top of the underlying layers. All changes made to the running container, such as writing new files, modifying existing files, and deleting files, are written to this thin writable container layer.
Note that FROM directive is pointing to python:alpine3.7. This is telling Docker what base image to use for the container, and implicitly selecting the Python version to use, which in this case is 3.7. Docker Hub has base images for almost all supported versions of Python including 2.7. This example is using Python installed on Alpine Linux, a minimalist Linux distro, which helps keep the images for Docker small. Prefer Alpine unless there’s a compelling reason to use another base image such as Debian Jessie.
Also, note is the RUN directive that is calling PyPi (pip) and pointing to the requirements.txt file. This file contains a list of the dependencies that the application needs to run. Because Flask is a dependency, it is included as such in the requirements.txt with a simple reference. You can also select version libraries if you need specific versions with requirements.txt. The file should also be at the root of the application.
To build the image, run Docker build from a command line or terminal that is in the root directory of the application.
docker build --tag my-python-app .
This will “tag” the image my-python-app and build it. After it is built, you can run the image as a container.
docker run --name python-app -p 5000:5000 my-python-app
This starts the application as a container. The –name parameter names the container and the -p parameter maps the host’s port 5000 to the containers port of 5000. Lastly, my-python-app refers to the image to run. After it starts, you should be able to browse to the container. Depending on how you are running Docker depends on what the IP address of the application will be. Docker for Windows and Docker for Mac will be able to use 127.0.0.1. For other instances, it will be the host IP of a VM or physical machine you are running Docker on.

codesimple.info
Hello world app

Naturally, more complex scenarios will require more attention to details, but the basic flow is the same for most Python apps. Putting it all together will enable containerized Python apps in short order though!
Thanks for this article.
#Docker #Flask #Dockerfile

No comments:

Post a Comment