e.g., create an App or API that exposes the functionality from our model, using Flask.
- Create a
Dockerfile
:
- Example Docker file (includes Python & installing dependencies from requirements.txt): https://drive.google.com/file/d/1CbgU0QWMZ-Uua7V3UOu547XqBwoL7Lt7/view
-
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 .
-
- Run these commands from the directory where we have our Dockerfile (replace
-
Run a container from our image in our local computer
docker run -p 5000:5000 my-cool-flask-api
-
Create an AWS account + Install AWS CLI
-
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)
-
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
-
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
)
-
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
-
-
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)
- run
-
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 withaws 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)
-
-
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")
-
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)
- To get the public IP address of our server:
-
Celebrate (hopefully!) 🍻
Once the resources are not needed anymore, remember to terminate/delete them on AWS
To delete the instance on EC2:
- Turn off the machine:
- Go to EC2 dashboard
- Select the instance
- Select "Instance state" > "Stop" (this will turn off the machine)
- 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