AWS-CDK and Gitlab returns Error: spawnSync docker ENOENT

Viewed 414

I am trying to run a Gitlab pipeline that builds an AWS-CDK project via the cdk-synth command. I have followed advice online in terms of what images I should be using in the Gitlab runner but I am running into the following error:

Error: spawnSync docker ENOENT

I'm not sure what this error means exactly, I guess it is trying to launch docker (which is used by thecdk synth command) but failing to do so. Again what I have found online all suggests to use the setup I currently have which is:

image: node:16.3.0-alpine

stages:
  - build
  - test
  - .post

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .npm
    - node_modules

build:
  tags:
    - mytag
  environment: ${CI_COMMIT_BRANCH}
  stage: build
  script:
    - npm i
    - cdk synth --no-staging > template.yaml
  artifacts:
    paths:
      - cdk.out
    expire_in: 30 mins

I'm really lost with this one, it may be staring me right in the face but could someone please point me in the right direction as to why i'd be getting this error when the image i'm using is itself a docker container?

2 Answers

After a good 10 or so days of fiddling with docker inside of docker etc... and lots of trial and error based on other solutions to the same problem e.g. this

I arrived at the following solution.

It seems you need to include the following in your gitlab-ci template (before_script/script) if you are using the NodejsFunction functions:

- apk add bash (source: here)

- npm i -g esbuild

esbuild is used when packaging the JavaScript based lambda function code and its dependencies into a single file. Here's what worked for me:

cdk_synth:
  image: node:16-alpine
  stage: validate
  before_script:
    - apk add bash
    - npm i -g esbuild
    - npm install
    - npm i -g aws-cdk
    - npm i -g aws-cdk-lib
  script:
    - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_REGION
    - cdk synth

Adding these lines resolved my issue!

  script:
    - apt-get update
    - apt-get install -y ca-certificates curl gnupg lsb-release
    - mkdir -p /etc/apt/keyrings
    - curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    - echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
    - apt-get update
    - apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    - npm i
    - npm run synth
Related