Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use environment variables in CMD

Can I use environment variables in my CMD stanza in a Dockerfile?

I want to do something like this:

CMD ["myserver", "--arg=$ARG", "--memcache=$MEMCACHE_11211_TCP_ADDR:$MEMCACHE_11211_TCP_PORT"] 

Where $MEMCACHE_11211_TCP_* would be set automatically by the inclusion of the --link parameter of my docker run command. And $ARG would be configurable by the user at runtime, maybe by the "-e" parameter?

This doesn't seem to be working for me, it seems to be literally passing through the string "$ARG" for example.

like image 793
brooks94 Avatar asked Apr 14 '14 22:04

brooks94


People also ask

How do I check environment variables in cmd?

To Check if an Environment Variable ExistsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable.

How do I use environment variables?

In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable. Click Edit to modify an existing environment variable.

How do I find environment variables in Windows 10 cmd?

You can open a Command Prompt, type set , and press Enter to display all current environment variables on your PC. You can open PowerShell, type Get-ChildItem Env: , and press Enter to display all current environment variables on your PC.


1 Answers

This answer may be a little late. But environment for CMD is interpreted slightly differently depending on how you write the arguments. If you pass the CMD as a string (not inside an array), it gets launched as a shell instead of exec. See https://docs.docker.com/engine/reference/builder/#cmd.

You may try the CMD without the array syntax to run as a shell:

CMD myserver --arg=$ARG --memcache=$MEMCACHE_11211_TCP_ADDR:$MEMCACHE_11211_TCP_PORT 
like image 181
Andy Shinn Avatar answered Oct 13 '22 08:10

Andy Shinn