Tutorials

From Stat DS Computing Wiki
Revision as of 03:35, 29 June 2026 by Ajshen (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


This tutorial is supposed to serve as a simple self-contained example that shows how to build a Docker image for a project that might be of interest in our department: a simple application using PyTorch.

To complete this tutorial, we will rely on conda Python version 3.8 or later. You also need to have installed Docker (see download and install Docker).

Example application

Create a directory named docker and create a conda environment with Python and PyTorch installed in it. Don't forget to activate the environment before installing packages!

Then, create a test_docker.py file and add the following simple lines:

import torch

a = torch.Tensor([1, 2, 3, 4, 5])

if __name__ == '__main__':
    print(a)

Inherit from an existing image

Create a Dockerfile for Python

Inside the docker directory create a Dockerfile (simply by tipying touch Dockerfile) and add a line that tells Docker what base image to use for the application.

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

Docker images can inherit from other images. Therefore, instead of creating your own base image, you can use the official Python image that has all the tools and packages needed to run a Python application.

To make things easier when running the remaining commands, create a working directory. This instructs Docker to use this path as the default location for all subsequent commands. This means you can use relative file paths based on the working directory instead of full file paths.

WORKDIR /path/to/your/project/dir

Now we would like the Docker image to install all the necessary packages we have in the environment. When installing Python, conda automatically downloads the pip package manager as well. We can then use it to create a requirements file by typing

pip list --format=freeze > requirements.txt

in the terminal, within the docker directory. Then we need to make sure the requirements file will be in the image. The COPY command takes two parameters. The first parameter tells Docker what file(s) you would like to copy into the image. The second parameter tells Docker where to copy that file(s) to. For this example, copy the requirements.txt file into the working directory:

COPY requirements.txt requirements.txt

With the requirements.txt file inside the image, you can use the RUN command to run pip install. This works exactly the same as running pip install locally on a machine, but this time pip installs the modules into the image.

RUN pip install -r requirements.txt

At this point, you have an image based on Python version 3.8 and have installed the dependencies. The next step is to add the source code into the image. Use the COPY command as with the requirements.txt file. 

COPY test_docker.py test_docker.py

Now, tell Docker what command to run when the image is executed inside a container using the CMD command.

CMD ["python", "test_docker.py"]

The complete Dockerfile should look like this:

# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /path/to/your/project/dir
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY test_docker.py test_docker.py
CMD ["python", "test_docker.py"]

Build an image

Now that you’ve created the Dockerfile, let’s build the image. To do this, use the docker build command. The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The Docker build process can access any of the files located in this context.

The build command optionally takes a --tag flag. The tag sets the name of the image and an optional tag in the format name:tag. Don't forget to open the Docker Desktop application before running the build command, otherwise the Docker deamon will not be running and the command will not work.

If everything went well, you should see something like this:

RTENOTITLE


Run the image

Now that we have an image, we can run that image and see if our application is running correctly.

A container is a normal operating system process except that this process is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter which is the name of the image. Let’s start our image and make sure it is running correctly. Run the following command in your terminal.

docker run test-docker

If everything goes well, you should see the desired output printed in the terminal (in this case, a simple PyTorch Tensor).

Creating new base images

Most Dockerfiles start from a parent image. If you need to completely control the contents of your image, you might need to create a base image instead. Here’s the difference:

  • A parent image is the image that your image is based on. It refers to the contents of the FROM directive in the Dockerfile. Each subsequent declaration in the Dockerfile modifies this parent image. Most Dockerfiles start from a parent image, rather than a base image. However, the terms are sometimes used interchangeably.
  • A base image has FROM scratch in its Dockerfile.

See this link for details.