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
Running this project in a python venv and use pip to install our deps.
python -m venv venv
. venv/bin/activate
pip install -r requirements.txt
We can then run this locally like before by using this console command.
uvicorn src.main:app --reload
Or just copy the same command from part 2/3 to be more explicit in port selection.
After this we can use the command:
deactivate
to jump out of our venv and back to our host machine.
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
We just need to go to /docs from our url to find all our endpoints automatically have been ported to Swagger.
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/
- https://www.uvicorn.org/
- https://fastapi.tiangolo.com/
docker rm container_name
docker image rm image_name
docker system prune
docker images prune
Check folder size:
du -sh *