Home > Back-end >  Env variable invalid interpolation format for "required variable is missing a value"
Env variable invalid interpolation format for "required variable is missing a value"

Time:03-13

I have this docker-compose.yaml

version: "3.7"
services:
  busybox:
    image: busybox:latest
    entrypoint: ''
    environment:
      - bar=${foo:-baz}
    command: 
      - "/bin/sh" 
      - "-c" 
      - "echo $$bar"

that I'm running this way:

docker-compose up && docker-compose down

output:

[ ] Running 2/2
 ⠿ Network interpolation-poblem_default      Created
 ⠿ Container interpolation-poblem-busybox-1  Created
Attaching to interpolation-poblem-busybox-1
interpolation-poblem-busybox-1  | baz
interpolation-poblem-busybox-1 exited with code 0
[ ] Running 2/2
 ⠿ Container interpolation-poblem-busybox-1  Removed
 ⠿ Network interpolation-poblem_default      Removed

It works properly - foo is substituted with baz that is echoed by: /bin/sh -c "echo $bar"

interpolation-poblem-busybox-1  | baz

but when I change bar=${foo:-baz} to bar=${foo:?mandatory}

version: "3.7"
services:
  busybox:
    image: busybox:latest
    entrypoint: ''
    environment:
      - bar=${foo:?mandatory}
    command: 
      - "/bin/sh" 
      - "-c" 
      - "echo $$bar"

and run again:

docker-compose up && docker-compose down

I'm getting the output:

invalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $

The error info is a bit confusing, it sounds to me as if literary the mandatory string was expected. But even if I run:

foo=mandatory docker-compose up && docker-compose down

output:

[ ] Running 1/1
 ⠿ Container interpolation-poblem-busybox-1  Created
Attaching to interpolation-poblem-busybox-1
interpolation-poblem-busybox-1  | mandatory
interpolation-poblem-busybox-1 exited with code 0
invalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $

note the output as above has:

interpolation-poblem-busybox-1  | mandatory

but lacks of:

[ ] Running 2/2
 ⠿ Container interpolation-poblem-busybox-1  Removed
 ⠿ Network interpolation-poblem_default      Removed

that was present in the example of bar=${foo:-baz} when everything worked well. I looks that the command docker-compose down did not do any job and in fact running:

docker ps -a

shows the output:

CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS                     PORTS     NAMES
3b7c0a9caa63   busybox:latest   "/bin/sh -c 'echo $b…"   3 minutes ago   Exited (0) 3 minutes ago             interpolation-poblem-busybox-1

so the container interpolation-poblem-busybox-1 was not removed.

Trying to take it down again by:

docker-compose down

gives the same output:

invalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $

Without the container being removed.

Even running:

docker-compose ps

generates the same output error.

as per documentation

Similarly, the following syntax allows you to specify mandatory variables:

  • ${VARIABLE:?err} exits with an error message containing err if VARIABLE is unset or empty in the environment.
  • ${VARIABLE?err} exits with an error message containing err if VARIABLE is unset in the environment.

the syntax of:

    environment:
      - bar=${foo:?mandatory}

seems to be correct.

Host's system:

docker --version

Docker version 20.10.13, build a224086

Docker Engine release notes v20.10 - however I see that the release on github is v19.03.14

docker-compose --version

Docker Compose version v2.3.3

Docker Compose release notes - There is no version v2.3.3 there, the highest is 1.29.2, but at github releases has v2.3.3

hostnamectl | grep Kernel

Kernel: Linux 4.15.0-171-generic

I guess that problem may be related to the Docker version 20.10.13, build a224086 that I recently updated by Ubuntu prompt and not checking off the Docker box from the list of choice.

There is no docker version v20.x at the Compatibility matrix

I could not find similar list for the docker-compose command.

However none of the versions I use are tagged with dev, alpha, beta, rc, nb so I would expect that I should run pretty stable release but maybe I am wrong.

Could anyone provide the solution - how to use:

    environment:
      - bar=${foo:?mandatory}

without any problems as described above?

The issues about interpolation do not clarify what I did wrong:

docker-compose is:issue is:open invalid interpolation

It would be helpful If someone could share a link to an official document that describes the release policy of docker and docker-compose including their road-map and possible info about their period of support plus the clarification on stable releases naming convention.

I only found this roadmap that says not too much.

CodePudding user response:

When you run foo=mandatory docker-compose up && docker-compose down, you are running foo=mandatory docker-compose up and then docker-compose down, which means the docker-compose down is not receiving that variable, and thus giving you that error when trying to read the template.

If you run foo=mandatory docker-compose up && foo=mandatory docker-compose down, it will work. You may also export the variable so you don't need to pass it twice:

export foo=mandatory
# now foo=mandatory will be passed to all commands after the export
docker-compose up && docker-compose down
  • Related