繁体   English   中英

docker-compose 没有为 django 容器安装正确的卷

[英]docker-compose doesn't mount volumes correct for django container

在 Windows 10 上运行 Docker 和 WSL 2 Ubuntu。 我有以下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 ,我会看到所有文件都在那里:

在此处输入图像描述

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

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

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

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

但它仍然没有显示主机中的文件。 在此处输入图像描述

我认为问题在于您的卷挂载是指绝对路径/django_project ,但是您在Dockerfile中指定WORKDIR $HOME/root 另一个线索是,当您使用相对路径在容器中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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM