How to Serve a Flask App
with Amazon Lightsail Containers
Introduction
Amazon Lightsail is an easy-to-use virtual private server that offers a containers service. Follow along this tutorial to learn how to serve a Flask app on Lightsail containers service.
In this tutorial, you create a Flask application, build a Docker container, create a container service on Lightsail, and then deploy the application.
Overview
In this tutorial, you will set up a Docker container on your local machine, setup a container in the Amazon Lightsail service, and launch "Hello, World!" applications.
Prerequisites
Before starting this tutorial, you will need:
- An AWS account: If you don't already have an account, follow the Setting Up Your Environment getting started guide for a quick overview.
- A properly configured AWS Command Line Interface (CLI): For more information on how to set up the AWS CLI, please refer to the AWS CLI documentation. Be sure to use the latest version of the AWS CLI which is v2. Finally, be sure a region is set by default or you will need to specify the region when issuing Lightsail deploy commands.
- Docker installed on your local machine.
- Installed and configured Amazon Lightsail Control (lightsailctl) plugin: For more information on how to install Lightsail Control, refer to the Lightsail Control documentation.
Overview
AWS experience | Intermediate
Time to complete | 30 minutes
Cost to complete | Free Tier eligible. See the Lightsail Pricing page for more details.
Requires | Active AWS Account, AWS CLI, Amazon Lightsail Control, Docker
Services used | Amazon Lightsail
Last updated | October 17, 2023
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
Congratulations! You have successfully deployed a containerized Flask application using Amazon Lightsail containers.
Amazon Lightsail is a great choice to develop, build, and deploy a variety of applications like WordPress, websites, and blog platforms.
You can continue your journey with AWS by following the next steps in the section below.
Next Steps
Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages