Start a tutorial on deploying Python apps on Docker _python

Source: Internet
Author: User
Tags git clone docker run dynamodb

A few weeks ago, elastic Beanstalk declared the configuration and management of Docker containers in the AWS Cloud. In this article, we use a simple sign-up form page application to understand the Docker deployment process, which uses the elastic Beanstalk python environment.
about Registration Form Application

A few months ago, we had developed this application and posted it on our blog. There are 4 sections of video and an article "Using DynamoDB and SNS with elastic Beanstalk in any supported AWS Region". Today, we will further develop and discuss how we deploy in the Docker and elastic beanstalk environments on this part of the content. This article will be divided into 4 sections to explain.
Reference Resources

The original Python application (Docker) source code is hosted on the GitHub (master version) and the URL is https://github.com/awslabs/eb-py-flask-signup/tree/docker. Docker version in the Docker version, the URL is: 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 Contrast feature to see the difference between the two versions. The website is https://github.com/awslabs/eb-py-flask-signup/compare/master...docker. You can also view each file or line of code added after Docker.


Docker Phase 1: adding dockerfile files

First clone the source code from the 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, knowing 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 dynamodb and SNS interaction.

Simple enough that we just create a dockerfile and build a mirror that works for that application. Dockerfile and other application sources are placed in the directory (i.e., with requirements.txt, application.py, etc.).


From ubuntu:12.10
 
# Install Python setuptools
run apt-get install-y python-setuptools
 
# Install PIP
Run Easy_install pip
 
# Add and install Python modules
add requirements.txt/src/requirements.txt
RUN cd/src p IP install-r requirements.txt
 
# Bundle app source
ADD./src
 
# Expose expose 5000
 
# Run
CM D ["Python", "/src/application.py"]

Docker Phase 2: Testing locally

Although this application requires a Dynamodb table and SNS theme to complete all functions, I can but not test them:

First, build Docker mirrors:


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

Finally (directly to the available after!), run a container (Map container 5000 port to host 8080 port and set some environment variables according to the following code) by building Image:

$> 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, and the image below shows one of my apps!


Copy Code code 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 theme 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 DynamoDB and SNS, and Boto use these environment variables to authenticate requests 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: modifying. ebextensions

Our application has a special folder. Ebextensions, there's a setup.config file in there. We use this file notification to elastic Beanstalk create the DynamoDB table and SNS theme that our program requires, and he will create a profile/var/app/app.config that contains the DynamoDB we just created Table and SNS theme of the name.

There are special areas in this file that he has a particular type of Python environment in elastic Beanstalk (as opposed 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 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 mappings. 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 make sure all 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, removed some. Ebextensions, it is a specific elastic Beanstalk python environment, I have been confidently ready to deploy it!

I created a file, called Dockerrun.aws.json, similar to this, and I created the dockerfile. This file will tell elastic Beanstalk how to run the Docker container and it looks like this (details of 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 on the EC2 to the/var/app instance to the container. The Docker container enables the app to run on the container by accessing the app.config file and creating. 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 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 Explorer to package):

$> git add docker* && git commit-am "dockerized"
$> git archive--format=zip head > Eb-py-flask-sign Up.zip

After that, I deploy the generated zip package through the elastic Beanstalk Management Console

When my environment passes, I can access it to make sure it works:

I also saved a snapshot of the environment 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.