Python Within Docker: Streamlined Creation

Gage Chappell
4 min readMay 4, 2024

Today, I will be showing you how to set up and run a single simple python script through Docker. For this demonstration, I will be using docker desktop installed on a windows-based machine. I will not be creating a full ‘Dockerfile’, as it is not required for a single file project like the one below and aimed for significantly larger projects than this.

Docker Download Page

The first step is to go to Docker Desktop: The #1 Containerization Tool for Developers and click Download for Windows. Set everything up as recommended by the installation wizard. Make sure to accept the administrator requests that will pop up, and make sure to restart to finish the installation process.

Visual Studio and Python Download Pages

The second step is to download Visual Studio, and Python. You can find these at Visual Studio Code Editor (microsoft.com) and Python.org respectively. I also recommend setting them up as recommended by their installation wizard. Make sure to accept the administrator requests that will pop up.

Visual Studio Project Screen

Open Visual Studio and create a new python project. For my example, I am using the ‘Python Application’ template, as shown in the photo above. I did not test the others but this was listed as being specifically for a command-line application.

For this example, I made a simple Visual Studio script to output two lines of code within the command prompt in windows. Of course, this is not all that can be done with python, but it serves as a good introduction and proof of the pieces of software working with each other. Be sure to save and take note of where the file is located in your filesystem, you *will* need this information later.

Now, the command prompt fun begins. Make sure Docker Desktop is up and running, and then open the Windows Command Prompt. To do this, press Start, or the windows key, and type in ‘cmd.’ I will provide photos along with each of the individual commands required below.

The sixth step is to use the command “docker pull python” as shown on python — Official Image | Docker Hub. This will pull the image for python off of docker’s website, and it is important to realize that this is different from the python installation we did above, as this interfaces directly with docker.

You will then use a variation of

“docker run -it — rm — name my-running-script -v “$PWD”:/usr/src/myapp -w /usr/src/myapp python:3 python your-daemon-or-script.py”

to path to the location of the python file we made in Visual Studio in a prior step. For me, this pathway would read out as the following:

“docker run -it — rm — name my-running-script -v “C:\\users\\low-l\\documents\\visualstudio\\pythontestscript\\pythontestscript”:/pythontestscript -w /pythontestscript python:3 python pythontestscript.py”.

You then will enter this in the command prompt and let it run, just as you did previously with step six.

Python Script Results

As we see by the results of this script, the lines of code we received came from our python script. This means that we have successfully run a single python script through docker! While it may not be much in my example, this could be much more if there were more detail, and not a ‘test’ file with two printed lines. It’s important to not think of the smaller examples and to think more of what they can become when combined into something greater.

Errors:

If you get the error message ‘Invalid Reference Type’ then the odds are you have the pathway to your python file incorrectly listed, ensuring there are no capital letters. You may also need to do double backslashes (\\) after the drive path (C:\\ or D:\\.) This is the only one that gave me any sort of trouble during the setup for this article.

--

--