Running a Python script on Docker

Running a Python script on Docker

Introduction

I was searching for virtualization techniques and I learned about Docker. As I was digging into it, it seemed more interesting. After a little research, I ended up installing Docker in my windows machine. So, let’s start quickly.

WHAT IS DOCKER?

Docker is a software platform for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. And developing apps today requires so much more than writing code. Multiple languages, frameworks, architectures, and discontinuous interfaces between tools for each lifecycle stage create enormous complexity. Docker simplifies and accelerates your workflow while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project.

WHAT IS DOCKER FILE?

A Dockerfile is a script that contains collections of commands and instructions that will be automatically executed in sequence in the docker environment for building a new docker image.

SOME BASIC COMMANDS TO CREATE DOCKER FILE:

FROM - It sets the base-image for the new image that you want to create. The FROM instruction will initialize the new build-stage and must be located at the top of the Dockerfile.

With this instruction, you can add additional information about your Docker image, such as the version, description, maintainer, etc. The LABEL instruction is a key-value pair that allows you to add multiple labels and multi-line values.

RUN - This instruction used to execute command during the build process of the docker image. You can install additional packages needed for your Docker images.

ADD - The ADD instruction is used to copy files, directories, or remote files from URL to your Docker images, from the 'src' to the absolute path 'dest'. Also, you can set up the default ownership of your file.

ENV - The ENV instruction is used to define an environment variable that can be used during the build stage and can be replaced inline in many as well.

CMD - The CMD instruction is used to define the default command to execute when running the container. And the Dockerfile must only contain one CMD instruction, and if there is multiple CMD, the last CMD instruction will be run.

EXPOSE - This instruction is used to expose the container port on the specific network ports at runtime. The default protocol exposed is TCP, but you can specify whether the TCP or UDP.

ARG - The ARG instruction is used to define a variable that the user can pass at the built-time. You can use this instruction in the docker 'build command' during the build time using the '--build-arg variable=value' option and can be pass through the Dockerfile. Also, you can use multiple ARGs at the Dockerfile.

ENTRYPOINT - The ENTRYPOINT instruction is used to define the first and default command that will be executed when the container is running. Define the command to start your application with the ENTRYPOINT instruction.

WORKDIR - The WORKDIR instruction is used to define the default working directory of your Docker image. The RUN, CMD, ENTRYPOINT, and ADD instructions follow the WORKDIR instruction. You can add multiple WORKDIR instruction on your Dockerfile, and if there is doesn't exist, it will be created automatically.

USER - The USER instruction is used to define the default user or gid when running the image. The RUN, CMD, and ENTRYPOINT follow the USER instruction in the Dockerfile.

VOLUME - The VOLUME instruction ad used to enable access/linked directory between the container and the host machine.

Don't panic, all commands are not required for this project only a few of them i.e- FROM, LABEL, ADD, CMD are required for the creation of our docker file.

STEPS TO BE FOLLOWED: -

Step 1: Install Docker on your respective machine.

Step 2: Create a folder in the local directory. Like I have created a folder “goutam_docker”.

Step 3: Create a python script to be run and save it in the same folder.

Step 4: Create a Docker file using the above-mentioned commands.

Step 5: Building the image using the docker file.

          #docker image build -t <dockerid>/<imagename>:<tag_name> PATH_OF_DOCKERFILE

Step 6: Run the container.

#docker container run <dockerid>/<image_name>:<tag_name>

Step 7: Upload the image to DOCKER HUB

As I mentioned before, I am assuming that Docker is already installed on your computer. I will be using rhel8 OS and build a python container on it using Docker to run Python Scripts on the container.

Now let's start with Step 2.

  • Create a folder in the local directory which will contain our python file and docker file.
mkdir goutam_docker
  • Write the python script you want to run inside your container. In my case, I have written a simple pattern program. After writing the python program save it using .py extension. (pattern.py in my case).
gedit pattern.py

Now inside pattern.py file write your own python code. I am providing mine here.

n = int(input())
co = 0
for i in range(n):
    a = [chr(j + 49) for j in range(n - i)]
    aj = "".join(a)
    print(aj + " " * co + aj[::-1])
    co += 2
co -= 4
for i in range(n - 1):
    a = [chr(j + 49) for j in range(i + 2)]
    aj = "".join(a)
    print(aj + " " * co + aj[::-1])
    co -= 2
  • Now the next step is to create our docker file.
mkdir dockerfile
  • Inside the docker file write the following snippet.
FROM python:latest

LABEL version="0.0.1"
LABEL maintainer="goutam.ghadai2199@gmail.com"

ADD pattern.py /

CMD [ "python", "./pattern.py" ]
  • Now start your docker by the following command and check the status that your docker is properly started or not.
systemctl start docker

systemctl status docker
  • Now we can build our docker image. The command to build the docker image is given below.
docker image build -t gg2199/dock_image:0.0.1 /root/goutam_docker

Then it will take some time to build successfully. Here I am providing a screenshot of mine.

No alt text provided for this image
  • It will automatically download the base image from the docker hub and build your own custom docker image. The screenshot of the successfully build image is given below.
No alt text provided for this image
  • Now we can run our own docker image by the use of the following command.
docker container run gg2199/dock_image:0.0.1
  • The output of my python script is shown below.
No alt text provided for this image
  • As we have now successfully run python script on a container now we will upload our image in DOCKER HUB. To upload your image you must log in to Docker using the command docker login and giving it the required credentials.
docker login

  • Now we will push our image to the DOCKER HUB by executing the following command.
docker image push gg2199/dock_image
  • Now we can go to the docker hub and check our image.
No alt text provided for this image

In the above screenshot, it is clearly visible that our image is successfully uploaded to the DOCKER HUB.

THANKS FOR READING.

FOR ANY QUERIES CONTACT - goutam.ghadai2199@gmail.com


To view or add a comment, sign in

More articles by Goutam Kumar Ghadai

Others also viewed

Explore content categories