Skip to main content
In this lesson you’ll build and run a Docker image for a Python (Flask) app inside an AWS Cloud9 environment. We will:
  • Build a Docker image locally inside the Cloud9 EC2 instance.
  • Run the container and map its port to the EC2 instance.
  • Open the EC2 security group so you can access the app from your browser.
  • Commit the Dockerfile and related changes back to your Git repository.
Docker CLI is already available in the Cloud9 environment. Follow the steps below.

Dockerfile for the Flask app

This Dockerfile uses the official Python 3.10 slim image, installs dependencies from requirements.txt, exposes port 5000, and runs the Flask development server.

Build the Docker image

Build the image locally and tag it (example tag: my-app):
Sample (trimmed) build output:
After the build completes, the image resides on the EC2 instance backing Cloud9.

Run the container

Start the container and map container port 5000 to the EC2 instance port 5000:
Sample runtime output (Flask development server):
Do not use Flask’s built-in development server in production. For external deployments, use a production-ready WSGI server such as Gunicorn or uWSGI (for example, gunicorn -w 4 app:app).

Make the app reachable from your browser

Cloud9 runs on an EC2 instance. To access the containerized app from your browser:
  1. Find the EC2 instance that backs your Cloud9 environment and open its instance details.
  2. If port 5000 is not allowed in the instance’s security group, add an inbound rule for TCP port 5000 (or restrict access to your IP).
The image shows an AWS EC2 console with a running instance, displaying details such as instance ID, type (t2.micro), and public IPv4 address.
Edit the security group inbound rules to add Custom TCP port 5000, then save the changes.
The image shows the AWS EC2 console on the "Edit inbound rules" page, where a Custom TCP rule is being configured with port 5000. There's an option to save or preview changes.
Opening ports to the public internet increases attack surface. Prefer restricting inbound access to a specific IP range (your workstation IP) when adding port 5000 to the security group.
After the security group change, copy the EC2 public IPv4 address and open http://<PUBLIC_IP>:5000 in your browser. The app should load and behave like a local run. Example flow inside the sample app:
  • Log in using the sample credentials.
  • Browse the product page, place an order, submit order details, and confirm the order — the shipped/confirmation flow is implemented by the app.

Default credentials and sample Flask routes

The sample app implements a simple login and order flow with these default credentials:

Commit the Dockerfile back to Git

From the Cloud9 terminal, verify status, add, commit, and push the Dockerfile and any related changes:
Sample git status showing untracked files:
Add, commit, and push:
Then confirm the files are present in your remote repository.
This image shows an AWS CodeCommit repository named "aws-microservice-project" with folders like "static" and "templates" and files including "app.py" and "Dockerfile." The README notes it's an educational website for buying and selling cloud crypto coins.

Next steps — production deployment

The image stored on the Cloud9 EC2 instance is local to that instance. For production deployment you should:
  • Push the image to a container registry (for example, Amazon ECR).
  • Deploy to a container service such as Amazon ECS, Amazon EKS, or AWS Fargate.
Useful links: Quick reference — common commands That is it for this lesson — see you in the next one.

Watch Video