Deploying from Sagemaker Studio Lab using Docker

Austin Lasseter
4 min readJul 3, 2022

--

In a previous blog post, I talked about how to deploy applications using the Heroku CLI. The purpose of this post is to demonstrate deploying a simple python app from Sagemaker to Heroku using Docker instead of the CLI.

Since Heroku is not a universally accepted tool across the industry, learning to use the CLI is not an easily transferable skill like Docker deployment. While this method has its advantages, it also has some serious disadvantages compared to Docker. These disadvantages are described in an excellent summary for Django. In addition, deployment using the CLI has a 500 MB limit on the slug size of the deployed application, which can be a serious setback when working with machine learning applications and their dependencies. Docker has no such restriction.

All the files for this example can be found in this github repository. The deployed app can be viewed here.

Fire up your Sagemaker instance.If you haven’t already done so, check out my other blog posts about getting started with SMSL and pulling your first repo from Github.

Clone the repo in terminal and cd into the directory. All the files you need are included in the repo. If you’re not familiar with app.py check out my post on this topic.

The contents of Dockerfile should look like this (replace the 3 instances of the name of the directory 207-xkcd-radio-docker with whatever directory Dockerfile is located in).

FROM python:3.10-slim
ARG port
USER root
COPY . /207-xkcd-radio-docker
WORKDIR /207-xkcd-radio-docker
ENV PORT=$portRUN apt-get update && apt-get install -y --no-install-recommends apt-utils \
&& apt-get -y install curl \
&& apt-get install libgomp1
RUN chgrp -R 0 /207-xkcd-radio-docker \
&& chmod -R g=u /207-xkcd-radio-docker \
&& pip install pip --upgrade \
&& pip install -r requirements.txt
EXPOSE $PORT
CMD gunicorn app:server --bind 0.0.0.0:$PORT --preload

Note that we are using python:3.10-slim. It’s necessary to install a version of python that is compatible with Heroku; that list is continuously updated here. Slim indicates a Docker image that’s compatible with ubuntu and has python installed.

Make sure that heroku.yml includes the following; you shouldn’t have to modify this code.

build:
docker:
web: Dockerfile

Initiate a python virtual environment in terminal like this:

python -m venv env
source env/bin/activate

Now you’ve activated the virtual environment. Notice that .gitignore includes an entry for env/ which will make sure you don’t push all the installs to github by accident. You can try running the application with python app.py and it will tell you the missing dependencies. These are just Dash and Gunicorn. You can install these on Sagemaker as follows (but this is unnecessary for the purpose of deployment):

pip install -r requirements.txt

Note that it’s not possible to view the running app on Sagemaker because it has no browser. It’s also not possible to install Docker desktop on Sagemaker. But this won’t prevent you from deploying successfully to Heroku as long as you have the CLI installed.

Log in to Heroku and create your new application as follows. Replace xkcd-radio-docker with an app name of your choice.

heroku login -i
heroku create xkcd-radio-docker

Commit any changes to your git index.

git add .
git commit -m 'description of any updates made.'

Set the stack of your app to container. Otherwise Heroku will choose the default stack type and will ignore Dockerfile, and you’ll get an H14 error (“No web processes running”).

heroku stack:set container

Now deploy the app to Heroku. This may take a few minutes.

git push heroku main 

Once the application is fully deployed you should see the following in terminal.

remote: Verifying deploy... done.

Don’t forget that you can check the logs if you need to do some troubleshooting.

heroku logs --tail

And that’s it! You can view the finished app here: https://xkcd-radio-callback.herokuapp.com/

--

--