Deploying Python to Azure with Docker - LabMazurokCom/Blockchain GitHub Wiki

  1. Install Docker

You are supposed to have Docker installed on your machine.

Read about technical requirements and installation steps here:

https://docs.docker.com/docker-for-mac/install/#install-and-run-docker-for-mac

Download the latest Docker version from here (you'll need to register or log in to download):

https://store.docker.com/editions/community/docker-ce-desktop-mac


  1. Add requirements.txt

There must be requirements.txt file in the root directory of your app. This file should contain full list of non-stdlib packages required to run your app. You can specify minimum or exact version of any package if you need. Here's an example:

aiohttp==3.1.3
async-timeout>=2.0
pyrebase

And one more example:

aiohttp
async-timeout
Flask
pymongo

The following steps are mainly taken from here: https://www.jamessturtevant.com/posts/Deploying-Python-Website-To-Azure-Web-with-Docker/#create-and-configure-your-azure-web-app


  1. Create Dockerfile

In the root directory of your Python App create a file called Dockerfile (without any file extensions).

touch Dockerfile

Open this file and add the following:

FROM python:3.6

RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install -r requirements.txt

EXPOSE 5000
CMD ["python", "/code/app.py"]

This Dockerfile uses the official Python Base image. The Dockerfile then creates a folder code and copies all the files from the current directory into the docker image. Next, it runs pip which installs all the library dependencies from the requirements.txt file. It opens the port 5000 and finally runs the command that launches the app. This example supposes that one needs to run app.py file to start your app and that app requires port 5000 to be opened (for example this port is required to run Flask).


  1. Build Docker image

o get your app ready for Deployment on Azure you need to build your docker image from your dockerfile. Run the following from your command prompt in the folder where you created your Dockerfile:

docker build --no-cache -f Dockerfile -t myappname:latest .

For this particular example you can test your image with the following command:

docker run -p 5000:5000 --rm myappname

  1. Publish Docker image

Here's how you can publish your image on public Docker Hub.

First find your image with the command

docker images

Tag the image of your app:

docker tag IMAGE_ID DOCKER_USERNAME/APP_NAME

Log in to Docker (follow the instructions in the terminal after running this command):

docker login

Push the image to Docker Hub:

docker push DOCKER_USERNAME/APP_NAME:TAG

For example if the available images are

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
logger              latest              ea093dd6f92c        9 minutes ago       936MB
<none>              <none>              25b30f36fbe6        29 minutes ago      912MB
python              3.6                 29d2f3226daf        3 weeks ago         912MB
hello-world         latest              e38bc07ac18e        6 weeks ago         1.85kB

and you want to publish logger app and your username is johndoe then you should run the following commands:

docker tag ea093dd6f92c johndoe/logger
docker login
docker push johndoe/logger:latest

  1. Deploy App to Azure

On Azure Portal main page click "Create a resource". Then in search field print "Web App for Containers" and click "Create".

Add app name, choose subscription plan, resource group and app service plan.

Click "Configure Container" and choose (for example described above):

  • Image source: Docker Hub
  • Repository Access: Public
  • Image and optional tag: johndoe/logger

Click "OK" and then "Create".


  1. Open necessary ports on Azure

On Azure Portal main page click "All Resources", select your app and click on "Application Settings". In Applications settings click "+ Add new setting" with the following parameters:

  • name: PORT
  • value: 5000

Click "Save" (in the top of the page).

⚠️ **GitHub.com Fallback** ⚠️