How to Run a Python Script in Docker with NumPy

Published Nov 13, 2021  ∙  Updated May 2, 2022

Running a quick Python script in Docker is quite simple, and adding NumPy to the mix is just as simple.

Dockerfile changes

Suppose we have a Dockerfile in the same directory as, the script we’d like to run.

We simply need to add a RUN command to install numpy.

FROM python:latest
WORKDIR /usr/app/src
RUN pip install numpy
CMD ["python", ""]

Build the image

In the directory of the Dockerfile, we can run a docker build command to build the image.

docker build -t numpy-script .

Start the container

We can then start the container with docker run.

Notice how we don’t copy the script into the container in the Dockerfile. Instead, we can mount our current directory $(pwd) (the directory with the script) to the container, and it’ll run whatever version of the Python script is on our local machine.

docker run -v $(pwd):/usr/app/src -ti numpy-script

This allows us to avoid building and running every time we make a change to the script.

We can simply run this docker run command to execute a single run of the script