Django, Docker, and Pipenv - Error adding new packages

走远了吗. 提交于 2020-04-30 15:42:46

问题


Using Pipenv with Docker is causing some issues in my Django project.

I've installed Django locally with Pipenv which generates a Pipfile and Pipfile.lock. Then used startproject to start a new Django project.

Then I add a Dockerfile file.

# Dockerfile
FROM python:3.7-slim

ENV PYTHONUNBUFFERED 1

WORKDIR /code
COPY . /code

RUN pip install pipenv
RUN pipenv install --system

And a docker-compose.yml file.

# docker-compose.yml
version: '3'

services:
  web:
    build: .
    command: python /code/manage.py migrate --noinput && /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000

And run docker-compose up --build to build the image and start the container. Everything works.

Now here's the issue...I want to add a new package, let's say psycopg2 so I can use PostgreSQL.

So...update my docker-compose.yml to add PostgreSQL.

# docker-compose.yml
version: '3'

services:
  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
  web:
    build: .
    command: python /code/manage.py migrate --noinput && /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db

volumes: postgres_data:

And update the DATABASE config in settings.py.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432
    }
}

Now if I install psycopg2-binary locally like pipenv install psycopg2-binary this "should" sync with Docker. But I get "No module named 'psycopg2'` errors".

Ok so maybe I need to install it directly within Docker:

$ docker-compose exec web pipenv install psycopg2-binary` 

Nope, same error.

Maybe I need to generate the lock file within Docker?

$ docker-compose exec web pipenv lock

Again no. So the issue is the state of Pipenv...I feel like I'm close but just not quite grasping something here.

Anyone see the error?

来源:https://stackoverflow.com/questions/53400385/django-docker-and-pipenv-error-adding-new-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!