我正在尝试使用
node:16.15.0-alpine
为我在 Vue 中的前端应用程序构建本地 docker 镜像,但失败了,因为我的 package.json
文件中有一个私有仓库。
但是,当我使用
node:16.15.0
构建图像时,一切正常,尽管我的 docker 图像大小拍摄到 1.8GB
.
下面是我的dockerfile
FROM node:16.15.0-alpine as develop-stage
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
EXPOSE 80
ENTRYPOINT [ "npm", "run", "dev" ]
Docker-compose
version: '3.8'
services:
frontend:
container_name: frontend-test
image: frontend-test:latest
build:
context:.
dockerfile: ./dockerfile.dev
target: 'develop-stage'
networks:
- test-app-network
environment:
- CHOKIDAR_USEPOLLING: true
ports:
- 8080:8080
volumes:
test-data:
external: false
networks:
test-app-network:
external: false
我的 package.json 文件
...
depedencies: {
private-repo: 'git+https://username:[email protected]/myproject/myrepo.git'
}
...
区别在于
node:16.15.0
图像安装了git
,而node:16.15.0-alpine
图像没有安装。因此,私有仓库的 git clone 将因缺少 git
命令而失败。
这可以通过在
git
中手动安装Dockerfile
包来解决:
FROM node:16.15.0-alpine as develop-stage
RUN apk add --no-cache git
...
这会将
git
包添加到图像中,因此会增加图像大小(大约 13 MiB)。如果无论如何稍后在图像中需要 git,这可能是有道理的。如果仅在最初的npm install
期间需要git,则可以通过将apk add
,npm install
和apk del
组合在一个RUN
节中来避免这种开销:
FROM node:16.15.0-alpine as develop-stage
...
RUN apk add --no-cache git && npm install && apk del git
...