Tuesday, November 30, 2021

Run Python Application in Docker Container

In this tutorial you will see how to run a HelloWorld Python program using Docker or how to dockerize your Python application to run it with in a Docker container.

Steps to run Python hello world program with Docker

  1. Lets start by creating a separate directory where we can put the Python file and DockerFile.
  2. Create a Python file helloworld.py with in this directory.


    class HelloWorld:
        # method
        def display(self):
            print("Hello World Python Program from Docker")
    #creating object
    obj = HelloWorld()
    #calling method
  3. Create a file named Dockerfile with in the same directory. Follow the same name as given with 'D' capitalized and no extension. This is the file which defines the steps needed to create the image and run it.

    Steps for running any application in Docker is to create a Docker image and then create and start a container using that image. Dockerfile has the set of instructions to create the Docker image, each instruction in a Dockerfile creates a layer in the image.


    FROM python:3
    COPY . /src/pythonapp
    WORKDIR /src/pythonapp
    CMD ["python", "helloworld.py" ]

    Explanation for these steps is as given below-

    • Often, an image is based on another image, in our case initial step is to get Python. Note that Docker container runs in isolation as a stand-alone entity having the application code and the required dependencies to run the application. In order to run our code Python should be installed in the container. There already is a Python image hosted in Docker hub, path is https://hub.docker.com/_/python, that we can pull by giving the name of the image (python) and tag (which indicates Python version, which is 3 in our Dockerfile).
    • COPY instruction is used to copy files or directories from source path to the to the filesystem of the container at the destination path. In our Dockerfile files from the current directory (.) are copied to the path /src/pythonapp in the container.
    • The WORKDIR instruction sets the working directory
    • CMD is used to provide defaults for an executing container. One of the forms for providing CMD instructions is ["executable","param1","param2"]. This form is used in our Dockerfile where "python" is passed as executable and "helloworld.py" is passed as parameter making the command to be executed as python helloworld.py when the container is run.
  4. Build image

    To build an image from the Dockerfile command used is docker build. One of the option you can use with build command is -t which is used to pass a name for your docker image and optionally a tag in a name:tag format.

    Change directory to the path where Dockerfile is stored and run the following command.

    D:\NETJS\python-docker>docker build -t python-hello-world:1.0 .

    . means using the current directory as context

    Build command goes through the instructions in the Dockerfile one by one and executes them as you can notice from the screenshot given below. Three instructions out of 4 from the Dockerfile will be executed to create Docker image where as the fourth instruction which is a CMD instruction will be executed when container is run.

    [+] Building 115.6s (8/8) FINISHED
     => [internal] load build definition from Dockerfile                                                                                                         
     => => transferring dockerfile: 131B                                                                                                                         
     => [internal] load .dockerignore                                                                                                                            
     => => transferring context: 2B                                                                                                                              
     => [internal] load metadata for docker.io/library/python:3                                                                                                  
     => [internal] load build context                                                                                                                            
     => => transferring context: 357B                                                                                                                            
     => [1/3] FROM docker.io/library/python:3@sha256:f44726de10d15558e465238b02966a8f83971fd85a4c4b95c263704e6a6012e9                                            
     => => resolve docker.io/library/python:3@sha256:f44726de10d15558e465238b02966a8f83971fd85a4c4b95c263704e6a6012e9                                            
     => => sha256:f48ea80eae5a5683e2a734cf4697827339af3ced11e26d0ad58433ddf6fac24f 8.62kB / 8.62kB                                                               
     => => sha256:647acf3d48c2780e00cd27bb0984367415f270d78477ef9d5b238e6ebd5290da 54.93MB / 54.93MB                                                             
     => => sha256:b02967ef003473d9adc6e20868d9d66af85b0871919bcec92419f65c974aa8ce 5.15MB / 5.15MB                                                               
     => => sha256:e1ad2231829e42e6f095971b5d2dc143d97db2d0870571ba4d29ecd599db62cb 10.87MB / 10.87MB                                                             
     => => sha256:f44726de10d15558e465238b02966a8f83971fd85a4c4b95c263704e6a6012e9 2.60kB / 2.60kB                                                               
     => => sha256:9f4d271aecfec02809b04fa2367895c173cbf2ad03a9b94d7b385498a826d2ce 2.22kB / 2.22kB                                                               
     => => sha256:5576ce26bf1df68da60eeb5162dccde1b69f865d2815aba8b2d29e7181aeb62b 54.57MB / 54.57MB                                                             
     => => sha256:a66b7f31b095b7fa01d8ba10e600a192bab43a1311f50216cf6fa9a45d0f435e 196.50MB / 196.50MB                                                           
     => => sha256:05189b5b27621c90d2dc99c54b36bf5820bb377d96a9947cd1a81438eae46ddf 6.29MB / 6.29MB                                                               
     => => extracting sha256:647acf3d48c2780e00cd27bb0984367415f270d78477ef9d5b238e6ebd5290da                                                                    
     => => sha256:af08e8fda0d6cb4f0394e2d81bc7c3e00c44d04f28c79b195e3e20e53e9e29b8 19.11MB / 19.11MB                                                             
     => => extracting sha256:b02967ef003473d9adc6e20868d9d66af85b0871919bcec92419f65c974aa8ce                                                                    
     => => extracting sha256:e1ad2231829e42e6f095971b5d2dc143d97db2d0870571ba4d29ecd599db62cb                                                                    
     => => sha256:287d56f7527b558bbad5f0dd5f529eb1d161d549cd7634990b203ea1af123f23 233B / 233B                                                                   
     => => sha256:dc0580965fb6016fadf90963a71fdd042a099f6043e816155fb2388c2c78e6a7 2.35MB / 2.35MB                                                               
     => => extracting sha256:5576ce26bf1df68da60eeb5162dccde1b69f865d2815aba8b2d29e7181aeb62b                                                                    
     => => extracting sha256:a66b7f31b095b7fa01d8ba10e600a192bab43a1311f50216cf6fa9a45d0f435e                                                                    
     => => extracting sha256:05189b5b27621c90d2dc99c54b36bf5820bb377d96a9947cd1a81438eae46ddf                                                                    
     => => extracting sha256:af08e8fda0d6cb4f0394e2d81bc7c3e00c44d04f28c79b195e3e20e53e9e29b8                                                                    
     => => extracting sha256:287d56f7527b558bbad5f0dd5f529eb1d161d549cd7634990b203ea1af123f23                                                                    
     => => extracting sha256:dc0580965fb6016fadf90963a71fdd042a099f6043e816155fb2388c2c78e6a7                                                                    
     => [2/3] COPY . /src/pythonapp                                                                                                                              
     => [3/3] WORKDIR /src/pythonapp                                                                                                                             
     => exporting to image                                                                                                                                       
     => => exporting layers                                                                                                                                      
     => => writing image sha256:b84b09f7ce8fc351122d1df9e32f82f9b0aa5808a93123986f5921a61603c9e3                                                                 
     => => naming to docker.io/library/python-hello-world:1.0      
  5. Run Docker Image

    Once the Docker image is created successfully you can run it using the docker run command by providing the name of the image which you want to run. Note that a container is a runnable instance of an image so this process can also be described as creating and starting a container where command is run.

    D:\NETJS\python-docker>docker run python-hello-world:1.0
    Hello World Python Program from Docker   

    As you can see on running the image CMD instruction given in Dockerfile is executed and output is displayed.

That's all for this topic Run Python Application in Docker Container. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Run Java Application in Docker Container

You may also like-

  1. How to Install PostgreSQL on Windows
  2. Transaction Management in Java-JDBC
  3. Try-With-Resources in Java With Examples
  4. static Import in Java With Examples
  5. Private Methods in Java Interface
  6. How to Install Node.js and NPM in Windows
  7. Java String charAt() Method With Examples
  8. Spring Boot REST API CRUD Example With Spring Data JPA

No comments:

Post a Comment