Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of docker-compose empty variable warnings that are only used in image builds and not at runtime?

I have a build script which defines the values of following variables to be used by docker-compose at runtime.

# build.sh
...
export FOO="foo"
export BAR="bar"
export BINARIES_DIR="./"
export BUILD_SRC=`pwd`
docker-compose build

And this is service definition in compose:

services:
  service_container:
    image: blabla:tag
    build:
      dockerfile: ${BUILD_SRC}/dockerfiles/Dockerfile.service
      context: "${BINARIES_DIR}"
      args:
        FOO: ${FOO}
        BAR: ${BAR}
    container_name: alpha_null
    network_mode: "host"
    privileged: true
    .
    .
    .
    command: /bin/bash 

Once I build my service, I start the container using docker-compose up.

During build, docker-compose expands the variables as passed in the shell script.

Evidently no variables defined in build/dockerfile/context are needed to start the container.

But I get these warnings which I want to get rid of:

WARNING: The BUILD_SRC variable is not set. Defaulting to a blank string.
WARNING: The BINARIES_DIR variable is not set. Defaulting to a blank string.
WARNING: The FOO variable is not set. Defaulting to a blank string.
WARNING: The BAR variable is not set. Defaulting to a blank string.

I have seen related issues that advocate using env files to define variables inside the container. But in this case I need the variables only at build time, and not at runtime. How to get rid of these warnings.

like image 506
Karan Shah Avatar asked Jan 19 '26 16:01

Karan Shah


1 Answers

You should be able to split these details out of the main docker-compose.yml file.

Since it looks like your script is basically passing in every build option to the Compose setup, it might be easier to put the docker build command in your wrapper script, and remove the build: block from the docker-compose.yml file:

#!/bin/sh
# build.sh
...
docker build \
  --build-arg FOO=foo \                 # arg:
  --build-arg BAR=bar \                 # arg:
  -f ./dockerfiles/Dockerfile.service \ # dockerfile:
  -t blabla:tag                         # image:
  .                                     # context:

Another option is to use two Compose YAML files. Create a second Compose file that only contains the build: definitions:

version: '3.8'
services:
  service_container:
    build:
      dockerfile: ${BUILD_SRC}/dockerfiles/Dockerfile.service
      context: "${BINARIES_DIR}"
      args:
        FOO: ${FOO}
        BAR: ${BAR}
    # No runtime options

Then in your build script, invoke this overlay file

docker-compose \
  -f docker-compose.yml \
  -f docker-compose.build.yml \
  build

In both cases you need to remove the build: definitions from the main docker-compose.yml file. That in turn will remove the environment-variable references and get rid of those warnings.

like image 158
David Maze Avatar answered Jan 22 '26 22:01

David Maze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!