Preparing Dockerfile for your model

This guide will help you to create Dockerfile for your model in easy steps.

Using Docker can help the users to easily deploy your AI model. The best way to understand the process is to look at actual Dockerfile and break it down. Here, we will look at the Dockerfile of 'Summer to Winter GAN' for reference.

1. FROM pytorch/pytorch
2. RUN apt-get update
3. RUN pip install certifi==2019.6.16 \
               chardet==3.0.4 \
               dominate==2.4.0 \
               idna==2.8 \
               numpy==1.17.1 \
               Pillow==6.1.0 \
               pyzmq==18.1.0 \
               requests==2.22.0 \
               scipy==1.3.1 \
               six==1.12.0 \
               torch==1.2.0 \
               torchfile==0.1.0 \
               torchvision==0.4.0 \
               tornado==6.0.3 \
               urllib3==1.25.3 \
               visdom== \
               websocket-client==0.56.0 \
4. ENV PYTHONPATH /usr/local:/model/:$PYTHONPATH
5. WORKDIR /model
6. COPY . .

Don't use line numbers in actual Dockerfile.

Let's break it down 👇

Statement - 1

FROM pytorch/pytorch

FROM instruction defines the base image for your Dockerfile. In above Dockerfile we are using pytorch base image which itself uses ubuntu as base. If your model uses tensorflow, you can use FROM tensorflow/tensorflow instead or search for other available base images from dockerhub .

Statement -2

RUN apt-get update

RUN instruction lets you execute any command in your environment's CLI shell. Ex - If your model requires to install some additional package using apt-get , you can use -

 RUN apt-get update && apt-get install -y \
             <package 1> \
             <package 2> \
             <package 3>

Statement -3

Here RUN instruction is used to install pip packages.

Statement -4

ENV instruction allows to change environment variables. Most probably, you won't need ENV if you simply want to share model on Dockship.

Statement -5

WORKDIR /model

As the name suggests, WORKDIR changes work directory. Think of it as 'cd' command's alternative for Docker .


COPY . .

COPY <src> <dest> copies content from your filesystem's \ to Docker's filesystem's \. In above example, COPY . . copies whole directory content into Docker's filesystem.

Test it yourself

Once you have created Dockerfile for your model, build its image and run container to verify if it's working properly. docker build -t <model_name> -f Dockerfile <path_to_model_folder> && docker run -it <model_name>

Using the Dockerfile shared above as a template and making necessary changes will make the process much easier for you.

Last updated