Search code examples
dockergitlabaws-cdkcicd

Docker not found in Gitlab CI


am trying to follow this link to deploy using Gitlab CI and CDK

my .gitlab-ci.yml looks like below ...

image: node:18-alpine    
cache:
  paths:
  - cdk/node_modules/

stages:
- build
- deploy

build:
  stage: build
  script:
    - cd cdk
    - npm install

deploy:
  stage: deploy
  services:
    - name: docker
  variables:
    ENV: "dev"
    AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
    CDK_DEPLOY_ACCOUNT: $SANDBOX_ACCOUNT_ID
    CDK_DEFAULT_ACCOUNT: $SANDBOX_ACCOUNT_ID
    CDK_DEPLOY_REGION: $AWS_DEFAULT_REGION
  before_script:
  - npm install -g aws-cdk@latest
  - cdk --version
  script:
    - docker --version
    - cd cdk && cdk deploy ExpressAppStack --require-approval never --verbose

but it keeps failing at deploy with

npm install -g aws-cdk@latest
added 1 package in 1s
$ cdk --version
2.130.0 (build bd6e5ee)
$ docker --version
/bin/sh: eval: line 155: docker: not found
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 127

I expected the YML to work as the one mentioned in AWS tutorial, can some one explain how and why to fix this ?


Solution

  • You can install required dependencies for the alpine based image as part of a before_script or similar:

    before_script:
      - apk add docker-cli bash
    

    You might also need to specify DOCKER_HOST=tcp://docker:2375 to make it actually target your sidecar service.

    bash is needed if you use esbuild for building lambdas and similar via CDK.

    You might also wanna consider creating this base image for repeated use instead of installing dependencies over and over again.