Как создать образ Docker для ссылки на библиотеку Python в Venv

Я пытаюсь докернизировать мое веб-приложение Django, которое работает в виртуальной среде. venv,

Это мой конфиг для моего Dockerfile:

FROM ppc64le/python:2.7

ENV PYTHONUNBUFFERED 1 
ENV DJANGO_SETTINGS_MODULE=agricultureProj.settings.deploy 
ENV UWSGI_VIRTUALENV=/venv 
UWSGI_WSGI_FILE=agricultureProj/wsgi.py 
UWSGI_HTTP=:8005 
UWSGI_MASTER=1 
UWSGI_WORKERS=2 
UWSGI_THREADS=8 
UWSGI_UID=1000 
UWSGI_GID=2000 
UWSGI_LAZY_APPS=1 
UWSGI_WSGI_ENV_BEHAVIOR=holy

RUN virtualenv venv 
RUN source /venv/bin/activate 
RUN mkdir /code 
WORKDIR /code   

ADD requirements.txt /code/ 
RUN pip install -r requirements.txt

ADD . /code/

EXPOSE 8005

COPY ./docker-entrypoint.sh / 
RUN chmod uog+rx ./docker-entrypoint.sh 
ENTRYPOINT ["sh","/docker-entrypoint.sh"]

CMD ["/venv/bin/activate", "--http-auto-chunked", "--http-keepalive"

Я хотел бы знать, как я должен определить свой Dockerfile, чтобы я мог напрямую ссылаться на существующую библиотеку Python и успешную сборку и компоновку, запустить для этого мой образ докера.

** Append:

Это ошибка, с которой я столкнулся после успешного создания образа докера.

Ошибка:

Recreating agricultureproj_db_1
Recreating agricultureproj_web_1
Attaching to agricultureproj_db_1, agricultureproj_web_1
db_1   | /docker-entrypoint.sh: 3: /docker-entrypoint.sh: source: not found
web_1  | ----------
db_1   | ----------
web_1  | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
db_1   | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
web_1  |  '/usr/lib/python2.7',
db_1   |  '/usr/lib/python2.7',
web_1  |  '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
db_1   |  '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1  |  '/usr/lib/python2.7/lib-tk',
db_1   |  '/usr/lib/python2.7/lib-tk',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
web_1  |  '/usr/lib/python2.7/dist-packages',
db_1   |  '/usr/lib/python2.7/dist-packages',
web_1  |  '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
db_1   |  '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
web_1  | ----------
db_1   | ----------
web_1  | Traceback (most recent call last):
web_1  |   File "manage.py", line 37, in <module>
db_1   | Traceback (most recent call last):
web_1  |     "Couldn't import Django. Are you sure it's installed and "
db_1   |   File "manage.py", line 37, in <module>
web_1  | ImportError: Couldn't import Django. Are you sure it's installed and 
available on your PYTHONPATH environment variable? Did you forget to activate 
a virtual environment?
db_1   |     "Couldn't import Django. Are you sure it's installed and "
web_1  | ----------
db_1   | ImportError: Couldn't import Django. Are you sure it's installed and 
available on your PYTHONPATH environment variable? Did you forget to activate 
a virtual environment?
web_1  | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
db_1   | ----------
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
db_1   | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
web_1  |  '/usr/lib/python2.7',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
web_1  |  '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1  |  '/usr/lib/python2.7/lib-tk',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
web_1  |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
web_1  |  '/usr/lib/python2.7/dist-packages',
web_1  |  '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
web_1  | ----------
db_1   |  '/usr/lib/python2.7',
db_1   |  '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1  | Traceback (most recent call last):
web_1  |   File "manage.py", line 37, in <module>
web_1  |     "Couldn't import Django. Are you sure it's installed and "
db_1   |  '/usr/lib/python2.7/lib-tk',
web_1  | ImportError: Couldn't import Django. Are you sure it's installed and 
available on your PYTHONPATH environment variable? Did you forget to activate 
a virtual environment?
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
db_1   |  '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
db_1   |  '/usr/lib/python2.7/dist-packages',
db_1   |  '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
db_1   | ----------
db_1   | Traceback (most recent call last):
db_1   |   File "manage.py", line 37, in <module>
db_1   |     "Couldn't import Django. Are you sure it's installed and "
db_1   | ImportError: Couldn't import Django. Are you sure it's installed and 
available on your PYTHONPATH environment variable? Did you forget to activate 
a virtual environment?
agricultureproj_web_1 exited with code 1
agricultureproj_db_1 exited with code 1
ppt@server:~/Elephas2/agricultureProj_v6_723/agricultureProj$ File 
"manage.py", line 37, in <module>
-bash: syntax error near unexpected token `newline'
ppt@server:~/Elephas2/agricultureProj_v6_723/agricultureProj$ db_1   |     
"Couldn't import Django. Are you sure it's installed and "
db_1: command not found
Couldn't import Django. Are you sure it's installed and : command not found
ppt@server:~/Elephas2/agricultureProj_v6_723/agricultureProj$ db_1   | 
ImportError: Couldn't import Django. Are you sure it's installed and 
available on 
your PYTHONPATH environment variable? Did you forget to activate a virtual 
environment?
db_1: command not found
ImportError:: command not found

Когда я пытаюсь собрать созданный образ в Docker, среда не смогла импортировать Django.

2 ответа

Не активируйте виртуальную среду, просто добавьте ее bin каталог к ​​началу PATH переменная окружения. Обычно этого достаточно.

Вы также должны избегать запуска своего изображения как root, Даже в контейнере это не очень хорошая практика.

Вы можете использовать --system-site-packages, чтобы указать venv для использования системных библиотек в приоритете, что означает изменение этой строки, а затем указание pip, чтобы игнорировать установленные системы библиотеки

RUN virtualenv --system-site-packages venv 
RUN source /venv/bin/activate 
RUN mkdir /code WORKDIR /code
ADD requirements.txt /code/ 
RUN pip install --ignore-installed -r requirements.txt
Другие вопросы по тегам