0

Docker version: 20.10.2, build 2291f61

In a Dockerfile, is it possible to use an ENV directive for which the key contains embedded periods? For example:

ENV story.paragraph.port 2029

And if it is possible to declare ENV variables with keys that have periods, then is it later possible to reference them, using familiar shell-interpolation, in the same Dockerfile?

EXPOSE $story.paragraph.port

The latter EXPOSE directive breaks for me.

My use-case: I have a python script that loads its configuration from an INI file. Eg, I might have configuration properties like these:

story.paragraph.word=helloworld
story.paragraph.length=256

The python logic recognizes configuration settings both from the INI file (by default) or, alternatively, in overrides specified in the environment. The idea is that a container instance could specify its own environment variables for story.paragraph.word or story.paragraph.length, and that those values would override the default configuration.

Kode Charlie
  • 1,297
  • 16
  • 32
  • For things like the port number your service runs on, you generally want to pick a fixed port number, and it's okay to hard-code _e.g._ `EXPOSE 2029`. When you go to `docker run -p ...:2029` from outside the container there's no way to reference this environment variable. – David Maze Jan 08 '21 at 12:11
  • @DavidMaze -- thx. That's a good point. – Kode Charlie Jan 08 '21 at 17:37

1 Answers1

1

Periods in env is not a valid identifier from unix prespective.

Setting env with periods inside a docker container is possible at runtime, but you can't directly access that env; you will need some workaround.

$ docker run --name test -itd -e story.paragraph.word=helloworld alpine sh
da4214ac2377cf1b3ce3af515f30e96dfadabf0140f541c06ee4a176a2bef746
$ docker exec -it test sh
/ # env | grep story
story.paragraph.word=helloworld     <=== env is set
/ # echo $story.paragraph.word
.paragraph.word                     <=== can't access the env
/ # echo ${story.paragraph.word}
sh: syntax error: bad substitution
/ # env | grep 'story.paragraph.word' | cut -f 2 -d '='    <=== need such workaround
helloworld
/ # $
mchawre
  • 10,744
  • 4
  • 35
  • 57
  • Technically, periods in environment variable names are a problem with the _shell_, not UNIX itself. At the level of a process, you can absolutely set & get environment variables whose names contain periods. But it's best to avoid them since most shells will behave as in your example. http://tpcg.io/_SKPWCE – Jonathan Gilbert Mar 28 '23 at 14:57