我的环境是: 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:
我的Dockerfile:
# ==================================== BASE ====================================
ARG INSTALL_PYTHON_VERSION=${INSTALL_PYTHON_VERSION:-3.7}
FROM python:${INSTALL_PYTHON_VERSION}-slim-buster AS base
RUN apt-get update
RUN apt-get install -y \
curl \
gcc
ARG INSTALL_NODE_VERSION=${INSTALL_NODE_VERSION:-12}
RUN curl -sL https://deb.nodesource.com/setup_${INSTALL_NODE_VERSION}.x | bash -
RUN apt-get install -y \
nodejs \
&& apt-get -y autoclean
ARG user=sid
# Add the user and their group
RUN groupadd -r ${user} && useradd -m -r -l -g ${user} ${user}
# the /app directory contains things that npm needs
# user won't have permissions to this, so copy it
# into their home dir
WORKDIR /app
COPY --chown=${user}:${user} . /home/${user}/
USER ${user}
WORKDIR /home/${user}/app
ENV PATH="/home/${user}/.local/bin:${PATH}"
RUN npm install
# ================================= DEVELOPMENT ================================
FROM base AS development
RUN pip install --user -r requirements/dev.txt
EXPOSE 2992
EXPOSE 5000
CMD [ "npm", "start" ]
# ================================= PRODUCTION =================================
FROM base AS production
RUN pip install --user -r requirements/prod.txt
COPY supervisord.conf /etc/supervisor/supervisord.conf
COPY supervisord_programs /etc/supervisor/conf.d
EXPOSE 5000
ENTRYPOINT ["/bin/bash", "shell_scripts/supervisord_entrypoint.sh"]
CMD ["-c", "/etc/supervisor/supervisord.conf"]
# =================================== MANAGE ===================================
FROM base AS manage
RUN pip install --user -r requirements/dev.txt
ENTRYPOINT [ "flask" ]
开发和生产目标均失败
这是我通过运行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目录,我是否需要在开发,生产和管理阶段进行一些额外的步骤?据我了解,先前阶段中定义的ARG在后续阶段中不可用,但是在后续阶段中进行的所有处理是否也不可用?
我还尝试在第二阶段使用COPY --from = base,并在基础构建阶段使用应用目录的完整路径,但这也不正确。
项目目录的相关部分是:
testapp/
docker-compose.yml
Dockerfile
testapp/
<web app code>
requirements/
dev.txt
prod.txt
答案 0 :(得分:0)
您已将文件复制到另一个目录:
WORKDIR /app
COPY --chown=${user}:${user} . /home/${user}/
USER ${user}
WORKDIR /home/${user}/app
需求目录位于/home/${user}/
中,而您的相对运行命令将使用workdir在/home/${user}/app
中查找。您可能要复制到应用子目录中:
COPY --chown=${user}:${user} . /home/${user}/app