Getting Started with Docker in Python - CNDRO.LLC
3238
post-template-default,single,single-post,postid-3238,single-format-standard,wp-custom-logo,theme-bridge,bridge-core-2.9.4,woocommerce-no-js,tribe-no-js,ehf-template-bridge,ehf-stylesheet-bridge-child,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,columns-4,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

Getting Started with Docker in Python

.

                                                                 Photo by Rubaitul Azad on Unsplash

This articles explains how you can get started with Docker in Python. Docker is a free software developed by Docker Inc. This containerization platform helps us package our application and its dependencies in the form of containers. This application can work effortlessly in any environment, either development, test, or production.
Docker was developed to tackle many problems developers face, including the issue of an application working on a computer but not starting on a staging server or development.

Let’s discuss some of the different terminologies in docker before moving to the installation aspect.

  • Docker Container — The containers are created from images. We can also refer to them as the runtime instances of Docker Images. The Containers also have the equipment required for an application to be run in an isolated way.
  • Docker Image — Docker images refer to the template for creating our Docker containers. The starting point of every Docker image is a base image such as ubuntu14.04 LTS.
  • Docker File: Docker file is usually a text file that has series of instructions on how Docker Image should be created.
  • Volume — This can be defined as a shared folder. They are usually initialized when a container is created.
  • Registry — The registry can be referred to as the server which stores the Docker Images. It provides us the ability to pull up any docker image.

Installation

Now, we need to install Docker Engine on our machine. To install Docker Engine on your machine either Linux, Windows, or Mac, you can visit this link .

After you’ve done your installation, you verify with the command docker --version on your terminal. I will be using a windows machine to demonstrate in this tutorial.

Create your Project

Open your VS Code or any other IDE to create a new project, let’s create a folder call docker_demo. In this folder we have our two files, main.py and requirements.txt just as seen below:

Main.py file

Inside this main.py file, we use the Numpy library. We want to create NumPy ndarray object by using the array() function as seen below:

1 import numpy as np
2
3 arr = np.array([1, 2, 3, 4, 5])
4
5 print(arr)

Requirements.txt file

1 -i https://pypi.org/simple
2 numpy>=1.18.1
3 pandas>=1.0.1
4 python-dateutil>=2.8.1
5 pytz>=2019.3
6 six>=1.14.0

Our requirements.txt also contains all the necessary libraries we need. Let’s create our Docker File now.

Create Docker File

Here, we’ll create a new file that has no extension and name it Docker File inside our current working directory, just as seen below:

We then write the following command inside our Docker file:

1 #all docker file starts with a "FROM" instruction for setting parent image, here we are 
pulling latest python to run our application 
2 FROM python:latest 
3 
4 #we copy the requirements file into the app folder we are just creating now 
5 COPY requirements.txt /app/ 
6 
7 #here we set this app folder as our working directory 
8 WORKDIR /app 
9 
10 #Now we use the RUN command to pip install --upgrade pip and install the requirements.txt 
in our parent image here 
11 RUN pip install --upgrade pip \ 
12    &&  pip install --requirement requirements.txt 
13 
14 #After installation, we copy our python file(main.py) inside our working directory 
in the parent image 
15 COPY main.py . 
16 
17 #here, we defined the command to be executed to run our python file 
18 #just like we do "python main.py" to run our python file in our VS Code terminal. 
19CMD ["python", "main.py"]
Build Docker Image

To build our Docker image, we will write this command below in our terminal docker build -t image_name . The image-name stands for the name you want to assign to your image .

The command I used is shown below:

docker build -t my_docker .

A local image with my_docker will be automatically created for us, just as shown below.

Run local Docker image

What we need to do now is to run our local docker image. We can run with the command below by using the docker image name;

Therefore our code is ready to be launched.

1 $ docker run my_docker

After running the docker image, we generated our result as shown in the image below;

Docker Commands

Check All Containers

You can also view all running containers you have by using the command below:

1 docker ps -a

 

Show Running Containers

To show only running containers, you use this command:

1 docker ps

Show Latest Created Container

To show the latest created container, you use the command below.

1 docker ps -l

Hope you found this post helpful. Thanks for reading.

No Comments

Post A Comment