【问题标题】:docker-compose doesn't mount volumes correct for django containerdocker-compose 没有为 django 容器安装正确的卷
【发布时间】:2022-01-14 16:28:00
【问题描述】:

在带有 WSL 2 Ubuntu 的 Windows 10 上运行 Docker。我有以下Dockerfile

FROM ubuntu

#base directory
ENV HOME /root
#subdirectory name for the REST project
ENV PROJECT_NAME django_project
#subdirectory name of the users app
ENV APP_NAME users

#set the working directory
WORKDIR $HOME

#install Python 3, the Django REST framework and the Cassandra Python driver
RUN apt-get update
RUN apt -y install python3-pip 2> /dev/null
RUN pip3 install djangorestframework
RUN pip3 install cassandra-driver

#initialize the project (blank project) and creates a folder called $PROJECT_NAME
#with manager.py on its root directory
RUN django-admin startproject $PROJECT_NAME .
#install an app in the project and create a folder named after it
RUN python3 manage.py startapp $APP_NAME

ENV CASSANDRA_SEEDS cas1

ENTRYPOINT ["python3","manage.py", "runserver", "0.0.0.0:8000"]

我用docker build -t django-img . 构建了一个图像,然后我有以下.yaml

version: '3'
services:
  django_c:
    container_name: django_c
    image: django-img
    environment:
      - CASSANDRA_SEEDS='cas1'
    ports:
      - '8000:8000'
    volumes:
      - /mnt/c/Users/claud/docker-env/django/django_project:/django_project

当我在 django-project 文件夹中运行 docker-compose up -d 时(.yml 和 Dockerfile 都在那里),我让容器运行,但我在主机中看不到容器中的任何文件.但是,如果我在容器中运行 ls,我会看到所有文件都在那里:

我应该如何使用主机中的编辑器来编辑容器文件?

p.s.:我已经用另一个容器测试了卷斜杠(“/”),因为我使用的是 WSL,所以它们工作正常。

添加 这里我的容器文件夹的内容使用相对路径,我试过了

volumes:
      - /mnt/c/Users/claud/docker-env/django/django_project:/root/django_project

但它仍然没有显示主机中的文件。

【问题讨论】:

  • 可以在没有 Docker 的情况下使用 Python 虚拟环境进行日常开发吗?不会遇到这样的麻烦,因为它没有文件系统隔离层;它直接在您的主机文件上运行,您可以使用普通的 IDE 工作流程。

标签: django docker docker-compose docker-volume


【解决方案1】:

我认为问题在于您的卷挂载指的是 absolute 路径 /django_project,但您在 Dockerfile 中指定了 WORKDIR $HOME/root。另一个线索是,当您使用 relative 路径在容器中 ls -la ./django_project 时,您会看到您的文件。

我敢打赌,您可以通过更新您的 docker-compose.yml django_c 服务定义以指定 /root/django_project 作为您的卷挂载来解决此问题:

volumes:
    - /mnt/c/Users/claud/docker-env/django/django_project:/root/django_project

【讨论】:

  • 谢谢!我试过你说的,还是不行。我在上面的问题中添加了容器文件夹的打印屏幕(更新)。
  • docker exec -it django_c pwd 返回什么?我敢打赌它是/root
猜你喜欢
  • 2018-01-14
  • 2020-03-17
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
相关资源
最近更新 更多