我一直在尝试创建 Dockerfile,其中 Jupyter Lab 以及 Pipfile 和 Pipfile.lock 中描述的 pip 扩展在我的 Pipenv 虚拟环境中运行。
以下是我的 Dockerfile:
FROM python:3.11 AS base
# Setup env
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
# Install pipenv and compilation dependencies
RUN apt-get update && apt-get install -y --no-install-recommends gcc
RUN pip install --upgrade pip
RUN pip install pipenv
# Copy Pipfile and Pipfile.lock
COPY Pipfile Pipfile.lock /app/
# Set up work directory
WORKDIR /app
# Install dependencies
RUN pipenv install --deploy
# Install Jupyter
RUN pipenv install jupyter notebook jupyterlab voila
# Expose ports
EXPOSE 8888
EXPOSE 5678
EXPOSE 8080
EXPOSE 80
# Create a directory for notebooks
RUN mkdir /notebooks
# Specify the directory as a volume
VOLUME /notebooks
# Create non-root user for security
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
# Change ownership of the notebooks directory to appuser
RUN chown -R appuser:appuser /notebooks
# Switch to appuser
USER appuser
# Change permissions of the directories to ensure appuser can write to them
RUN chmod -R 755 /app && chmod -R 755 /notebooks
# Start JupyterLab
CMD ["/app/.venv/bin/pipenv", "run", "jupyter", "lab", "--notebook-dir=/notebooks", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
这是我的 docker-compose.yaml:
version: '3.8'
services:
webapp:
build:
context: .
dockerfile: Dockerfile
ports:
- "8888:8888"
volumes:
- ./app:/app
- ./notebooks:/notebooks
user: appuser
这是我的 docker-compose.debug.yaml:
版本:'3.8'
services:
webapp:
build:
context: .
dockerfile: Dockerfile
ports:
- "8888:8888"
- "5678:5678"
- "8080:8080"
volumes:
- ./notebooks:/notebooks
- ./app:/app
- ./logs:/logs
user: appuser
environment:
- DEBUG=1
- JUPYTER_PORT=8888
command: ["/venv/bin/pipenv", "run", "jupyter", "lab", "--notebook-dir=/notebooks", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
Pipfile 和 Pipfile.lock 与 Dockerfile、docker-compose.yaml 和 docker-compose.debug.yaml 文件位于同一目录中。
当我输入
sudo docker compose
时,它起作用了;但是,当我运行 sudo docker compose up
时出现错误:
[+] Running 1/0
✔ Container docker-webapp-1 Recreated 0.1s
Attaching to webapp-1
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/app/.venv/bin/pipenv": stat /app/.venv/bin/pipenv: no such file or directory: unknown
我在这些文件中没有看到任何问题,但我无法启动我的 Dockerfile。有人可以帮我解决这些问题吗?
您的
volumes:
块会覆盖图像中的虚拟环境。您需要删除隐藏的卷/app
。
volumes:
# - ./app:/app # DELETE -- causes problems
- ./notebooks:/notebooks # keep this one
您的 Dockerfile 使用
pipenv
在 /app/.venv
中创建虚拟环境。卷挂载隐藏了 /app
中的所有内容,包括虚拟环境,并将其替换为主机中的内容。