Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker: poorly formatted environment: variable contains whitespaces

I have multi-line environment variables:

SINGLE_LINE=VALUE
MULTI_LINE=VA
LU E

I want to pass this environment variables using a file through --env-file parameter of docker run. When I pass this file to a Docker container, using --env-file, it fails with a message:

export SINGLE_LINE=VALUE
export MULTI_LINE="VA
LU E"
env > .env
docker run -ti --rm --env-file .env busybox sh
docker: poorly formatted environment: variable 'LU E' contains whitespaces.
See 'docker run --help'.

How to fix that?

like image 554
FelikZ Avatar asked Apr 22 '26 10:04

FelikZ


2 Answers

For me this was caused by having export in the .env file:

export ENDPOINT=https://endpoint.io

Instead do

ENDPOINT=https://endpoint.io

See also: https://github.com/docker/for-linux/issues/701#issuecomment-506884219

like image 128
thisismydesign Avatar answered Apr 24 '26 04:04

thisismydesign


The problem occur because the way docker parses this file it does not accept multi-line string and whitespaces in the key names. See relevant issue.

Workaround. Strip all line-endings from multi-line variables:

>.env
for var in $(compgen -v | grep -Ev '^(BASH)'); do
    var_fixed=$(printf "%s" "${!var}" | tr -d '\n' )
    echo "$var=${var_fixed}" >>.env
done

Each line explained:

  1. >.env - make .env to be an empty file
  2. for var in $(compgen -v | grep -Ev '^(BASH)'); do - iterate over env keys
  3. var_fixed=$(printf "%s" "${!var}" | tr -d '\n' ) - remove new-lines from key value
  4. echo "$var=${var_fixed}" >>.env - write key=value pair to .env file
like image 24
FelikZ Avatar answered Apr 24 '26 05:04

FelikZ



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!