How to do it...

Let's imagine we want to deploy the following Python application, which we call dockerize.py:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int("5000"), debug=True)

The example application uses the Flask module. It implements a simple web application at the localhost address, 5000.

The first step is to create the following text file, with the extension of .py, which we will call Dockerfile.py:

FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./dockerize.py

The directives listed in the previous code perform the following tasks:

  • FROM python: alpine3.7 instructs Docker to use Python version 3.7.
  • COPY copies the application into the container image.
  • WORKDIR sets the working directory (WORKDIR).
  • The RUN instruction calls the pip installer, pointing to the requirements.txt file. It contains the list of dependencies that the application must execute (in our case the only dependence is flask).
  • The EXPOSE directive exposes to the port that is used by Flask.

So, in summary, we have written three files:

  • The application to be containerized: dockerize.py
  • Dockerfile
  • The dependency list file

So, we need to create an image of the dockerize.py application:

docker build --tag dockerize.py

This will tag the my-python-app image and build it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.140