Open In App
Related Articles

How to Run a Python Script using Docker?

Improve Article
Improve
Save Article
Save
Like Article
Like

Docker helps you to run your python application very smoothly in different environments with out worrying about underlying platforms. Once you build an image using dockerfile you can run that image where ever you want to run. Docker image will help you to package all the dependencies required for the application which will used for further while running the container.

What is Docker?

Docker is a set of platforms as a service (PaaS) products that use the Operating system level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than a virtual machine. To know more about docker refer to Introduction to Docker.

Key Terminologies

Docker mainly depends on the following three components as mentioned below.:

  1. Dockerfile: Dockerfile is an source code of an docker image. It consists of set instructions to build an docker image.
  2. Image: The instructions which are given in the dockerfile are called layers in the docker image. Docker image consists of several layers according to the no.of instructions you have given.
  3. Container: Docker image helps to build docker containers which are light weight and easy to portable from one platform to another platform. Docker container is an running instance of docker image.

Docker components

Dockerfile Instruction

There is an Docker syntax that you need to follow while writting the dockerfile as shown below:

INSTRUCTION arguments

Most frequently used instructions in dockerfile are as follows:

  1. FROM
  2. RUN
  3. ADD
  4. ENV
  5. ENTRYPOINT
  6. CMD

This are the some of the instructions which are regularly used in dockerfile to know more about the syntax of dockerfile refer to What is Dockerfile Syntax?.

Docker Commands

Docker commands make your work easier which will help you to manage the docker containers like removing containers, running docker image as an container,and managing the containers and so on some of the most commonly used docker commands are.

  1. docker run
  2. docker ps
  3. docker stop
  4. docker rm
  5. docker images
  6. docker pull

To know more about docker commands refer to the Docker – Instruction Commands.

Installing Docker

To run docker container first you need to install docker in your local machine you can install docker in different operating systems

Creating Dockerfile For Python Application

Step 1: Creating the Files and Folders

We will be creating a Folder docker_2 at the desktop location in our PC . Inside the Folder another Folder called docker_assignment is created. Then two files dockerfile and test.py are created in this folder.

Dockerfile

Folder 

Step 2: Creating the Dockerfile

Inside the dockerfile we will start by first taking the python base image from docker hub. A tag latest is used to get the latest official python image. It is very important to set your working directory inside your container. I have chosen /usr/src/app. All commands will be executed here as well as the images will be copied here only.

I have then copied the test.py file from my pc to the container current working directory(./ or /usr/src/app)  by using the COPY command. 

#Deriving the latest base image
FROM python:latest


#Labels as key value pair
LABEL Maintainer="roushan.me17"


# Any working directory can be chosen as per choice like '/' or '/home' etc
# i have chosen /usr/app/src
WORKDIR /usr/app/src

#to COPY the remote file at working directory in container
COPY test.py ./
# Now the structure looks like this '/usr/app/src/test.py'


#CMD instruction should be used to run the software
#contained by your image, along with any arguments.

CMD [ "python", "./test.py"]



Build The Docker Image For Python Application

Step 1: Building the Docker Container

After you have created both the Python script and the Dockerfile, you can now use the Docker build command to build your Docker Image.

Here -t is for adding tags so as to identify your image easily.

docker image build -t python:0.0.1 /home/roushan/Desktop/docker_2/docker_assignment





Building docker image

Step 2: Verify the Image Build

After you have built your Docker Image, you can list all the Images to check whether your image has been successfully built or not.

docker images





You will find your Image name listed here and with the tag name, you can easily find it.

Docker images

Run The Docker Container For Python Docker Image

Step 1: Running the Docker Container

Now, you can use the Docker run command to run your Docker Container.

docker run python:0.0.1





Running Docker container

After running the Docker Container, you will see the output printed after adding the two numbers.

Conclusion

To conclude, in this article, we saw how to build a simple addition Python script and run it inside the Docker Container.

FAQs On Running Python Application Using Docker

1. Can I Run An Application On Docker?

Yes you can run you application by using docker by using docker image and docker run command.

2.Can Docker Run A GUI?

We can easily run the most common GUI applications without getting into trouble inside a Docker Container.

3. Is Docker Only For Web Apps?

You can run any type of application as an container using docker. It is well suited for micro service applications


Last Updated : 20 Sep, 2023
Like Article
Save Article
Similar Reads
Related Tutorials