Run a Python Script Inside a Docker Container

Running a Python script or program on your host or workstation is always convenient; however, you might need to run your code inside an isolated environment like a container in some cases.

In this post, we will use the official Python Docker image to run a Python script that we will copy from our host (WSL, but if you are on a macOS or Linux, it will work).

Run Container

The first step we need to start with is the actual deployment of our Python Docker container. Let’s run the following command and deeply it.

docker run --name deploy01 -ti  python /bin/bash

Next, I will copy a Python script from my Docker host to the Python container.

docker container cp script.py  deploy01:/

In the last step, I will attach to the Python container and run the uploaded script.

docker attach deploy01

From the container, I will run the script.

python script.py

1 thought on “Run a Python Script Inside a Docker Container”

Comments are closed.