Run a Python Web application in Docker

Source: Internet
Author: User

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 is hosted on the master version on GitHub. The URL is 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:

 
 
  1. $> git clone git@github.com:awslabs/eb-py-flask-signup.git  
  2. $> cd eb-py-flask-signup  
  3. $> 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, they are included in requirements.txt, application. py, and so on ).

 
 
  1. FROM ubuntu:12.10 
  2.  
  3. # Install Python Setuptools  
  4. RUN apt-get install -y python-setuptools  
  5.  
  6. # Install pip  
  7. RUN easy_install pip  
  8.  
  9. # Add and install Python modules  
  10. ADD requirements.txt /src/requirements.txt  
  11. RUN cd /src; pip install -r requirements.txt  
  12.  
  13. # Bundle app source  
  14. ADD . /src  
  15.  
  16. # Expose  
  17. EXPOSE  5000 
  18.  
  19. # Run  
  20. CMD ["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:

 
 
  1. $> 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 ):

 
 
  1. $> docker run -d \  
  2.      -e APP_CONFIG=application.config.example \  
  3.      -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \  
  4.      -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \  
  5.      -p 8080:5000 \  
  6.      eb-py-sample  

On OS x, I open the http: // localhost: 8080 link, showing an application of mine!

 

Sidebar: we use the-e Option to pass some options:

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 some special things that he has special Python environment types in Elastic Beanstalk (compared with Docker). What is the python version ?) , We need to remove them:

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

 
 
  1. files:  
  2.   "/var/app/app.config":  
  3.     mode: "000444" 
  4.     content: |  
  5.       AWS_REGION = '`{ "Ref" : "AWS::Region"}`' 
  6.       STARTUP_SIGNUP_TABLE = '`{ "Ref" : "StartupSignupsTable"}`' 
  7.       NEW_SIGNUP_TOPIC = '`{ "Ref" : "NewSignupTopic"}`' 

Modify option_settings to delete static file ing. Make him look like the following:

 
 
  1. option_settings:  
  2.   "aws:elasticbeanstalk:customoption":  
  3.      "AlarmEmail" : "nobody@amazon.com" 
  4.   "aws:elasticbeanstalk:application:environment":  
  5.     "APP_CONFIG": "/var/app/app.config" 
  6.     "FLASK_DEBUG": "false" 
  7.     "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 file for details, see below ).

 
 
  1. {  
  2.   "AWSEBDockerrunVersion": "1",  
  3.   "Volumes": [  
  4.     {  
  5.       "ContainerDirectory": "/var/app",  
  6.       "HostDirectory": "/var/app" 
  7.     }  
  8.   ],  
  9.   "Logging": "/var/eb_log" 
  10. }  

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 to package it ):

 
 
  1. $> git add Docker* && git commit -am "Dockerized"  
  2. $> 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:

 

Next

In the next article, I will use the eb command line tool to directly deploy this Dockerized program in the command line without a browser or management console!

Related Resources
  • Dockerized sample app on GitHub-https://github.com/awslabs/eb-py-flask-signup/tree/docker

  • Docker on Elastic Beanstalk documentation-http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker.html

Dockerizing a Python Web App

Http://www.oschina.net/translate/dockerizing-a-python-web-app.

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.