首页 > 解决方案 > 在构建期间安装 NPM 失败 Docker 构建

问题描述

我正在尝试让 GitLab CI 运行程序从 Docker 映像构建我的项目并在构建过程中安装 NPM 包。我的.gitlab-ci.yml文件受到这个主题Gitlab CI with Docker and NPM的启发,其中 PO 正在处理相同的问题:

image: docker:stable

services:
  - docker:dind

stages:
  - build

cache:
  paths:
    - node_modules/

before_script:
  - export REACT_APP_USERS_SERVICE_URL=http://127.0.0.1

compile:
  image: node:8
  stage: build
  script:
    - apk add --no-cache py-pip python-dev libffi-dev openssl-dev gcc libc-dev make
    - pip install docker-compose
    - docker-compose up -d
    - docker-compose exec -T users python manage.py recreate_db
    - docker-compose exec -T users python manage.py seed_db
    - npm install
    - bash test.sh


after_script:
  - docker-compose down

可悲的是,该解决方案效果不佳,但我觉得我现在更接近实际解决方案。我在构建过程中遇到两个错误:

/bin/bash: line 89: apk: command not found
Running after script...
$ docker-compose down
/bin/bash: line 88: docker-compose: command not found

我该如何解决这个问题?

编辑:

image: docker:stable

services:
  - docker:dind

stages:
  - build
  - test

before_script:
  - export REACT_APP_USERS_SERVICE_URL=http://127.0.0.1

compile:
  stage: build
  script:
    - apk add --no-cache py-pip python-dev libffi-dev openssl-dev gcc libc-dev make
    - pip install docker-compose
    - docker-compose up -d
    - docker-compose exec -T users python manage.py recreate_db
    - docker-compose exec -T users python manage.py seed_db

testing:
  image: node:alpine
  stage: test
  script:
    - npm install
    - bash test.sh


after_script:
  - docker-compose down

我将测试移到testing了无论如何我都应该完成的单独阶段,并且我认为我已经在那里定义了图像以将其与构建阶段分开。没变。找不到 Docker,也无法运行 bash 测试:

$ bash test.sh
/bin/sh: eval: line 87: bash: not found
Running after script...
$ docker-compose down
/bin/sh: eval: line 84: docker-compose: not found

标签: dockergitlab-ci

解决方案


image: node:8此图像不是基于 alpine,因此,您得到了错误

apk:找不到命令

node:<version>

这些是Debian 发行版的套件代码名称,并指示映像基于哪个发行版。如果您的映像需要安装映像附带的任何其他软件包,您可能需要明确指定其中之一,以在有新版本Debian时最大限度地减少损坏。

只需将图像替换为

node:alpine

它应该可以工作。

第二个错误是因为未安装 docker-compose。

您可以查看此答案以获取有关作曲家的更多详细信息。


推荐阅读