Deploy a Container Web App on Amazon Lightsail
Module 1: Prepare Your Container
In this module, we will create a basic containerized web application
In this module, we will be reviewing the sample application provided with this tutorial. We will work through building a container to run the provided web application locally and test the web application on our local laptop before deploying it to the cloud.
What you will accomplish
In this module, you will:
- Build a container for an existing web application
- Run a container to test an application locally
Before starting this tutorial, you will need:
- Docker Desktop installed
- Basic working knowledge of Docker commands
Create the client app
The web application provided in the code folder later in this module and on the Introduction page of this tutorial is a web application developed with the Flask application framework for Python.
In a nutshell, the Flask framework includes a mini server to receive HTTP requests, a routing mechanism to associate specific requests (an HTTP verb and a /path) to your Python code, and a templating engine to render the final HTML.
The communication between Nginx and the Flask app is made with uWSGI protocol. The uWSGI component is provided by Flask.
The setup of the application is illustrated by this image.
The application is made of the following files:
- application.py, the core of the application, defines attributes of the app, such as the logic and the communication with a backend, if any.
- Pipfile lists the Python dependencies of the web application.
- Dockerfile configures the container. It starts from an official Nginx container, adds Python, Flask, and its dependencies. Then, it copies the Nginx configuration file and a startup script.
- nginx-app.conf is the Nginx configuration file. It makes sure static content is served by Nginx while other requests are passed on to uWSGI/Flask.
- start.sh starts the Nginx and uWSGI daemons.
The application is a simple web application to announce the launch of a new service or product and has a form to capture pre-registrations.
Download the project
To build and test the container, download a local copy of the project. You can download a zip containing the files here.
After extracting the files from the zip, make sure to change your command or terminal window to the corresponding directory.
From now on, all commands are assuming you have navigated to this directory.
Build your container
Now that you have a good idea of the main application components and how they work together, let's build a container.
To build the application inside a container, use your terminal and enter the following commands:
# Assuming you navigated to the application directory as instructed above # Make sure to include the space and period after signup in the below command docker build -t demo-flask-signup .
If you run this tutorial on an Apple Silicon-based device, use Docker's multi-architecture build feature. To ensure your containers will run on Amazon Lightsail, use the following additional commands:
docker buildx create lightsaildemo docker buildx build -t demo-flask-signup:amd-build-latest --platform linux/amd64 . —load
After a few minutes (depending on your internet bandwidth), the container is built.
You can list all container images on your system using the following command:
docker images # It should output something similar to REPOSITORY TAG IMAGE ID CREATED SIZE demo-flask-signup latest 26d7924c5980 28 minutes ago 640MB (...)
Test your container
To test the container on your local machine, first start the container, then point a browser at it.
docker run -p 8080:80 \ --rm \ -v ~/.aws:/root/.aws \ demo-flask-signup:latest
The options passed to the docker run command are
- -p 8080:80 to bridge your local machine TCP Port 8080 to the container port 80 (Nginx)
- --rm to instruct Docker to remove the container when it stops
- -v ~/.aws:/root/.aws to map your local machine AWS CLI configuration in directory ~/.aws to the container directory /root/.aws
- Finally, demo-flask-signup:latest - the name and tag of the container we just built.
Now that the container is running, open a browser and point it at http://localhost:8080. You should see the application home screen, as shown below.
Congratulations, you now have a container ready to be deployed into the cloud.