top of page
Image by Pascal Meier

Hosting Deep Learning apps on AWS (Stage 4): Building the Docker Container Image

Updated: May 25

We have set up the AWS Infrastructure needed to deploy our Deep Learning app but we now need to pull in our code base, build the (Docker) container image and push it to Amazon Elastic Container Registry (ECR). We are using the AWS Region eu-west-1 below (Ireland) - choose a region closest to your physical location.

Follow these steps:

i. Fork the GitHub app code base and clone into your EC2 instance

We first need to create / bring in our code base into the EC2 machine in order to create the Docker image.

Do this by first forking the GitHub repo below and then cloning (with git clone) into your EC2 project folder:

Note the GitHub repo name corresponds to the project folder we created in Stage 3. The repo contains a Dockerfile, serverless.yml, python requirements and a copy of the Deep Learning source code from stage 2 (unpacked flask-backend folder only).

ii. Build the Docker image

Inside your project folder (lambda-dl-aws), run the command:

docker build -t  aws-lambda-dl-app .

Note here aws-lambda-dl-app is the name of our app, not the name of the project folder.

iii. Create an ECR repository

Create an ECR repo with:

aws ecr create-repository --repository-name aws-lambda-dl-app --image-scanning-configuration scanOnPush=true --region eu-west-1 

iv. Tag the image to match the repository name

Fetch your AWS Account ID <AWS_ACCOUNT_ID> from the AWS Console and together with your AWS region, replace in the line below and run the command inside your project folder:

docker tag aws-lambda-dl-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>

v. Register docker to ECR

Again replacing <AWS_ACCOUNT_ID> and <REGION> below with your AWS Account ID and Region, run this command:

aws ecr get-login-password --region <REGION> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>

vi. Push the image to ECR

Finally push your Docker container image to AWS Elastic Container Registry:

docker push <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>

Make note of the ECR URI – this is the web address for the hosted app on AWS.

Note that there are typically a number of file changes to make after this first deploy - rather than rebuilding the Docker image though, redeploy instead with Serverless:

sls deploy

Any errors encountered in the process are often associated with the slug size / size of the container image. There are a few things you can do here:

1. Periodically delete old container images within the AWS (ECR) Console (in AWS Console > search Elastic Container Registry > select ECR repo > delete

2. Perform a Docker prune with:

docker system prune -a --volumes

If making changes to the forked GitHub repo, its recommended to open your GitHub source code in VS Code in GitHub by pressing the “.” shortcut after clicking on the filename in your repo. Also don’t forget to perform a pull request back into your EC2 instance (project root) after making the changes:

git pull [url of GitHub repo]

then redeploy with

sls deploy

3. Rebuild existing container image

As a last resort you can also delete your existing ECR container image and rebuild it from scratch with Serverless.

20 views0 comments

Recent Posts

See All
bottom of page