skip to Main Content

My Docker Compose expects a boolean (docs):

version: '3.8'

services:
  backend:
    build:
      no_cache: ${NO_CACHE}

I trigger the build like this:

NO_CACHE=true docker compose up

But I get the error

services.backend.build.no_cache must be a boolean

I’ve tried replacing ${NO_CACHE} with '${NO_CACHE}' and "${NO_CACHE}" after reading this SO answer as well as the Docker docs, but I get the same error in both cases. How can I inject a boolean from environment variables into a Docker Compose field?

$ docker -v 
Docker version 20.10.17, build 100c701

2

Answers


  1. Set environment variable:

    export NO_CACHE=false
    
    Login or Signup to reply.
  2. If the issue is that Docker Compose does not natively support injecting boolean values from environment variables (because environment variables are always treated as strings, enclosed in quotes), then you might need a wrapper script (to wrap the docker compose up call).

    For testing, try and write a shell script (run-docker.sh + chmod 755 run-docker.sh) to read the environment variable and then set the no_cache option in the docker-compose.yml file based on the value of that variable.
    You would then use docker-compose to build your services using the now correctly set no_cache option.

    #!/bin/bash
    
    # Read the environment variable
    NO_CACHE=${NO_CACHE:-false}
    
    # Determine the value to set in the docker-compose file
    if [ "$NO_CACHE" = "true" ]; then
      NO_CACHE_VALUE="true"
    else
      NO_CACHE_VALUE="false"
    fi
    
    # Update the docker-compose file with the determined value
    sed -i.bak -e "s/no_cache: .*/no_cache: $NO_CACHE_VALUE/" docker-compose.yml
    
    # Run docker-compose up
    docker compose up
    
    # Restore the original docker-compose file
    mv docker-compose.yml.bak docker-compose.yml
    

    This script assumes that your docker-compose.yml contains a line that matches the regular expression no_cache: .*. You will need to adjust the sed command if your file is structured differently.

    Then call it with:

    NO_CACHE=true ./run-docker.sh
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search