Work in progress
- Why Docker?
- Dockerize an app
- Immediate file changes (volumes)
- Use IDE in Docker
- Docker Compose
- Add more services (Redis)
- Debug Python code inside a container
Python versions, more than just a virtual env, ...
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
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
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
services:
app:
...
depends_on:
- redis
redis:
image: redis:alpine
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
docker pull python:3.11-slim
docker run -d -i --name python_dev python:3.11-slim
docker exec -it python_dev /bin/sh
- https://towardsdatascience.com/debugging-for-dockerized-ml-applications-in-python-2f7dec30573d
- https://github.com/Wyntuition/try-python-flask-redis-docker-compose
- https://docs.docker.com/compose/gettingstarted/
- https://www.docker.com/blog/containerized-python-development-part-1/
docker rm container_name
docker image rm image_name
docker system prune
docker images prune
Check folder size:
du -sh *