Getting Started with Preconfigured Docker Containers
This section walks you through how to develop a sample application locally and then deploy your application to Elastic Beanstalk with a preconfigured Docker container.
Set Up Your Local Development Environment
For this walkthrough we will use a Python Flask “Hello World” application.
To set up your develop environment
Create a new folder for the sample application.
In the application's root folder, create an
application.pyfile. In the file, include the following:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
In the application's root folder, create a
requirements.txtfile. In the file, include the following:
Develop and Test Locally
To develop a sample Python Flask application
Dockerfileto your application’s root folder. In the file, specify the AWS Elastic Beanstalk Docker base image to be used to run your local preconfigured Docker container and against which Elastic Beanstalk runs any subsequent
Dockerfileinstructions by including the following:
AWS Elastic Beanstalk also supports Docker images for Glassfish 4.1 Java 8 and Glassfish 4.0 Java 7. For their Docker image names, see Pre-Configured Platforms. For more information about using a
Dockerfile, see Single Container Docker Configuration.
Build the Docker image.
docker build -t my-app-image .
Run the Docker container from the image.
You must include the
-pflag to map port 8080 on the container to the localhost port 3000. Elastic Beanstalk Docker containers always expose the application on port 8080 on the container. The
-itflag runs the image as an interactive process. The
-rmflag cleans up the container file system when the container exits. You can optionally include the
-dflag to run the image as a daemon.
docker run --it --rm -p 3000:8080 my-app-image
To view the sample application, type the following URL into your web browser.
Deploy to Elastic Beanstalk
After testing your application, you are ready to deploy it to Elastic Beanstalk.
To deploy your application to Elastic Beanstalk
In your application's root folder, rename the
Dockerfile.local. This step is required for Elastic Beanstalk to use the
Dockerfilethat contains the correct instructions for Elastic Beanstalk to build a customized Docker image on each Amazon EC2 instance in your Elastic Beanstalk environment.
Create an application source bundle. For more information, see Create an Application Source Bundle.
To create a new Elastic Beanstalk application to which you can deploy your application, see Managing and Configuring AWS Elastic Beanstalk Applications. At the appropriate step, on the Environment Type page, in the Predefined configuration list, under Preconfigured - Docker, click Python.