Dockerfile 中的“npm run build”:dist 文件夹已生成但消失

Xar*_*Xar 5 npm gitlab docker gitlab-ci webpack

我有一个DockerfileDjango 和 Vue.js 应用程序,与Gitlab.

我将要描述的问题仅在部署 viaGitlab CI和相应.gitlab-ci.yml文件时发生。docker-compose up在我的本地机器上运行命令时,不会发生这种情况。

所以我运行docker-compose up并且 Dockerfile 中的所有指令都运行正常。但是当我检查生产服务器时,dist文件夹(bundle.jsbundle.css应该存储的地方)不存在。

运行时吐出的日志Dockerfile确认运行了npm installandnpm run build命令,甚至确认了dist/bundle.jsanddist/bundle.css文件已经生成。但由于某种原因,它们似乎被删除了。

这是我的Dockerfile

FROM python:3.7-alpine
MAINTAINER My Name

ENV PYTHONUNBUFFERED 1

RUN mkdir /app

# make the 'app' folder the current working directory
WORKDIR /app

# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY ./app .

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
      gcc libc-dev linux-headers postgresql-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

# copy both 'package.json' and 'package-lock.json' (if available)
COPY app/frontend/package*.json ./frontend/

# Install npm
RUN  apk add --update nodejs && apk add --update nodejs-npm

# install project dependencies
WORKDIR /app/frontend
RUN npm install

# build app for production with minification
RUN npm run build

RUN adduser -D user
USER user

CMD ["sh ../scripts/entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)

这是.gitlab-ci.yml文件:

image: docker:latest

services:
  - docker:dind

before_script:
  - echo "Runnig before_script"
  - sudo apt-get install -y python-pip
  - sudo apt-get install -y nodejs
  - pip install docker-compose

stages:
  - test
  - build
  - deploy

test:
  stage: test
  script:
    - echo "Testing the app"
    - docker-compose run app sh -c "python /app/manage.py test && flake8"

build:
  stage: build
  only:
    - develop
    - production
    - feature/gitlab_ci
  script:
    - echo "Building the app"
    - docker-compose build

deploy:
  stage: deploy
  only:
    - master
    - develop
    - feature/gitlab_ci
  script:
    - echo "Deploying the app"
    - docker-compose up --build -d
Run Code Online (Sandbox Code Playgroud)

这是docker-compose.yml文件的内容:

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python /app/manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=app
      - DB_USER=postgres
      - DB_PASS=postgres
    depends_on:
      - db
  db:
    image: postgres:10-alpine
    environment:
      - POSTGRES_DB=app
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
Run Code Online (Sandbox Code Playgroud)

这是entrypoint.sh文件的内容:

#!/bin/bash

(cd .. && ./manage.py collectstatic --noinput)
# Migration files are commited to git. Makemigrations is not needed.
# ./manage.py makemigrations app_name
(cd .. && ./manage.py migrate)
Run Code Online (Sandbox Code Playgroud)

我想知道dist/文件夹为什么会消失以及如何保留它。

Dav*_*aze 10

当你的docker-compose.yml文件说

volumes:
  - ./app:/app
Run Code Online (Sandbox Code Playgroud)

它隐藏了 Dockerfile 在/app目录中构建的所有内容,并将其替换为本地系统中的任何内容。如果你的主机没有,./app/frontend/dist那么你的容器也不会有那个路径,不管 Dockerfile 做什么。

我通常会建议完全删除这个volumes:块。它引入了一条笨拙的实时开发路径(您的所有工具都需要知道实际服务在 Docker 中运行),同时也不是您在开发中运行的(您希望映像是自包含的,而不是需要将应用程序与图像分开复制)。