{"id":968,"date":"2018-04-14T18:44:36","date_gmt":"2018-04-14T18:44:36","guid":{"rendered":"http:\/\/www.codeastar.com\/?p=968"},"modified":"2018-04-14T18:46:14","modified_gmt":"2018-04-14T18:46:14","slug":"deploy-flask-app-docker-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.codeastar.com\/deploy-flask-app-docker-raspberry-pi\/","title":{"rendered":"Tutorial: Deploy a Flask app with Docker on Raspberry Pi"},"content":{"rendered":"

We have just\u00a0installed Docker on our Raspberry Pi<\/a>, now it is time to bring into action. Do you remember the EZW, Easy Weather Forecast Flask app<\/a>? Yes, we are going to deploy this EZW flask app. Of course, we do it in the Docker way!<\/p>\n

<\/p>\n

Prerequisites<\/h3>\n
    \n
  1. Docker installed on a Raspberry Pi (read here<\/a> for help if you haven’t done that)<\/li>\n
  2. Easy Weather Forecast (EZW) flask app source\u00a0(read here<\/a> for help if you haven’t downloaded the source)<\/li>\n
  3. Be a fan of Code A Star<\/del><\/li>\n<\/ol>\n

    Build our EZW Docker image<\/h3>\n

    On our Pi, let’s copy the EZW source and create a Dockerfile<\/em> there. The Dockerfile is a text file without any file extension. It is used for telling our\u00a0Docker on how to build an image. Then our file structure should look like this:<\/p>\n

    | Dockerfile\r\n\\ezw\r\n  | Pipfile\r\n  | ezw_app.py\r\n  | ezw_model.py\r\n  | ezw_controller.py\r\n  \\templates\r\n<\/pre>\n

    All we need to do is updating the Dockerfile, then Docker will follow the file and build an image. So what’s inside a Dockerfile?<\/p>\n

    Create a Dockerfile<\/h3>\n

    A Dockerfile is a list of build instructions. You can read the official guide here<\/a>. In our EZW case, we can build an image with following content:<\/p>\n

    FROM python:3.6-slim-stretch\r\nRUN pip install -U pip\r\nRUN pip install pipenv\r\nCOPY .\/ezw\/Pipfile .\r\nCOPY .\/ezw\/Pipfile.lock .\r\nRUN pipenv install --system\r\nEXPOSE 5000\r\nCOPY .\/ezw \/app\r\nWORKDIR \/app\r\nCMD [\"python3\", \"ezw_app.py\"]\r\n<\/pre>\n

    The file just tells Docker to:<\/p>\n

      \n
    1. Download a minimal Python 3.6<\/em> from Docker Hub<\/li>\n
    2. Update the version of pip<\/em><\/li>\n
    3. Use pip<\/em> to install pipenv\u00a0<\/em><\/li>\n
    4. Copy Pipfile<\/em> from \/ezw folder<\/li>\n
    5. Copy Pipfile.lock\u00a0<\/em>from \/ezw folder<\/li>\n
    6. Use pipenv<\/em> to install dependencies to its parent system (i.e. the system enclosed in a Docker image)<\/li>\n
    7. Open TCP port 5000 for this image<\/li>\n
    8. Copy content from file system\u00a0folder \/ezw<\/em>\u00a0to image\u00a0folder \/app<\/em><\/li>\n
    9. Use \/app<\/em> folder at working directory<\/li>\n
    10. Run “python3 ezw_app.py<\/em>”\u00a0command to start the EZW Flask app<\/li>\n<\/ol>\n

      Once we have prepared the Dockerfile, it is time to build our first Docker image.<\/p>\n

      Build a Docker image<\/h3>\n

      We can run following command to make the magic happen:<\/p>\n

      $docker build -t ezw .\r\n<\/pre>\n

      which we use “-t” option to define an image tag “ezw”.<\/p>\n

      Run from a container<\/h3>\n

      Now we have an image, we can run it with a container:<\/p>\n

      $docker run -d --name ezcon -p 8081:5000 -e DARK_SKY_KEY=[your Dark Sky API Key]\u00a0ezw\r\n<\/pre>\n

      The parameters we have used:<\/p>\n