Coder Social home page Coder Social logo

docker-python-env's Introduction

How to create a great local Python development environment with Docker

Work in progress

  1. Why Docker?
  2. Dockerize an app
  3. Immediate file changes (volumes)
  4. Use IDE in Docker
  5. Docker Compose
  6. Add more services (Redis)
  7. Debug Python code inside a container

1. Why Docker?

Python versions, more than just a virtual env, ...

2. Dockerize an app

FROM python:3.10-slim
WORKDIR /code
COPY ./requirements.txt ./
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY ./src ./src
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]

Use Dockerfile and the code in scr directory

(Use slim or alpine for smaller versions)

docker build -t fastapi-image .
docker run --name fastapi-container -p 80:80 fastapi-image
docker run -d --name fastapi-container -p 80:80 fastapi-image

3. Immediate file changes (volumes)

 docker stop fastapi-container
 docker ps -a
 docker rm fastapi-container

 docker run -d --name fastapi-container -p 80:80 -v $(pwd):/code fastapi-image

4. Use IDE in Docker

5. Docker Compose

services:
  app:
    build: .
    container_name: python-server
    command: uvicorn src.main:app --host 0.0.0.0 --port 80 --reload
    ports:
      - 80:80
      - 5678:5678
    volumes:
      - .:/code

Use docker-compose.yml

docker-compose up
docker-compose down

6. Add more services (Redis)

services:
  app:
    ...
    depends_on:
      - redis

  redis:
    image: redis:alpine

7. Debug Python code inside a container

Add this in the code

import debugpy

debugpy.listen(("0.0.0.0", 5678))

# print("Waiting for client to attach...")
# debugpy.wait_for_client()

And the port in yml:

services:
  app:
    ...
    ports:
      - 80:80
      - 5678:5678

Attach to running container

Try a Python version easily with Docker

 docker pull python:3.11-slim
 docker run -d  -i --name python_dev python:3.11-slim
 docker exec -it python_dev /bin/sh

Further Resources:

Other commands for cleaning up

docker rm container_name
docker image rm image_name
docker system prune
docker images prune

Check folder size:

du -sh *

docker-python-env's People

Contributors

kenjitagawa avatar renovate[bot] avatar

Watchers

 avatar

docker-python-env's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (local>kenjitagawa/renovate-config)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository.

  • WARN: Base branch does not exist - skipping

This repository currently has no open or pending branches.

Detected dependencies

docker-compose
docker-compose.yaml
dockerfile
Dockerfile
  • python 3.11-slim-bullseye
github-actions
.github/workflows/labeler.yaml
  • actions/labeler v4
.github/workflows/publish.yaml
  • actions/checkout v3
  • docker/setup-qemu-action v2
  • docker/setup-buildx-action v2
  • docker/login-action v2
  • docker/metadata-action 3343011071fa59c64a174cc6aa415dc13b31b7b2
  • docker/build-push-action v4
  • actions/checkout v3
  • docker/metadata-action v4
  • docker/login-action v2
  • docker/build-push-action v4
.github/workflows/pylint.yaml
  • actions/checkout v3
  • actions/setup-python v4
pip_requirements
requirements.txt

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.