How to run Python Scripts inside Docker Containers?

Posted in /  

How to run Python Scripts inside Docker Containers?

Vinay Khatri
Last updated on May 22, 2024

    If you're working on several Python projects in a local environment, it can be difficult to keep track of anything. It can take some time to start a project because we need to handle versions, dependencies, and configurations.

    Initially, we would install all project specifications on our local machine and then concentrate on writing code. However, having multiple projects running in the same environment quickly becomes a challenge because we risk experiencing conflicts in configuration and dependency.

    Furthermore, when working on a project with others, we must share our environment. To do so, we must describe our project environment in a way that allows it to be easily shared. Creating isolated production environments for each project is a good way to do this.

    Let’s suppose you have a Python script or a Python application such as Flask, it might fetch data from a particular source, scrape websites, or send emails, or it may do anything. Deploying such scripts and applications inside Docker containers instead of running them on local machines is a better way. Suppose in our particular application you might use Python 2.7, and in other applications, you need Python 3. Hence, this would lead to several dependency issues as well.

    You might think that we can easily install several VMs on our host and run each application on each one of them. However, VMs require a lot of hardware resources, and for such simple applications, you might now want to waste them. Moreover, Docker containers are highly portable and scalable.

    You can easily scale your applications to support millions of users. Hence, the Docker official registry called Dockerhub has tons of official and vendor-specific Docker images that you can use to access a python environment. Moreover, both the Docker community and Python core developers maintain their own sets of repositories in the Docker registry.

    In this guide, we will discuss how to use a Python release candidate image to get access to the Python REPL directly, which can be used for simple tasks like learning Python or testing an application. We will also try to pull the official Python image and use it as a base image to run a Python script using a Dockerfile. So without any further delay, let’s get started.

    Create a Container for Python REPL

    Let’s try to run a Python Release Candidate image by pulling it straight from the Dockerhub. Now, there are two ways to do it. Either you can first pull an image and then run the Docker run command on it to access the interpreter. Or you can directly run the Docker run command.

    In this case, the Docker daemon will first check whether your local machine already has the same image by comparing the digests of the image. If it finds one, it will either try to update it or if already in the newest version, will simply start the Python REPL. Let’s try to use the second approach.

    $ docker run -it --rm python:rc

    Here, we have used the -i and -t options to run the container interactively using a terminal driver so that we can input our commands as well. We have also used the --rm option, which automatically removes the container once we exit.

    Finally, we have specified the image name as python with a tag RC which stands for release candidate. Let’s try to execute this command.

    Docker Containers

    We can see that after it has pulled the Python RC Image from Dockerhub, it has automatically started the Python REPL. Before pulling the Image, the Docker daemon checks for a similar image in our machine, and if one is not found, it will try to pull it. Let’s try to interact with the REPL to confirm.


    We have used a simple print statement to print a message.

    Running a Python Script using Dockerfile

    We can also specify instructions inside a Dockefile to create a Python environment and run a Python script or application inside it. Let’s try to create a Dockerfile first.

    FROM python:latest
    WORKDIR /usr/src/app
    COPY . .
    RUN apt-get update
    CMD [""]
    ENTRYPOINT ["python3"]

    In the above Dockerfile, we first used the FROM instruction to pull a Python image with the latest tag from the official Docker registry. Then, we used the WORKDIR instruction to set a default working directory.

    After that, we used the COPY instruction that will copy the build context, which includes the Dockerfile and the Python script that we want to run. Post that, we used the RUN instruction, which instructs the Docker daemon to update the container when the container is run.

    Finally, we have used the CMD and ENTRYPOINT instructions to tell the daemon to run the Python script called using the Python3 command when the container is started. Our directory structure is -

    Directory structure

    Let’s use the Docker build command to build our Docker image.

    $ docker build -t pythonimage:v1 .

    Here, we have used the -t option to specify a tag for the image. In the end, we have used a dot to tell the daemon that the current directory is the build context that contains the Dockerfile.

    $ docker build -t pythonimage-v1 .

    Now, let’s verify whether the image has been built or not by listing all the images.

    $ docker images

    $ docker image

    You can see that the image has been built successfully. Let’s try to run a container associated with the image using the Docker run command below.

    $ docker run -it --rm pythonimage:v1

    The daemon should create and run the container, execute the script and display the message written using a print statement inside the python script, exit the container, and remove it automatically. Let’s verify the output.

    $ docker run -it --rm pythonimage:v1

    You can see that this is what exactly it does. Let’s list all the active containers to verify whether the container has been removed or not.

    $ docker ps -a

    You can see that there are no active containers running.

    $ docker ps -a

    Wrapping Up!

    To conclude, in this article, we have discussed how to create a python environment using Docker, run python scripts by specifying instructions inside a Dockerfile, and also access the Python REPL by creating a container associated with the Python release candidate image.

    We certainly hope that through this comprehensive guide, you will be able to run your own python scripts and applications inside Docker containers easily.

    Happy Learning!

    People are also reading:

    Leave a Comment on this Post