Beginner’s Guide to Using Python with Docker: Easy Setup

In today’s fast-evolving tech environment, Docker has emerged as a revolutionary tool in software development, allowing developers to package applications into containers. These containers can run consistently across different systems, making development and deployment smoother. If you’re a beginner curious about integrating Python with Docker, this guide will walk you through a straightforward setup process.

Table of Contents

What is Docker?

Docker is an open-source platform that automates the deployment of applications in lightweight containers, enabling applications to work seamlessly in different environments. Containers encapsulate everything needed to run a piece of software, including the code, runtime, libraries, and system tools, ensuring consistency across various platforms.

Why Use Docker with Python?

Python is a versatile programming language widely used for web development, data science, automation, and more. However, Python applications can sometimes face compatibility issues due to differing environments. Docker resolves these issues by providing an isolated environment that ensures your Python applications run precisely as intended, regardless of where they are deployed.

Getting Started: Installing Docker

Before we proceed, you need to have Docker installed on your machine.

Installation on Windows

  1. Download Docker Desktop for Windows
  2. Install Docker Desktop
    • Run the installer and follow the on-screen instructions.
    • Ensure “Enable WSL 2 feature” is checked during installation.
  3. Start Docker Desktop
    • After installation, start Docker Desktop from the Start menu.
    • The Docker icon in your system tray indicates Docker is running successfully.

Installation on macOS

  1. Download Docker Desktop for Mac
  2. Install Docker Desktop
    • Open the installer file and drag the Docker icon to your Applications folder.
  3. Start Docker Desktop
    • Open Docker from your Applications and follow the tutorial for an initial setup.

Installation on Linux

  1. Install Prerequisites
    sudo apt-get update
    sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
  2. Add Docker’s official GPG key
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  3. Add Docker repository
    sudo add-apt-repository \   
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
  4. Install Docker
    sudo apt-get update
    sudo apt-get install docker-ce
  5. Verify Docker installation
    sudo docker --version

Running Python in a Docker Container

With Docker installed, let’s set up a simple Python application within a Docker container. Let’s first create a simple Python script.

Write a Python Script

Create a folder named python_docker and navigate to it:

mkdir python_docker
cd python_docker

Create a file named app.py and add the following code:

# app.py
print("Hello, from Docker!")
Code language: PHP (php)

Create a Dockerfile

A Dockerfile is a text document that contains all the commands to assemble an image.

In the same directory, create a file named Dockerfile (without any extension) and add the following content:

# Use the official Python image from the Docker Hub
FROM python:3.8-alpine

# Set the working directory
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Run the command
CMD [ "python", "./app.py" ]
Code language: PHP (php)

Build and Run the Docker Container

Now, let’s build a Docker image from your Dockerfile.

  1. Build the Docker Image

<p>docker build -t python-docker-app .</p>Code language: HTML, XML (xml)
  1. -t python-docker-app: Tags your image with a name for easier reference.
  2. .: Refers to the current directory, where the Dockerfile is located.
  3. Run the Docker Container

<p>docker run python-docker-app</p>Code language: HTML, XML (xml)
  1. This command will execute the app.py script, and you should see Hello, from Docker! printed.

Understanding the Dockerfile

  • FROM python:3.8-alpine: This line defines the base image. python:3.8-alpine is a lightweight version of Python, perfect for reducing the size of your final image.
  • WORKDIR /usr/src/app: Sets the working directory inside the container.
  • COPY . .: Copies all files from the current directory on your host machine into the /usr/src/app directory inside the container.
  • CMD [ “python”, “./app.py” ]: Specifies the command to execute when the container starts.

Conclusion

You’ve just learned how to run a Python application in a Docker container! This powerful tool not only standardizes development environments but also simplifies the deployment process, ensuring your Python applications are more scalable and portable. Now that you’ve grasped the basics, consider experimenting with more complex projects and explore the vast ecosystem of Docker images available on Docker Hub. How will you use Docker to enhance your Python development process? Share your thoughts and next steps with us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap