Tutorials to start deploying Python apps on Docker

Source: Internet
Author: User
Tags dynamodb
A few weeks ago, Elastic Beanstalk announced the configuration and management of Docker containers in the AWS Cloud. In this article, we use a simple Registration form page application to understand the Docker deployment process, which uses the elastic Beanstalk python environment.
about registering a form app

A few months ago, we've developed this app and posted it on blogs. There are 4 parts of the video and one article "Using DynamoDB and SNS with Elastic Beanstalk in any supported AWS region". Today, we will further develop and discuss how we deploy in Docker and elastic beanstalk environments above this part of the content. This article will be divided into 4 sections to explain.
Reference Resources

The original Python app (non-Docker) source code is hosted on GitHub (master version), and the URL is https://github.com/awslabs/eb-py-flask-signup/tree/docker. The Docker version is in the Docker version, with the URL: https://github.com/awslabs/eb-py-flask-signup/tree/docker

If you like the comparison between the code and the different versions, you can use the GitHub compare feature to see the difference between the two versions. The URL is https://github.com/awslabs/eb-py-flask-signup/compare/master...docker. You can also view each file or line of code that you added after Docker.


Docker Phase 1: Add Dockerfile file

First clone the source code from GitHub:

$> git clone git@github.com:awslabs/eb-py-flask-signup.git$> cd eb-py-flask-signup$> git checkout Master

By looking at the contents of the catalog, it is known that this is a simple Python application, using the Flask framework, Boto and some other dependencies (declared in Requirements.txt), where Boto is used for the interaction of Dynamodb and SNS.

Simple enough that we just need to create a dockerfile to build a mirror for the app to run. Dockerfile and other application sources are placed in the directory (i.e., Requirements.txt, application.py, and so on).


From ubuntu:12.10 # install Python setuptoolsrun apt-get install-y python-setuptools # install Piprun easy_install PIP # Add and install Python modulesadd requirements.txt/src/requirements.txtrun cd/src; Pip Install-r requirements.txt # Bundle app Sourceadd. /SRC # exposeexpose # runcmd ["Python", "/src/application.py"]

Docker Phase 2: on-premises testing

Although this application requires a Dynamodb table and an SNS theme to complete the full functionality, I can but not test them:

First, build the Docker Image:


$> Docker build-t Eb-py-sample.

Finally (direct to can use after!), run a container with the built-in image (map the container 5000 port to the host's 8080 port, and set some environment variables according to the following code):

$> Docker run-d \   e app_config=application.config.example \   e aws_access_key_id= $AWS _access_key_id \ -   e aws_secret_access_key= $AWS _secret_access_key \-   p 8080:5000 \   eb-py-sample

On OS x, I open the http://localhost:8080 link, which shows one of my apps!


Copy the Code code as follows:

Sidebar: We use the-e option to pass some options:

    • App_config: This program uses this option to load (point) its configuration file. By default, we specify a default configuration file. You can create a DynamoDB table and SNS topic and add them to this profile so that your application can work better when it is developed locally.
    • AWS_ACCESS_KEY_ID and Aws_secret_access_key: This application uses Boto to connect to DynamoDB and SNS, and Boto uses these environment variables to authenticate requests for the above services. These settings are for local development only. The unified identity and access control Scheme (IAM) role (Roles) is used when we deploy to Elastic Beanstalk.

Docker Phase 3: Modify. ebextensions

Our application has a special folder. Ebextensions, there is a setup.config file in it. We use this file notification to Elastic Beanstalk to create the Dynamodb table and SNS topic that we need for our program, and he creates a profile/var/app/app.config, which contains the Dynamodb table we just created and The name of the SNS topic.

What's special about this file is that he has a special type of environment for Python in Elastic Beanstalk (relative to Docker) (Python version?). ), we need to remove them:

Modify the members of the files and remove the owner and group Keys so that he looks like these:


Files: "/var/app/app.config":  mode: "000444"  content: |   Aws_region = ' {' ref ': ' Aws::region '} '   startup_signup_table = ' {' ref ': ' startupsignupstable '} '   New_signup _topic = ' {' Ref ': ' Newsignuptopic '} '

Modify Option_settings to delete the static file mappings. Make him look like these:

Option_settings: "aws:elasticbeanstalk:customoption":   "Alarmemail": "Nobody@amazon.com" "Aws:elasticbeanstalk : application:environment ":  " App_config ":"/var/app/app.config "  " Flask_debug ":" false "  " THEME ":" Flatly "

Check the Setup.config file to see if all of the previous changes are correct, or refer to the Setup.config hosted on GitHub.

Docker Phase 4: Deploy to Elastic Beanstalk

I have built and tested my local container and removed some. Ebextensions, which is a specific elastic Beanstalk python environment, I am ready to deploy it confidently!

I created a file named Dockerrun.aws.json, similar to this, and I created Dockerfile. This file will tell elastic Beanstalk how to run the Docker container and it looks like this (for more information on this file, see below).


{  "awsebdockerrunversion": "1",  "Volumes": [   {    "containerdirectory": "/var/app",    " Hostdirectory ":"/var/app "   }  ],  " Logging ":"/var/eb_log "}

About Dockerrun.aws.json

The volumes member will map the/var/app instance to/var/app on the container on EC2. The Docker container allows the app to run on the container by accessing the app. Config file and by creating a. Ebextensions/setup.config. Logging members told Elastic Beanstalk that our Docker app would log logs to/var/eb_log to the container. In the console, whenever you click Snapshot logs or if you enable automatic log rotation, Beanstalk will automatically push the log/var/eb_log to this directory.


I will commit my changes and use git archive to generate a ZIP file for deployment to elastic beanstalk (you can package it using the zip tool, Finder, or Windows Explorer):

$> git add docker* && git commit-am "dockerized" $> git archive--format=zip HEAD > Eb-py-flask-signup.zi P

After that, I deployed the generated zip package via Elastic Beanstalk Management Console

After my environment has passed, I can access it to make sure it works correctly:

I also saved a snapshot of the Environment Day log:

Because I have added logging members to the Dockerrun.aws.json file, the logs that are exported to/var/eb_log in the container can be directed to S3, and I can access them in the browser:

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.