← Go Back

How to Run a Python File in Docker

First create a Dockerfile to generate a Linux image with the latest Python installed:

FROM python:3
WORKDIR /usr/src/app
# Copy the .py file we want to run to the image.
COPY script.py ./
# Run the file when starting the image in a container.
CMD ["python", "./script.py"]

Note that the script.py file we want to execute must be in the same folder as the Dockerfile. Then generate the image and start the container by running in the terminal:

docker build -t python-test .
docker run python-test

For other official Python images see this link.

docker