Docker多级生成在第二阶段中找不到文件

Docker多级生成在第二阶段中找不到文件,docker,docker-compose,docker-multi-stage-build,Docker,Docker Compose,Docker Multi Stage Build,我的环境是: Ubuntu 18.04 Docker 19.03.11 我正在开发一个Flask应用程序,并与Docker一起管理服务。这是我的撰写文件: version: '3.6' x-build-args: &build_args INSTALL_PYTHON_VERSION: 3.8 INSTALL_NODE_VERSION: 12 x-default-volumes: &default_volumes volumes: - ./:/app

我的环境是: Ubuntu 18.04 Docker 19.03.11

我正在开发一个Flask应用程序,并与Docker一起管理服务。这是我的撰写文件:

version: '3.6'

x-build-args: &build_args
  INSTALL_PYTHON_VERSION: 3.8
  INSTALL_NODE_VERSION: 12

x-default-volumes: &default_volumes
  volumes:
    - ./:/app
    - node-modules:/app/node_modules
    - ./dev.db:/tmp/dev.db

services:
  flask-dev:
    build:
      context: .
      target: development
      args:
        <<: *build_args
        user: abc
    image: "testapp-development"
    ports:
      - "5000:5000"
      - "2992:2992"
    <<: *default_volumes

  flask-prod:
    build:
      context: .
      target: production
      args:
        <<: *build_args
    image: "testapp-production"
    ports:
      - "5000:5000"
    environment:
      FLASK_ENV: production
      FLASK_DEBUG: 0
      LOG_LEVEL: info
      GUNICORN_WORKERS: 4
    <<: *default_volumes

  manage:
    build:
      context: .
      target: manage
    environment:
      FLASK_ENV: production
      FLASK_DEBUG: 0
    image: "testapp-manage"
    stdin_open: true
    tty: true
    <<: *default_volumes

volumes:
  node-modules:
  static-build:
  dev-db:

开发和生产目标都失败了

下面是我从运行
docker compose build flask dev
得到的输出:

Building flask-dev
Step 1/20 : ARG INSTALL_PYTHON_VERSION=${INSTALL_PYTHON_VERSION:-3.7}
Step 2/20 : FROM python:${INSTALL_PYTHON_VERSION}-slim-buster AS base
 ---> 38cd21c9e1a8
Step 3/20 : RUN apt-get update
 ---> Using cache
 ---> 5d431961b77a
Step 4/20 : RUN apt-get install -y     curl     gcc
 ---> Using cache
 ---> caeafb0035dc
Step 5/20 : ARG INSTALL_NODE_VERSION=${INSTALL_NODE_VERSION:-12}
 ---> Using cache
 ---> 6e8a1eb59d3c
Step 6/20 : RUN curl -sL https://deb.nodesource.com/setup_${INSTALL_NODE_VERSION}.x | bash -
 ---> Using cache
 ---> 06fe96eb13e7
Step 7/20 : RUN apt-get install -y     nodejs     && apt-get -y autoclean
 ---> Using cache
 ---> 1e085132d325
Step 8/20 : ARG user=sid
 ---> Using cache
 ---> 3b3faf180389
Step 9/20 : RUN groupadd -r ${user} && useradd -m -r -l -g ${user} ${user}
 ---> Running in 9672d6f8b64d
Removing intermediate container 9672d6f8b64d
 ---> 02a5c2602513
Step 10/20 : WORKDIR /app
 ---> Running in bd48ac652908
Removing intermediate container bd48ac652908
 ---> 92c16bf347d4
Step 11/20 : COPY --chown=${user}:${user} . /home/${user}/
 ---> 2af997c5a255
Step 12/20 : USER ${user}
 ---> Running in 6409cf8784ee
Removing intermediate container 6409cf8784ee
 ---> 012d9cf92f31
Step 13/20 : WORKDIR /home/${user}/app
 ---> Running in a12b164c39dd
Removing intermediate container a12b164c39dd
 ---> db6fba37f948
Step 14/20 : ENV PATH="/home/${user}/.local/bin:${PATH}"
 ---> Running in eb13f4786b17
Removing intermediate container eb13f4786b17
 ---> a32249e3169b
Step 15/20 : RUN npm install
 ---> Running in 8aefdd56e8f2
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/watchpack-chokidar2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

audited 712 packages in 2.873s

43 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Removing intermediate container 8aefdd56e8f2
 ---> 737faa9b974e

Step 16/20 : FROM base AS development
 ---> 737faa9b974e
Step 17/20 : RUN pip install --user -r requirements/dev.txt
 ---> Running in af5508643877
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements/dev.txt'
ERROR: Service 'flask-dev' failed to build: The command '/bin/sh -c pip install --user -r requirements/dev.txt' returned a non-zero code: 1
我认为我的方法是错误的。我需要让Flask应用程序在撰写文件中使用的应用程序卷之外运行

我认为我误解的是多阶段构建。为了能够访问/home/${user}/app目录,我是否需要在开发、生产和管理阶段执行额外的步骤?据我所知,在前几个阶段中定义的参数在后续阶段中不可用,但在一个阶段中完成的所有处理是否也在后续阶段中不可用

我还尝试在第二阶段使用COPY--from=base,在基本构建阶段使用app目录所在位置的完整路径,但这也不正确

项目目录的相关部分包括:

testapp/
    docker-compose.yml
    Dockerfile
    testapp/
       <web app code>
    requirements/
        dev.txt
        prod.txt
testapp/
docker-compose.yml
Dockerfile
特斯塔普/
要求/
dev.txt
prod.txt

您已将文件复制到其他目录中:

WORKDIR /app
COPY --chown=${user}:${user} . /home/${user}/
USER ${user}
WORKDIR /home/${user}/app
COPY --chown=${user}:${user} . /home/${user}/app
需求目录将位于
/home/${user}/
中,而您的相对run命令将使用workdir查看
/home/${user}/app
。您可能希望复制到应用程序子目录:

WORKDIR /app
COPY --chown=${user}:${user} . /home/${user}/
USER ${user}
WORKDIR /home/${user}/app
COPY --chown=${user}:${user} . /home/${user}/app

您已将文件复制到其他目录中:

WORKDIR /app
COPY --chown=${user}:${user} . /home/${user}/
USER ${user}
WORKDIR /home/${user}/app
COPY --chown=${user}:${user} . /home/${user}/app
需求目录将位于
/home/${user}/
中,而您的相对run命令将使用workdir查看
/home/${user}/app
。您可能希望复制到应用程序子目录:

WORKDIR /app
COPY --chown=${user}:${user} . /home/${user}/
USER ${user}
WORKDIR /home/${user}/app
COPY --chown=${user}:${user} . /home/${user}/app

dev.txt
还是
dev.text
?您可以在目录中列出一个内容,并在Dockerfile中复制另一个内容。您可以在pip命令中使用绝对路径吗
运行pip安装--user-r/app/requirements/dev.txt
或``运行pip安装--user-r/home/user/app/requirements/dev.txt`。这取决于您的requirements.txt在哪里。@BMitch很抱歉输入错误,它是dev.txt。编辑original@M.Iduoad该路径也不起作用。它是
dev.txt
还是
dev.text
?您可以在目录中列出一个内容,并在Dockerfile中复制另一个内容。您可以在pip命令中使用绝对路径吗
运行pip安装--user-r/app/requirements/dev.txt
或``运行pip安装--user-r/home/user/app/requirements/dev.txt`。这取决于您的requirements.txt在哪里。@BMitch很抱歉输入错误,它是dev.txt。编辑original@M.Iduoad这条路也行不通。