无法使用节点alpine拉入私有git存储库

bogh5gae  于 2023-03-06  发布在  Git
关注(0)|答案(1)|浏览(206)

我尝试使用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" ]

对接合成

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:apppassword@bitbucket.org/myproject/myrepo.git'
}
...
92dk7w1h

92dk7w1h1#

不同的是node:16.15.0镜像安装了git,而node:16.15.0-alpine镜像没有安装,因此git克隆私有仓库会因为缺少git命令而失败。
这可以通过在Dockerfile中手动安装git包来解决:

FROM node:16.15.0-alpine as develop-stage

RUN apk add --no-cache git
...

这会将git包添加到映像中,从而增加映像的大小(大约13 MiB)。如果映像稍后需要git,这可能是有意义的。如果git只在最初的npm install中需要,可以通过将apk addnpm installapk del合并到一个RUN节中来避免这种开销:

FROM node:16.15.0-alpine as develop-stage
...

RUN apk add --no-cache git && npm install && apk del git
...

相关问题