Reputation: 537
I am using docker in django project, and installed packages which are in req.txt.
During project I needed to install a package and did it using docker-compose exec web pip install 'package'
and docker-compose up -d --build
, it installed in docker but I cannot use it in my project that not installed in project.
Question:
Dockerfile:
FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml:
version: '3.9'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
volumes:
postgres_data:
Upvotes: 0
Views: 2731
Reputation: 158647
Here's the standard workflow I'd use here.
First of all, make sure you have a normal working non-Docker development environment.
python3 -m venv vpy
. vpy/bin/activate
pip install -e .
Then edit your setup.cfg
file to include the new package and update the virtual environment.
$EDITOR setup.cfg
# add install_requires= package
pip install -e .
Make sure your application works properly.
pytest
run_the_app # calling a setuptools console_scripts= script
Update the package lock file.
pip freeze > requirements.txt
All of this so far is still on your host, outside of Docker. Now you can rebuild the image to run integration tests:
docker-compose build
docker-compose up -d
curl http://localhost:8000
Note that this setup is the totally normal Python dependency workflow, plus a final "build your Docker image" at the end. There is nothing Docker-specific in it, and you do not "install a dependency in Docker" or "update the host source code from the deployed image". You could do a similar thing with newer tools like Pipenv instead of plain virtual environments.
(You do not need a Compose volumes:
block to hide the code in the image, nor should you need a Compose command:
line to override the Dockerfile's CMD
.)
Upvotes: 1