Docker Python is an open-source tool and a standard shipping container. This tool is used for automating the deployment of any application inside a software container.

Consider a scenario when we develop our application providing all the dependencies like database, web server, and other types of libraries and notice that our web application is only working on our computer, but it is not working either on the dev or QA’s machine or on the staging server. Generally, to avoid such unexpected errors, we use the VM. However, the main problem is that an “extra OS” on top of the host operating system adds gigabytes of space to our project. As a result, we need to pay the cloud-based server providers for this extra space. 

Docker shares all the kernel OS across all the containers running as the host OS’s separate processes to eliminate all these extra storage problems.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Importance of Docker

There are many reasons for using docker, such as:

  • Fast development process.
  • Easy to scale.
  • Easy and clear monitoring process.
  • It shows the same application behavior in all the servers such as local machine server, dv server, production server, and staging server.

Installation

We need to run all the following commands as roots to run Docker in Linux.

sudo usermod -aG docker $(whoami)`  

Terminologies

We must be familiar with some terminologies to get started with Docker.

  • Container - For encapsulating the required software, we need this running instance.
  • Image - This is the basic element for every container.
  • Port - A TCP/UDP port. This can be exposed to the outer world (accessible from the host OS) or connected to the other containers.
  • Volume - The shared folder can be described through this. 
  • Registry - It is the docker image storing server

Now let us discuss Docker Hub:

Docker Hub

Docker hub is provided by Docker Inc. provides. It is a registry with a web interface. A huge amount of Docker images are stored here with different software. We can say Docker hub is an official source for the images of Docker and the main Docker team made this with the association of original software manufacturers.

The official images list all the potential vulnerabilities, and all this information is only provided to users who are logged in.

There are two types of accounts available in Docker hub:

  • a free account 
  • a paid account

Only one private image is available for an account, whereas we can have an infinite number of public images per account, which are for free.

Dockerfile for Python

  • Dockerfiles in Python enables us to generate our custom images. 
  • All the descriptions are provided to the image creator softwares by these dockerfiles. 
  • All the required instructions are provided to the docker files for specifying what environments to use and what specific commands to run.

Become a Certified UI UX Expert in Just 5 Months!

With The Best-in-class UI UX ProgramExplore Program
Become a Certified UI UX Expert in Just 5 Months!

How to Create a Docker File

We have to start with a new and clean directory. In this context, the directory can be defined as a container containing all the things required to build our image.

You can name the directory whatever you want.

Next, we have to create a text file in our directory called Dockerfile. The files need to be saved as “All types” in windows, and we should also put our filenames within the quotes so that automatically appending the extension can be avoided. You can use the text file editor of your choice, such as Sublime, Notepad++, nano, etc.

As our launching point, we can use our basic Python 3 image; we need to run the following command.

FROM python:3

Then we will run our basic python script and name the script file as my_script.py. For this, we first need to add the script to our Dockerfile.

ADD my_script.py /

The script that we have created is dependent on the Python pyStrich library.

Generally, 1D and 2D barcodes are generated by the Python pyStrich library.

So we have to install this library before running our script file called my_script.js. 

Add this to our Dockerfile to install random:

RUN pip install pystrich

To execute the script, add the following line to our Dockerfile:

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

Now the Dockerfile of ours should appear as shown below:

FROM python:3

ADD my_script.py /

RUN pip install pystrich

CMD [ "python", "./my_script.p

Here, FROM tells Docker which image you base your image on. Here, in this example, Python 3.

RUN tells Docker about those additional commands that are to be executed.

CMD tells our Docker to execute all the commands soon after loading the image.

The Python script called my_script.js, which we have created earlier, should look something like this:

# Sample taken from pyStrich GitHub repository

# https://github.com/mmulqueen/pyStrich

from pystrich.datamatrix import DataMatrixEncoder

encoder = DataMatrixEncoder('This is a DataMatrix.')

encoder.save('./datamatrix_test.png')

print(encoder.get_ascii())

From our Dockerfile, we will first build our image and then run it.

docker build -t python-barcode .

Now let us see how we can run our image:

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

Run Image

After successfully building our image, we can run it as a container. To view all the images, we need to run the command docker images in our terminal.

Check an entry for “python-barcode”. Run the new image by entering:

docker run python-barcode

Docker_Python

We can avoid writing the complete Dockerfile if we want to run only a single script with a single file.

Example:

Let us suppose we are storing my_script.py in /usr/src/widget_app/, and we want to name the container as our-first-python-script.

In Python 3, we can write the following command.

docker run -it --rm --name our-first-python-script -v "$PWD":/usr/src/widget_app python:3 python my_script.py

And for Python 2, we can run the following command:

docker run -it --rm --name our-first-python-script -v "$PWD":/usr/src/widget_app python:2 python my_script.py

Now let us go through some more instructions about creating the Dockerfile:

Creating a Dockerfile

First, we have to ensure that no extension is added to the Dockerfile.

We have to familiarize ourselves with the Dockerfile content, but it doesn’t mean that we have to read all the contents of the Dockerfile. The installation of redundant software can be avoided. 

All the run commands should be written appropriately by basing all the images on only one Linux distribution that is not enforced by the Docker hub. If all the Debian-based distributions, such as Ubuntu, Debian, Mint, etc., are used by us, then app-get is required to be called by us to install the software. There are also some Red Hat-based distributions available which we need to use yum.

Sometimes we might end up starting with an unfamiliar base image, i.e., if you use CentOS and want to run a Python installation, the Python image extends Debian Jessie. Hence, you need to use caution in writing your RUN directives. Debian does not pose too many challenges if we maintain familiarity with Ubuntu.

In our build directory, putting any unused file must be avoided.

Note: We should not attempt running a script requiring dependencies using the Alternative method unless those dependencies come with the bare Python installation.

Now let us see how we can delete our Docker containers.

How to Delete Docker Containers

Open the Docker console and run the following command to see all containers.

docker ps

# OR #

docker ps -a  # to see all containers, including those not running

Delete Single Container

To delete a single container, we need to run the following command docker ps -a and retrieve container id.

After that, just run docker rm container_id.

The above command will just remove the container of that particular container id.

Delete All Containers

To delete all the containers,  we need to run the following command.

$ docker ps -q -a | xargs docker rm

Here -q stands for the container ids of all the containers and -a for all the containers.

Deleting Docker Images

Delete All Images Without Tags

Docker marks images without tags with "<none>", so we need to process only those images. 

docker rmi $(docker images | grep "<none>" | awk '{print $3}')

Delete All Images

To delete all images, we can simplify our code above and write it as shown below:

docker rmi $(docker images | awk '{print $3}')

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

Docker is a critical topic for the deployment of applications, and it works best with web applications.

If you want to learn more about Docker Python and related concepts and want to become a full-stack web and desktop application developer, Simplilearn offers an exclusive Full-stack Web Development Certification Course to master both backend and frontend with tools, including SpringBoot, AngularMVC, etc.

If you are not up to enrolling yourself into the full certification course yet, Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. Become a full-stack web developer today!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449