docker - "npm run build"在 Dockerfile : dist folder is generated but disappears

标签 docker npm webpack gitlab gitlab-ci

我有一个 Dockerfile对于我与 Gitlab 一起使用的 Django 和 Vue.js 应用程序.

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

所以我跑 docker-compose up Dockerfile 中的所有指令显然都运行良好。但是当我检查生产服务器时,dist文件夹(应存储 bundle.jsbundle.css)不存在。

运行Dockerfile时吐出的日志确认 npm installnpm run build命令运行,它甚至确认 dist/bundle.jsdist/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"]

这是 .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

这是的内容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

这是的内容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)

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

最佳答案

当您的 docker-compose.yml文件说

volumes:
  - ./app:/app

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

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

关于docker - "npm run build"在 Dockerfile : dist folder is generated but disappears,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56424252/

相关文章:

docker - 找不到quay.io/etcd容器命令

reactjs - React 风格、Webpack、React - 未捕获错误 : Invariant Violation: The `style` prop

css - 使用 webpack,为什么要从 JS 源代码导入 CSS 文件而不是像传统那样单独构建 CSS?

mysql - 如何使MYSQL在Docker运行时启动并在Dockerfile中创建mysql用户

docker - Diff Docker注册表镜像

docker - 通过 Helm 缓慢安装/升级(针对 Kubernetes)

javascript - 如何重用用于 Node.js API 的 JS 文件

makefile - 什么是生成文件?它与 Gruntfile 或 npm run 有何不同?

node.js - npm 更新包括预发布

webpack - 导入的 scss 类名转换为哈希