Docker-Compose 如何引用其他目录中的文件

4

以下是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:


我需要引用以下文件(在docker-compose.yml文件中),这些文件位于其他目录而不是 .devcontainer 中:

  • manage.py
  • requirements.txt
  • .env

这是我的文件夹结构:

enter image description here

一个简单的解决方案是将 dockerfiledocker-compose.yml.env 移动到 django 目录 djsr 中,但是我想保持文件的这种结构。如何在 docker-compose.yml 中引用这些文件?

2个回答

16

通常将与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本身都是很好的,但是如果entrypoint脚本在子目录中,则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

非常感谢!这是我的第一个Docker项目,你的回答非常有帮助! - Alex Bran

0
为什么你不像之前对文件夹那样,将这些文件也挂载起来呢?
The source of the mount. For bind mounts, this is the path to the file or directory on the
Docker daemon host. May be specified as source or src.

我会尝试这个。谢谢! - Alex Bran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接