DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • OpenTelemetry Python: All You Need to Know About Tracing
  • Getting Started With Windows Containers

Trending

  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Enhancing Avro With Semantic Metadata Using Logical Types
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Develop Your Python Docker Applications Faster

How to Develop Your Python Docker Applications Faster

Tutorial and example for developing Python applications faster on Docker using host volumes and runserver

By 
Ethan J Jackson user avatar
Ethan J Jackson
·
Jul. 27, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

Docker has many benefits that make deploying applications easier. But the process of developing Python with Docker can be frustratingly slow. That’s because testing your Python code in Docker is a real pain.

Luckily, there’s a technique you can use to reduce time you spend testing. In this tutorial, we’ll show you how to use Docker’s host volumes and runserver to make developing Python Docker applications easier and faster.

(If you’re a Node.JS developer, see How to Develop Your Node.Js Docker Applications Faster.)

How Host Volumes and Runserver Can Speed Up Your Python Development

As every Python developer knows, the best way to develop your application is to iterate through short, quick cycles of coding and testing. But if you’re developing using Docker, every time you change your code, you’re stuck waiting for the container to rebuild before you can test.

As a result, you end up with a development workflow that looks like this:

  • You make a change.
  • You wait for the container to rebuild.
  • You make another change.
  • You wait some more.

And if your team uses CI/CD, so you’re constantly running your code through automated tests? You’re going to be spending even more time waiting for the container to rebuild.

Coding and waiting and coding and waiting is not a recipe for developer productivity – or developer happiness.

But there’s a way to modify a container’s code without having to rebuild it. The trick is to use a Docker host volume.

Host volumes sync file changes between a local host folder and a container folder. If you use a host volume to mount the code you’re working on into a container, any edits you make to your code on your laptop will automatically appear in the container. And as you will see in the next section, you can use the runserver package to automatically restart your application without having to rebuild the container – a technique known as “live reloading.”

The result: instead of wasting lots of time waiting for your containers to rebuild, your code-test-debug loop is almost instantaneous.

Example: Using Host Volumes and Runserver in Python Docker Development

The idea of using a host volume to speed up your Python coding might seem a little daunting, but it’s pretty straightforward.

To demonstrate this, let’s use a Python example: django-polls, a basic poll app that’s part of Django’s introductory tutorial. To clone the repo:

Shell
xxxxxxxxxx
1
 
1
$git clone https://github.com/kelda/django-polls


The repo assumes you are using Docker Compose. You can also use Blimp, our Compose alternative that scales to the cloud.

Here’s the docker-compose.yml file for django-polls:

YAML
xxxxxxxxxx
1
22
 
1
version: '3'
2
services:
3
  web:
4
    build: .
5
    command:
6
      - sh
7
      - -c
8
      - "./wait-for-postgres.sh && python manage.py migrate ; python manage.py shell < init-db.py ; python manage.py runserver 0.0.0.0:8000"
9
    ports:
10
      - "8000:8000"
11
    depends_on:
12
      - db
13
    volumes:
14
      - ".:/code"
15
  db:
16
    image: "postgres:12"
17
    ports:
18
      - "5432:5432"
19
    environment:
20
      - POSTGRES_USER=polls
21
      - POSTGRES_PASSWORD=polls
22
      - POSTGRES_DB=polls 


This file tells Docker to boot a container, the Django application, and a Postgres database where the application stores the poll. It also tells Docker to mount a host volume:

YAML
xxxxxxxxxx
1
 
1
volumes:
2
    - ".:/code"


As a result, Docker will mount the ./ directory on your laptop, which contains the code you’re developing into the container at /code.

Next, you need to set up your Docker container so that whenever you edit your code, Docker automatically restarts your Python application. That way, your application will always use the latest version of your code.

If you are creating a Django app, the easiest way to do that is to have your .yml file tell Docker to use runserver, Django’s development web server:

YAML
xxxxxxxxxx
1
 
1
  web:
2
    build: .
3
    command:
4
      - sh
5
      - -c
6
      - "./wait-for-postgres.sh && python manage.py migrate ; python manage.py shell < init-db.py ; python manage.py runserver 0.0.0.0:8000"


As a result, whenever you modify your code on your laptop, runserver restarts the process without rebuilding the container.

In short, by using a host volume and runserver, you can set up your Python application’s container so it automatically syncs code changes between the container and your laptop. If you didn’t do this, you’d have to rebuild the container every single time you made a change to your code.

Over time, this technique can substantially speed up your Python development. For example, we’ve heard from users that it’s not uncommon for container rebuilds to take 5-30 minutes. With host volumes and runserver, your code sync is almost instantaneous. Imagine what your day would look like if you could save yourself 5-30 minutes every time you modify and test your code.

Syncing Your Own Code When Developing a Python Application

Now that you’ve seen how to use this technique in a sample application, the rest of this tutorial will show you how to enable code syncing in one of your existing Python projects.

Prerequisites

Just like the example above, your Python project should include the following:

  • A git repo that contains your code.
  • A Dockerfile that builds that code into a working container.
  • A docker-compose.yml file you use to run that container.

How to Configure Your Container to Automatically Sync Your Python Code

1) Locate the folder in your Docker container that has your code. The easiest way to figure out where your code is stored in your container is to look at your Dockerfile’s COPY commands. In the django-polls example, you can see from the Dockerfile that the container expects the code to be in /code:

Dockerfile
xxxxxxxxxx
1
 
1
FROM python:3
2
RUN apt update && apt install -y netcat && rm -rf /var/lib/apt/lists/*
3
ENV PYTHONUNBUFFERED 1
4
RUN mkdir /code
5
WORKDIR /code
6
COPY requirements.txt /code/
7
RUN pip install --no-cache-dir -r requirements.txt 


2) Find the path to the folder on your laptop that has the same Python code.

3) Add a host volume to your docker-compose file. Find the container in your docker-compose file that you want to sync code with, and add a volume instruction underneath that container:

YAML
xxxxxxxxxx
1
 
1
volumes:
2
  "/path-to-laptop-folder:/path-to-container-folder"


4) Make sure your Docker Compose file configures your container for live reloading. In the django-poll example, you implemented it by using runserver as your web server:

YAML
xxxxxxxxxx
1
 
1
  web:
2
    build: .
3
    command:
4
      - sh
5
      - -c
6
      - "./wait-for-postgres.sh && python manage.py migrate ; python manage.py shell < init-db.py ; python manage.py runserver 0.0.0.0:8000"


5) Run Docker Compose or Blimp. Now all you need to do is either run docker-compose:

Shell
xxxxxxxxxx
1
 
1
$ docker-compose up


Or if you’re using Blimp:

Shell
xxxxxxxxxx
1
 
1
$ blimp up


As a result, Docker will update the container’s code with the code that’s on your laptop.

Now that your container is set up to use a host volume and runserver, whenever you modify the Python code on your laptop, your new code will automatically appear in the container.

Conclusion

At first, the idea of using host volumes to sync the Python code on your laptop with your container might seem a little weird. But once you get used to this workflow, you’ll see how much more efficient it is. With just a few tweaks to your Docker containers’ set up, developing your Python Docker app is easier and faster.

Resources

Try the Python example on Blimp

Check out Blimp, our team’s project to improve developer productivity for Docker Compose.

Docker (software) Python (language) application Host (Unix)

Published at DZone with permission of Ethan J Jackson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • OpenTelemetry Python: All You Need to Know About Tracing
  • Getting Started With Windows Containers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!