Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker compose unexpectedly replaces environment: VAR=* with an ls of the contents of the root folder inside the container

I have a docker compose service defined with an environment variable:

    command: /run.sh
    entrypoint: /bin/sh
    environment:
      - TEST=*

In run.sh I have the following echo statement:

echo TEST: ${TEST}

The unexpected result of that echo statement is an ls of the root folder inside the docker container.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TEST: bin dev etc home lib media mnt opt proc root run run.sh sbin secrets srv static sys tmp usr var

My goal is to set the environment variable to the literal value *.

I’ve tried single quoting it and escaping it:

    environment:
      - TEST='*'

    environment:
      - TEST=\*

In those two cases I get the literal values '*' and \* passed in, both of which break the app that I’m using because it’s expecting the value to be a single * character, not quoted or escaped.

GPT4 considered this unexpected behavior and I’m not finding much on it in google searches.

>Solution :

The issue is unrelated to docker-compose.

Unquoted variable expansion in the shell undergoes word splitting and filename expansion. * is filename expansion trigger in shell. * is replaced by the list of files in the current working directory in the shell script you are running with /bin/sh.

Quote variable expansion to prevent filename expansion and word splitting expansion. Check your scripts with shellcheck.

echo TEST: "$TEST"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading