我有一個 Django 應用程式,其 Docker 構建分為兩個階段:
- 第一階段使用 Node 映像使用 Gulp 編譯靜態資產。此階段創建
node_modules
目錄和一個名為build
. - 第二階段應該從前一階段復制檔案并安裝 python 依賴項。
我的問題是第一階段創建的新檔案夾沒有被繼承。
這是 Dockerfile:
# Dockerfile
# STAGE 1: Compile static assets
# ----------------------------
FROM node:17-slim as client-builder
ARG APP_HOME=/code
WORKDIR ${APP_HOME}
COPY . ${APP_HOME}
# npm's post-install script will use GulpJs to compile the static
# assets into a folder named "build"
RUN npm install && npm cache clean --force
# STAGE 2: Add python dependencies
# ----------------------------
FROM python:3.10-slim as python-build-stage
ARG APP_HOME=/code
ARG USERNAME=docker
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV POETRY_NO_INTERACTION 1
ENV POETRY_VIRTUALENVS_CREATE false
ENV POETRY_CACHE_DIR "/var/cache/pypoetry"
ENV POETRY_HOME "/usr/local"
WORKDIR ${APP_HOME}
# Create the user
RUN addgroup --system ${USERNAME} \
&& adduser --system --ingroup ${USERNAME} ${USERNAME}
# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential \
# psycopg2 dependencies
libpq-dev \
# dev utils
git zsh
# Copy project files
COPY --from=client-builder --chown=${USERNAME}:${USERNAME} ${APP_HOME} ${APP_HOME}
# Install python dependencies
RUN pip install --upgrade pip
RUN pip install poetry
RUN poetry install --no-interaction --no-ansi
# Set default shell
RUN chsh -s $(which zsh)
# Set user
USER ${USERNAME}
我嘗試過的事情
- 第一階段運行正常。Gulp.js 是能夠運行的,所以
node_modules
必須是正確創建的。我嘗試除錯第一階段并看到所有檔案都正確創建。 - 也嘗試以root身份運行
- 我還嘗試“記錄”作業區
RUN ls . >> /tmp/ls.txt
的內容,檔案串列的內容讓我感到驚訝:
我可以看到那里列出的檔案夾:
但是當我ls
實際作業區時,我什么也沒看到:
怎么了?
碼頭工人組成
我忘了提,我正在使用 docker-compose 來構建和啟動這些影像:
version: '3.10'
services:
db.postgres:
image: "postgres"
env_file: .env
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
web:
build: .
command: >
zsh -c "python3 /code/manage.py migrate &&
python3 /code/manage.py runserver 0.0.0.0:8000"
env_file: .env
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db.postgres
restart: on-failure:5
volumes:
postgres_data:
uj5u.com熱心網友回復:
這里的問題是,在構建程序中,您將東西安裝到/code
目錄中:
ARG APP_HOME=/code
...
COPY --from=client-builder --chown=${USERNAME}:${USERNAME} \
${APP_HOME} ${APP_HOME}
但是在您的 中docker-compose.yaml
,您/code
通過在其上安裝本地目錄來掩蓋目錄:
web:
...
volumes:
- .:/code
這意味著在構建程序中放置??在那里的任何東西都將被隱藏。
一種解決方案是將您的本地目錄掛載到其他地方
/code
(然后更新您的代碼/配置/任何內容以了解新的掛載點)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/507911.html