amazon-web-servicesdockerparametersaws-codebuildbuildspec

docker build build-args containing spaces in a buildspec.yml file doesn't work


We are using CodeBuild AWS service, that use a buildspec.yml file to execute some command on different stage of the build (pre_build, build, post_build and others). I wanted to have a global buildspec.yml file, that could be edited thanks for environments variables in CodeBuild. So for example, we pass environments type (staging, pre-prod, production...etc), commands to execute, S3 buckets and others.

In this file, i create a docker image, so i use docker build, i tag my image, add arguments to pass to the Dockerfile, and specify the Dockerfile path.

This command is used :

docker build -t ABC:v1 --build-arg LAUNCH_COMMAND=${LAUNCH_COMMAND} -f ${DOCKERFILE_PATH} .

To specify, i use build-arg to pass the command to launch our application (that is not the same between environments). The first one passed is LAUNCH_COMMAND, that is, the name of the variable in the Dockerfile, the second one with the ${} syntax is AWS CodeBuild's environment variable.

The problem is, that my launch command contains spaces.

Here is what i tried :

CodeBuild variable with spaces : KO

Written directly in the docker build command with spaces and surrounded by quote : KO

As i found later on, according to this thread, we need to escapes variables to keep spaces when Dockerfile get them.

CodeBuild variable with spaces escaped : KO

Written directly in the docker build command with spaces escaped and surrounded by quote : OK

By doing an echo on the CodeBuild environment, the space is kept, as well as the escaping character. So the problem seems to be when CodeBuild pass variables to docker build.

The error message i get in my build is : errorMessageWhenKO


Solution

  • As @AymDev stated in a comment, it worked by adding quotes around the CodeBuild environment variable in the buildspec.yml file.

    That means doing something like this :

    docker build -t ABC:v1 --build-arg LAUNCH_COMMAND="${LAUNCH_COMMAND}" -f ${DOCKERFILE_PATH} .