How to run my Python script on Docker?

| |

Home » How to run my Python script on Docker?
Spread the love

Writing a Python script seems easy enough. All you need to do is open up your favorite text editor, type in the code, and run the script. Well, it sounds easy enough at first, but you have to remember that your code will need to work on any PC that you are using. If you are working in an environment where it doesn’t have the right libraries to run the program, then you are out of luck. With Docker, you can use containers to run your code. Docker is similar to what virtualization has been doing for a while. Virtualization is purely based on the fact that we want to make more efficient use of the hardware that we have access to, and that is the main purpose. Docker’s main purpose is to help us streamline the process of software development and its deployment. In this article, I’ll show you how to run my python script on docker?

What is docker & why it’s important:

Docker is a comprehensive platform designed to provide incredibly simple and straightforward solutions for application containers. The development of tools has become much more complicated in recent years, with developers having to use multiple languages, frameworks, architectures, and discontinuous interfaces in what feels like an endless cycle. Development teams working on big projects can easily become mixed up over which technologies are being used when. Docker aims to solve these issues by simplifying processes whilst ensuring that each developer has total freedom when choosing the tools they want to use throughout application creation. With this new tool, any application can be deployed to any operating system!

How to install docker:

Installing Docker is pretty easy you just have to go to this link, download docker for your respective operating system, and install it

In windows, you need to install one more software for running the docker desktop software called wsl_update_x64 you can download that from here.

Note: I am using visual studio code, code editor for all of the processes. In VS Code you can install the official docker extinction provided by Microsoft. It gives you nice features line debugging and autocompletion. This will make it easy for me on how to run my python script on docker?

Note: I am using Visual Studio Code, my code editor of choice when working with Docker. It gives a developer nice features like debugging and autocompletion. In VS Code you can install the official docker extension provided by Microsoft directly from the Visual Studio extension which makes everything a lot more simple.

how-to-run-my-python-script-on-docker

How to Containerize Python Applications:

Step 1: Create a folder in the local directory. I have created a folder named “python-docker”.

Step 2: Create a python script (main.py) to be run and save it in the same folder.

To demonstrate, I went ahead and added some custom Python code to the main.py file. The script will show a basic orange circle in the middle of the screen using the pygame library. You can run any script/code you want:

# Simple pygame program

# Import and initialize the pygame library
import pygame
pygame.init()

# Set up the drawing window
screen = pygame.display.set_mode([500, 500])

# Running the game until the user asks to quit
running = True
while running:

    # creating an if statement for the close button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Filling the background color with a gray
    screen.fill((55, 55, 55))

    # Drawing a solid purple circle in the center
    pygame.draw.circle(screen, (0, 0, 251), (251, 251), 74)
    # Flip the display
    pygame.display.flip()

# Done! Time to quit.
pygame.quit()

Step 3: Create a file name “dockerfile” in the same directory as our python script.

A Dockerfile is a script that contains code that’s executed in sequence to produce a new Docker image, which is essentially an executable collection of files that are assembled inside of another set of executable files. In the docker file, we have to differentiate between three different things. 

  1. Docker file: a docker file is a blueprint for building docker images.
  2. Docker image: a docker image is a simple template for running docker containers.
  3. Docker container: a docker container is the actual running process where we have our package project.

To create a Dockerfile to build Docker Images, you’ll need to use need to know some of the basic commands

FROM: The FROM instruction is crucial because it sets the foundation for the base image that you are making. And must be placed at the top of the Dockerfile.

RUN: This command is a step in the build process of a Docker image. It’s a way to install additional packages needed for your Docker images to run well.

CMD: The CMD instruction is used to define the default command that should be executed when running the container. It doesn’t matter how many times this command gets added because only the last one will be run.

ADD: ADD allows you to copy a file from URL to your Docker images, from the ‘src’ to the absolute path ‘dest’. Also, you can set up default ownership on new files.

Writing Code in dockerfile:

#Specifying the base image
FROM python:3.10
#here the dockerfile is pulling the python 3.10 from docker hub which already has python installed so we have all the things we need to have python in our container.

ADD main.py.
#Here we added the python file that we want to run in docker and define its location.

RUN pip install requests pygame
#Here we installed the dependencies, we are using the pygame library in our main.py file so we have to use the pip command for installing the library

CMD [ "python3" "./main.py" ]
#lastly we specified the entry command this line is simply running python ./main.py in our container terminal

Step 4: Create a docker image.

The following command will create the docker image. You can use any name for the docker image I am using python-game:

docker image build -t python-game 
how-to-run-my-python-script-on-docker

To verify the images have been created, run the following command:

docker images
how-to-run-my-python-script-on-docker

You can also configure and access your images and files through the docker desktop software and upload them to your Docker Hub by creating and logging into your account.

how-to-run-my-python-script-on-docker

For running the docker image use the following command:

docker run python-game
how-to-run-my-python-script-on-docker
how-to-run-my-python-script-on-docker

Final Words

If you’re looking to build an application for yourself or your clients, you’ll need to stress test the product and deploy it in a live environment before its official launch date. There are many ways in which you can go about doing this, but one of the most efficient ways is by deploying your app to the cloud with Docker. In summary, Docker has become a must-have development tool for Python developers and goes well beyond delivering services to clients. With its production-ready architecture, it provides an impressive infrastructure from experience. So, this is how to run my Python script on Docker.

We hope you enjoyed our article about how to run Python apps in Docker containers. With this knowledge, we know that you can enjoy the benefits of using Docker for your Python applications.

Here are some useful tutorials that you can read:

Previous

Detect the Age and Gender of a Face using OpenCV

Emotion Detection using Python

Next

Leave a Comment