Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luisjunco/994a77b46cd3a2bcb50407c78e7efdf3 to your computer and use it in GitHub Desktop.
Save luisjunco/994a77b46cd3a2bcb50407c78e7efdf3 to your computer and use it in GitHub Desktop.
DSML - How to deploy a Flask app with Docker and AWS

Deploy a Flask app with Docker and AWS


1. Create the App

e.g., create an App or API that exposes the functionality from our model, using Flask.


2. Build the Docker image

  1. Create a Dockerfile:
  1. Build our docker image:

    • Run these commands from the directory where we have our Dockerfile (replace name-of-the-image with the desired name):
      • If you use Mac, use these commands (this will create an image in x86 architecture, so that it's compatible with the free tier of AWS):

        • docker buildx create --use
        • docker buildx build --platform linux/amd64 -t name-of-the-image:latest . --load
      • If you're on Linux/Windows:

        • docker build -t name-of-the-image .
  2. Run a container from our image in our local computer

    • docker run -p 5000:5000 my-cool-flask-api

3. Deploy on AWS

  1. Create an AWS account + Install AWS CLI

  2. Config AWS CLI in our local computer (we will need it to push the Docker image to AWS)

    • Login on AWS (root user / IAM user)
    • Click on the User menu > Security Credentials > Create access key
    • Configure AWS CLI: run aws configure + enter the access key
    • Test that the configuration is correct: aws sts get-caller-identity (will output the user id)
  3. Upload the Docker image to AWS Elastic Container Registry (ECR)

    • Go to AWS ECR
    • Click "Create repository"
    • Choose name for the repository & keep the default settings
    • Open the ECR repository we just created
    • Click "View push commands" and follow those instructions to push the Docker image we created earlier
  4. Create a virtual server on AWS EC2

    • Go to AWS EC2
    • Create a new instance on EC2:
      • Click "Launch instance"
      • Select OS (e.g. Amazon Linux 2 AMI)
      • Select architecture (note: this usually depends on the machine where you've built the docker image: for win/linux, usually x86; for Mac usually ARM)
      • Select instance type (for a demo, the free tier will be just fine; for production, may need a more powerful machine)
      • Network settings: allow HTTP and HTTPS traffic (IMPORTANT)
      • To confirm the settings, click "Launch instance"
        • Note: it may ask if you want to configure a key pair; if you're not planning to use SSH, no need to do it.
    • Configure the instance you've just created:
      • Go to the EC2 Dashboard and click on "Instances (running)"
      • Click on the instance
      • Click on "Security" (for that specific instance)
        • Inside security, click on "Security groups"
        • Click "Edit inbound rules"
        • Click "Add rule"
        • Create a new rule with these settings:
          • Type: Custom TCP
          • Port range: 5000
          • Source: anywhere (0.0.0.0/0)
  5. Configure Docker on the EC2 instance

    • Connect to the EC2 instance

      • Go to the EC2 Dashboard and click on "Instances (running)"
      • Click on the instance
      • Click on "Connect" (at the top)
      • Select "EC2 Instance Connect"
      • Select "Connect using a Public IP"
      • Click "Connect" (will open a terminal running on the EC2 instance)
    • Setup Docker, by running the following commands:

      • sudo yum install docker -y (install docker)
      • sudo service docker start (start the docker service)
      • sudo systemctl enable docker (configures the Docker service to start automatically when the machine restarts or powers on)
      • sudo usermod -aG docker $USER (allows running Docker commands without sudo)
      • newgrp docker (applies the new permissions, without the need to start a new shell)
    • Check that Docker is running:

      • docker --version
  6. Configure AWS CLI and Pull the Docker image, from our EC2 instance

    • Configure AWS CLI on the EC2 instance (execute the following commands on the terminal running on the EC2 instance):

      • run aws configure + enter the access key (similar process than we did earlier, but now on the EC2 instance)
      • Test that the configuration is correct: aws sts get-caller-identity (will output the user id)
    • Connect to the ECR repository:

      • Go to AWS ECR and open the ECR repository with the image that we want to use (ie. the ECR repository that we create earlier)
      • Click "View push commands":
      • Copy the first command (Retrieve an authentication token and authenticate your Docker client to your registry). It starts with aws ecr get-login-password ......
      • Execute that command (on the terminal running on the EC2 instance)
      • After running that command, it should say "Login successfully"
    • Pull the Docker image

      • Go to AWS ECR and open the ECR repository with the image that we want to use (ie. the ECR repository that we create earlier)
      • Copy the URI of the image (see screenshot)
      • run the command docker pull xxxxx (replace "xxxxx" with the URI of the docker image)

deploy-aws-ECR-copy-image-uri

  1. Run the Docker container on our EC2 instance

    • docker run -d -p 5000:5000 xxxxx (replace "xxxxx" with the Docker image tag -if we followed the instructions from AWS, this will be the same as the URI of the Docker image but, if in doubt, can get it with the command "docker images")
  2. Test

    • To get the public IP address of our server:
      • Go to the EC2 Dashboard and click on "Instances (running)"
      • Click on the instance
      • Click on "Details"
      • Copy the public IP address
    • Test (e.g., using the browser or Postman)
  3. Celebrate (hopefully!) 🍻



4. Clean up unused resources 🧹

Once the resources are not needed anymore, remember to terminate/delete them on AWS

To delete the instance on EC2:

  1. Turn off the machine:
    • Go to EC2 dashboard
    • Select the instance
    • Select "Instance state" > "Stop" (this will turn off the machine)
  2. Terminate the instance:
    • Select again the instance (note: now it will not appear in the list of instances running)
    • Select "Instance state" > "Terminate (delete) instance"
    • Note: once the instance is terminated, it may still appear in the dashboard for a few minutes/hours (but should disappear soon).

To delete the repository on ECR:

  • Go to the ECR dashboard
  • Select the repository
  • Click on "Delete"

Also, if you created an access key that you don't need anymore, it'd be good to delete it:

  • Go to security credentials > Access Keys
  • Select the key and deactivate/delete it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment