Tutorial on deploying Python applications on Docker, dockerpython

Source: Internet
Author: User
Tags dynamodb

Tutorial on deploying Python applications on Docker, dockerpython

Several weeks ago, Elastic Beanstalk declared that Docker containers were configured and managed on the AWS cloud. In this article, we use a simple registry form page application to understand the Docker deployment process. This form uses the Elastic Beanstalk Python environment.
About Single Registry Application

A few months ago, we have developed this application and published it on our blog. There are 4 videos and an article "Using DynamoDB and SNS with Elastic Beanstalk in any Supported AWS Region ". Today, we will further develop and discuss how we are deployed in the Docker and Elastic Beanstalk environments. This article is divided into four parts.
Reference resources

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

If you like code comparison with different versions, you can use the GitHub comparison function to view the differences 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 added after dockerization.


Docker Phase 1: add the Dockerfile

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

Dependencies), where Boto is used for interaction between DynamoDB and SNS.

It is simple enough that we only need to create a Dockerfile to build an image suitable for running the application. Dockerfileand other application sources are stored in the directory (that is, and put together with requirements.txt, application. py, etc ).

 

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 5000 # RunCMD ["python", "/src/application.py"]

Docker Phase 2: Local Testing

Although this application requires a DynamoDB table and an SNS topic to complete all the functions, I can but did not test them:

First, build a Docker image:

 

$> docker build -t eb-py-sample .

Finally (directly after it can be used !), Run a container through the constructed image (MAP port 5000 of the container to port 8080 of the host, 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, showing an application of mine!


Copy codeThe Code is as follows: sidebar: we use the-e Option to pass some options:

  • APP_CONFIG: This program uses this option to load (point to) 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 configuration file so that your application can work more perfectly during local development.
  • AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY: This application uses Boto to connect DynamoDB and SNS, and Boto uses these environment variables to authenticate requests for the above services. These settings are only for local development. When we deploy Elastic Beanstalk, we will use the Unified Identity and access control solution (IAM) role (Roles ).

Docker Phase 3: Modify. ebextensions

Our application has a special folder. ebextensions, which contains a setup. config file. We use this file notification to Elastic Beanstalk to create the DynamoDB table and SNS topic required by our program. At the same time, it will create a configuration file/var/app. config. This file contains the DynamoDB table we just created and the name of the SNS topic.

In this file, there are also some special points that he has a special Python environment type (python version?) in Elastic Beanstalk (relative to Docker ?) , We need to remove them:

Modify the files member and remove the owner and group keys to make it look like the following:

 

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 static file ing. Make him look like the following:
 

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 check whether all previous changes are correct, or refer to setup. config hosted on GitHub.

Docker Phase 4: deployment to Elastic Beanstalk

I have created and tested my local container and removed some. ebextensions, which is a specific Elastic Beanstalk Python environment. I am confident that I am ready to deploy it!

I created a file named Dockerrun. aws. json. Similarly, I created a Dockerfile. This file will tell Elastic Beanstalk how to run the Docker container and it looks like this (for details about this file, see below ).

 

 {  "AWSEBDockerrunVersion": "1",  "Volumes": [   {    "ContainerDirectory": "/var/app",    "HostDirectory": "/var/app"   }  ],  "Logging": "/var/eb_log" }

About Dockerrun. aws. json

Volumes members MAP/var/app instances on EC2 to/var/app on the container. The Docker container allows the app to run on the container by accessing the app. config file and creating. ebextensions/setup. config. The Logging member tells Elastic Beanstalk that our Docker app will log to/var/eb_log to the container. In the console, whenever you click Snapshot Logs or Enable Automatic Log rotation, Beanstalk will automatically push Logs/var/eb_log to this directory.


I will submit my changes and use git archive to generate a zip file for deployment to Elastic Beanstalk (you can use the zip tool, Finder, or Windows Resource Manager for packaging ):
 

$> git add Docker* && git commit -am "Dockerized"$> git archive --format=zip HEAD > eb-py-flask-signup.zip

Then, I deploy the generated zip package through the Elastic Beanstalk Management Console.

When my environment passes, I can access it to ensure that it works properly:

I also saved a snapshot of the Environment log:

Because I went to Dockerrun. aws. logging members are added to the json file. Therefore, logs output from the container to/var/eb_log can be directed to S3 and can be accessed in the browser:

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.