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 ?
2
Answers
The problem is that the
docker
executable is not found in the CI runner machine’s paths to executable commands.Every job in your gitlab ci file will run in a docker container, as specified by the
image
keyword. At the top of your file you specifiedimage: node:18-alpine
which means all your jobs will run using that image. That means that the docker container executing your script will only have access to the executables that the image has pre-configured with it. And in your case,node:18-alpine
does not have the docker cli installed.You can reproduce this behavior locally using a docker container.
If you switch your image to be something that has the docker cli installed, you’ll be able to access it within your script. For example, we can see with the
docker:25.0.3-dind
image.This particular image is just an example. You’ll want to ensure that the particular image you choose to run your script with has all the necessary dependencies for your deployment process. It’s entirely possible that no such off-the-shelf image has everything you need. In which case you can either build a custom image, or install what you need within your script itself. For example, you might choose to modify your script to simply install the docker CLI before executing the rest of the script steps.
You can install required dependencies for the alpine based image as part of a before_script or similar:
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.