Run a Python Script in a Linux Docker Container

This guide intends to teach you to Run a sample Python Script in a Linux Docker Container. As you know, Docker is a great choice for containerizing apps and running them in a special environment. You may have a Python script that you want to run in a Docker container. For this purpose, you can use a Dockerfile to define your Python image.

Follow the rest of the article to see how you can do it. Here we run Docker in Ubuntu 22.04 as an example to display how you can run a sample Python script in Linux Docker Container.

Steps To Run a Python Script in a Linux Docker Container

Because we use Ubuntu 22.04 as our Linux server, you can use the following requirements for this server which are Initial Server Setup and Docker installation on Ubuntu 22.04.

You must log in to your server as a non-root user with sudo privileges. To do this, you can visit this guide on Initial Server Setup with Ubuntu 22.04.

Also, you must have Docker installed on your server. To do this, you can check this guide on Install Docker on Ubuntu 22.04.

Now proceed to the following steps to complete this guide.

Step 1 – Create a Python Script in Linux

First, you must create the Python script that you want to use in the Docker Container. In this guide, we create a Hello World script as an example for you. To do this, you can use your desired text editor like Vi Editor:

vi hello.py

Then, copy the following sample Python script inside the file:

print("Hello, world!")

When you are done, save and close the file.

Step 2 – Create a Dockerfile for Python Script

At this point, you must create a Dockerfile for your Python script. Dockerfile will help you build your Pyhotn image and run your script in a Docker container. Just remember to create the Dockerfile inside the directory where you have defined your Python script.

To create your Dockerfile, you can use your desired text editor, here we use vi:

vi Dockerfile

In the file, you can copy the following content as an example:

# Use an official Python runtime as a parent image
FROM python:3.11

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Run hello.py when the container launches
CMD ["python", "hello.py"]

When you are done, save and close the file.

As you can see, we use Python 3.11 as the base image. Copy the contents into the /app directory and run the hello.py script using the CMD instruction.

Step 3 – Build and Run Python Script in a Docker Container

At this point, you must build your Docker image for Python. Remember to do these steps in the same directory as your Dopckerfile. To build your Docker image, you can use the following command and set a name for it, here we named it testapp.

docker build -t testapp .

This command will the base image that you have defined in the Dockerfile to build your Docker image.

When it is finished, you will get the following output:

Output
[+] Building 0.6s (8/8) FINISHED                                 docker:default
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 364B                                       0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for docker.io/library/python:3.11             0.5s
 => [1/3] FROM docker.io/library/python:3.11@sha256:02808bfd640d6fd360c30  0.0s
 => [internal] load build context                                          0.0s
 => => transferring context: 9.96kB                                        0.0s
 => CACHED [2/3] WORKDIR /app                                              0.0s
 => [3/3] COPY . /app                                                      0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:61136d3236e6b4aab3eabcc5df8ea0771ad9cc07b7b8d  0.0s
 => => naming to docker.io/library/testapp                                 0.0s

Finally, you can use the following docker command to run your Python script:

docker run testapp

This will run and print your Python script in a Docker Container:

Output
Hello, world!

Conclusion

At this point, you have learned to Build a sample Dockerfile for Python and Run a Python Script in a Linux Docker Container. As you can see working with Docker is so easy and useful. If you are looking for any help or have an idea, feel free and comment for us.

Hope you enjoy it. For more guides on Docker, you can visit the Docker Tutorials.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!