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:
- $> 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, they are included in requirements.txt, application. py, and so on ).
- 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; pip install -r requirements.txt
-
- # Bundle app source
- ADD . /src
-
- # Expose
- EXPOSE 5000
-
- # Run
- 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:
- $> 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!
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:
- 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 file for details, 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 to package it ):
- $> 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:
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.