我有一个 Dockerfile
对于我与 Gitlab
一起使用的 Django 和 Vue.js 应用程序.
我将要描述的问题仅在通过 Gitlab CI
部署时发生和相应的 .gitlab-ci.yml
文件。运行 docker-compose up
时命令在我的本地机器上,这不会发生。
所以我跑 docker-compose up
Dockerfile 中的所有指令显然都运行良好。但是当我检查生产服务器时,dist
文件夹(应存储 bundle.js
和 bundle.css
)不存在。
运行Dockerfile
时吐出的日志确认 npm install
和 npm run build
命令运行,它甚至确认 dist/bundle.js
和 dist/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/