bekkidavis.com

Getting Started with Docker for Python Applications

Written on

Chapter 1: Introduction to Docker

Docker is an open-source platform developed by Docker Inc. that enables users to package applications along with their dependencies into containers. This technology allows applications to run seamlessly across various environments, including development, testing, and production. Docker addresses common challenges faced by developers, such as discrepancies between local and staging environments.

Before diving into installation, let's clarify some key Docker concepts:

  • Docker Container: These are instances created from Docker images. They provide the necessary environment for running applications in isolation.
  • Docker Image: This is a template used to create containers. Each Docker image is built upon a base image, like Ubuntu 14.04 LTS.
  • Dockerfile: A text file containing a series of commands that specify how to build a Docker image.
  • Volume: This represents a shared directory initialized when a container is created.
  • Registry: A server that stores Docker images, allowing users to pull images as needed.

Section 1.1: Installing Docker

After installation, confirm it by running the command docker --version in your terminal. For this tutorial, I will be using a Windows machine.

Section 1.2: Creating Your Project

Open your preferred IDE, such as VS Code, and create a new project folder named docker_demo. Inside this folder, you will create two files: main.py and requirements.txt.

Project folder structure for Docker demo

Main.py File

In the main.py file, we will utilize the NumPy library to create an ndarray object using the array() function as shown below:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

Requirements.txt File

The requirements.txt file should contain the necessary libraries:

numpy>=1.18.1

pandas>=1.0.1

python-dateutil>=2.8.1

pytz>=2019.3

six>=1.14.0

Next, we will create our Dockerfile.

Section 1.3: Creating the Dockerfile

Create a new file named Dockerfile (without any extension) in your working directory.

Example of a Dockerfile for a Python application

Inside the Dockerfile, include the following commands:

# Start with the latest Python image

FROM python:latest

# Copy the requirements file into the app directory

COPY requirements.txt /app/

# Set the working directory

WORKDIR /app

# Upgrade pip and install the requirements

RUN pip install --upgrade pip

&& pip install --requirement requirements.txt

# Copy the main Python file into the working directory

COPY main.py .

# Define the command to run the application

CMD ["python", "main.py"]

Section 1.4: Building the Docker Image

To create your Docker image, execute the following command in your terminal:

docker build -t image_name .

Replace image_name with your desired name. For this example, I used:

docker build -t my_docker .

This command will generate a local image named my_docker.

Output of Docker image build process

Section 1.5: Running the Local Docker Image

To run your newly created Docker image, use the following command:

docker run my_docker

After executing this command, your application should run successfully, displaying the results as shown below:

Output of the running Docker container

Chapter 2: Useful Docker Commands

To view all containers, including stopped ones, use:

docker ps -a

Listing all Docker containers

For displaying only the active containers, execute:

docker ps

To show the most recently created container, run:

docker ps -l

We hope you found this guide informative. Thank you for reading!

A beginner-friendly tutorial on using Docker to containerize Python applications.

A comprehensive guide on how to effectively containerize Python applications with Docker.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Exploring UFOs, Metaphysics, and the Nature of Reality

A reflective exploration of UFOs, metaphysics, and the connections between our experiences and consciousness.

Elon Musk's Controversies: Free Speech, Fraud, and Twitter's Role

A deep dive into Elon Musk's challenges with Tesla, legal issues, and his stance on free speech, highlighting the implications for his businesses.

# Reflecting on 1966: A Year of Unforgettable Events

Explore the intriguing occurrences of 1966, from music to UFO sightings, revealing the year’s remarkable impact on culture and society.

A Mobile Device Transformed My Daughter's Life

A heartfelt exploration of how a smartphone reshaped my daughter's social interactions and identity.

Innovative Brewing: NASA's Wastewater Recycling Revolution

Discover how NASA's recycling technology is transforming wastewater into delicious craft beer while promoting sustainability.

Racial Disparities in Maternal Health: Addressing the Crisis

Examining the challenges of maternal mortality legislation amid racial disparities affecting Black women.

The Enigmatic Forests of La Gomera: A Journey Through Time

Explore La Gomera's unique laurel forests, revealing ancient history and the delicate balance of nature.

Title: Six Essential Elements for Crafting a Visually Engaging Business

Discover the six key components that contribute to a visually appealing and successful business, enhancing customer engagement and brand perception.