Home Explore Blog CI



docker

1st chunk of `content/guides/python/develop.md`
dee9e654d55c036743acd62e7369f553ef252692f21c1cdc0000000100000fab
---
title: Use containers for Python development
linkTitle: Develop your app
weight: 40
keywords: python, local, development
description: Learn how to develop your Python application locally.
aliases:
  - /language/python/develop/
  - /guides/language/python/develop/
---

## Prerequisites

Complete [Containerize a Python application](containerize.md).

## Overview

In this section, you'll learn how to set up a development environment for your containerized application. This includes:

- Adding a local database and persisting data
- Configuring Compose to automatically update your running Compose services as you edit and save your code

## Get the sample application

You'll need to clone a new repository to get a sample application that includes logic to connect to the database.

1. Change to a directory where you want to clone the repository and run the following command.

   ```console
   $ git clone https://github.com/estebanx64/python-docker-dev-example
   ```

2. In the cloned repository's directory, manually create the Docker assets or run `docker init` to create the necessary Docker assets.

   {{< tabs >}}
   {{< tab name="Use Docker Init" >}}

   In the cloned repository's directory, run `docker init`. Refer to the
   following example to answer the prompts from `docker init`.

   ```console
   $ docker init
   Welcome to the Docker Init CLI!

   This utility will walk you through creating the following files with sensible defaults for your project:
     - .dockerignore
     - Dockerfile
     - compose.yaml
     - README.Docker.md

   Let's get started!

   ? What application platform does your project use? Python
   ? What version of Python do you want to use? 3.11.4
   ? What port do you want your app to listen on? 8001
   ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8001
   ```

   Create a file named `.gitignore` with the following contents.

   ```text {collapse=true,title=".gitignore"}
   # Byte-compiled / optimized / DLL files
   __pycache__/
   *.py[cod]
   *$py.class

   # C extensions
   *.so

   # Distribution / packaging
   .Python
   build/
   develop-eggs/
   dist/
   downloads/
   eggs/
   .eggs/
   lib/
   lib64/
   parts/
   sdist/
   var/
   wheels/
   share/python-wheels/
   *.egg-info/
   .installed.cfg
   *.egg
   MANIFEST

   # Unit test / coverage reports
   htmlcov/
   .tox/
   .nox/
   .coverage
   .coverage.*
   .cache
   nosetests.xml
   coverage.xml
   *.cover
   *.py,cover
   .hypothesis/
   .pytest_cache/
   cover/

   # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
   __pypackages__/

   # Environments
   .env
   .venv
   env/
   venv/
   ENV/
   env.bak/
   venv.bak/
   ```

   {{< /tab >}}
   {{< tab name="Manually create assets" >}}

   If you don't have Docker Desktop installed or prefer creating the assets
   manually, you can create the following files in your project directory.

   Create a file named `Dockerfile` with the following contents.

   ```dockerfile {collapse=true,title=Dockerfile}
   # syntax=docker/dockerfile:1

   # Comments are provided throughout this file to help you get started.
   # If you need more help, visit the Dockerfile reference guide at
   # https://docs.docker.com/go/dockerfile-reference/

   # Want to help us make this template better? Share your feedback here: https://   forms.gle/ybq9Krt8jtBL3iCk7

   ARG PYTHON_VERSION=3.11.4
   FROM python:${PYTHON_VERSION}-slim as base

   # Prevents Python from writing pyc files.
   ENV PYTHONDONTWRITEBYTECODE=1

   # Keeps Python from buffering stdout and stderr to avoid situations where
   # the application crashes without emitting any logs due to buffering.
   ENV PYTHONUNBUFFERED=1

   WORKDIR /app

   # Create a non-privileged user that the app will run under.
   # See https://docs.docker.com/go/dockerfile-user-best-practices/
   ARG UID=10001
   RUN adduser \
       --disabled-password \
       --gecos "" \
       --home "/nonexistent" \

Title: Setting up a Python Development Environment with Docker
Summary
This section guides you on setting up a development environment for a containerized Python application, including adding a local database, persisting data, and configuring Docker Compose for automatic updates as you edit your code. It involves cloning a sample application repository and creating or using `docker init` to generate the necessary Docker assets like Dockerfile, compose.yaml, and .dockerignore.