How to Serve a Flask App
with Amazon Lightsail Containers
Introduction
Overview
Implementation
1

Step 1: Create a Flask application
Complete the following steps on your local machine that is running Docker. These steps walk you through the process of creating the Flask application files
Step 1a: In the command line of your local machine, create a new project directory and switch to that directory.
Step 1b: Create a new file named app.py with the following code:
This minimal Flask application contains a single function hello_world that is triggered when the route “/” is requested. When it runs, this application binds to all IPs on the system (“0.0.0.0”) and listens on port 5000, which is the default Flask port..
Step 1c: Create a new file, requirements.txt. Edit the file and add the following code. Then, save the file.
The requirements.txt files are used to specify what Python packages are required by the application. For this minimal Flask application there is only one required package, Flask.
Step 1d: Create a new file named Dockerfile. Edit the file and add the following code to it. Save the file.
The Python alpine image ensures the resulting container is as compact and small as possible. The command to run when the container starts is the same as if run from the command line: python app.py
Step 1e: At this point, your directory should contain the following files:
2
Step 2: Build your container image
Step 2a: Build the container using Docker. Use the following command from the same directory as the Dockerfile:
This command builds a container using the Dockerfile in the current directory and tags the container “flask-container”.
Step 2b: Once the container build is done, test the Flask application locally by running the container.
3
Step 3: Create a container service
Complete the following steps to create the Lightsail container service using the AWS CLI, and then push your local container image to your new container service using the Lightsail control (lightsailctl) plugin.
4
Step 4:Deploy the container
Complete the following steps to create deployment and public endpoint configuration JSON files, and then deploy your container image to your container service.
5
Step 5: Clean up resources
Complete the following steps to the Lightsail container service that you created as part of this tutorial.
Conclusion
Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages