django - docker-compose 如何引用其他目录中的文件
问题描述
拥有这个 dockerfile:
FROM python:3.8.3-alpine
ENV MICRO_SERVICE=/home/app/microservice
# RUN addgroup -S $APP_USER && adduser -S $APP_USER -G $APP_USER
# set work directory
RUN mkdir -p $MICRO_SERVICE
RUN mkdir -p $MICRO_SERVICE/static
# where the code lives
WORKDIR $MICRO_SERVICE
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev gcc python3-dev musl-dev \
&& apk del build-deps \
&& apk --no-cache add musl-dev linux-headers g++
# install dependencies
RUN pip install --upgrade pip
# copy project
COPY . $MICRO_SERVICE
RUN pip install -r requirements.txt
COPY ./entrypoint.sh $MICRO_SERVICE
CMD ["/bin/bash", "/home/app/microservice/entrypoint.sh"]
以及以下 docker-compose.yml 文件:
version: "3.7"
services:
nginx:
build: ./nginx
ports:
- 1300:80
volumes:
- static_volume:/home/app/microservice/static
depends_on:
- web
restart: "on-failure"
web:
build: . #build the image for the web service from the dockerfile in parent directory
command: sh -c "python manage.py collectstatic --no-input &&
gunicorn djsr.wsgi:application --bind 0.0.0.0:${APP_PORT}"
volumes:
- .:/microservice:rw # map data and files from parent directory in host to microservice directory in docker containe
- static_volume:/home/app/microservice/static
env_file:
- .env
image: wevbapp
expose:
- ${APP_PORT}
restart: "on-failure"
volumes:
static_volume:
我需要引用其他目录而不是.devcontainer中的以下文件(在 docker-compose.yml 文件中):
- 管理.py
- 要求.txt
- .env
这是我的文件夹结构:
一个简单的解决方案是将dockerfile、 docker -compose.yml和.env移动到 django 目录djsr中,但我试图保持文件的结构是这样的。如何在docker-compose.yml中引用这些文件?
解决方案
将几个与 Docker 相关的文件放在项目根目录中是很常见的,这可能会为您节省一些麻烦;我建议将其作为首选。
但是,如果您确实想将其全部保存在子目录中,则可以。运行时docker-compose
,可以指定配置文件的位置。它会将所有路径视为相对于该文件的目录。
# Either:
docker-compose -f .devcontainer/docker-compose.yml up
cd .devcontainer && docker-compose up
当您构建映像时,构建会读取上下文目录,并且COPY
语句总是相对于该目录进行解释。对于您的设置,您需要将上下文目录放在源代码树的顶部,然后在子目录中指定备用 Dockerfile。
services:
web:
build:
context: ..
dockerfile: .dockerenv/Dockerfile
在大多数情况下,Dockerfile 本身很好,但是入口点脚本位于子目录中时,COPY
命令也需要反映这一点。由于您要复制整个源目录,因此您还可以将图像内的内容重新排列为您想要的布局。
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
# Either:
COPY .dockerenv/entrypoint.sh ./
# Or:
RUN mv .dockerenv/entrypoint.sh .
# Or:
CMD ["./dockerenv/entrypoint.sh"]
我不推荐你有的卷结构,但是如果你想保留它,你还需要将绑定挂载的源路径更改为父目录。(特别注意,在前面的 Dockerfile 片段中,有几个选项涉及在映像中移动文件,绑定挂载将隐藏该更改。)
services:
web:
volumes:
# Ignore the application built into the container, and use
# whatever's checked out on the host system instead.
- ..:/home/app/microservice
# Further ignore the static assets on the host system and
# use the content in a named volume instead.
- static_volume:/home/app/microservice/static
推荐阅读
- c++ - c ++内存泄漏检测器'asan'无法检测内存泄漏并且无法将asan输出日志重定向到文件
- c++ - 在hackerrank平台上的列表中插入节点时出现分段错误
- ios - 不是每次都调用accessibilityPerformMagicTap
- node.js - 续集 ORDER BY
- amazon-web-services - 创建 IAM 角色并附加自定义策略
- tensorflow - 如何使用 Densenet/ResNet 使用训练、验证和测试图像实现多标签分类
- kotlin - 使用 Kotlin 在 Android Studio 中按钮推送的触觉反馈
- html - 没有让 tr 与伪类一起工作:nth-child
- c# - Winform: System.ArgumentNullException: 'Value cannot be null. (Parameter 'stream')'
- powershell - 检查多台服务器上的服务状态