26

I'm running the following RUN command in my Dockerfile, expecting a "logs" directory to be created under each of the listed subdirectories:

RUN mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs

But when I check the image, I see a directory literally called "{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}" created under /opt/seagull, instead of brace expansion taking place.

What could I be doing wrong?

Ates Goral
  • 137,716
  • 26
  • 137
  • 190

2 Answers2

43

You're not using brace expansion, because you're not using Bash. If you look at the documentation for the RUN command:

RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)

And also:

Note: To use a different shell, other than ‘/bin/sh’, use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]

So, just change the command to use the exec form and explicitly use a Bash shell:

RUN [ "/bin/bash", "-c", "mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs" ]
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • 4
    Thanks! I can't believe I spent 45 minutes on this, trying different escaping techniques, checking default bash settings, etc. – Ates Goral Oct 20 '16 at 21:17
  • 45
    Wait, what? You've answered you own question, and you're thanking yourself for it...? – Jonnix Oct 20 '16 at 21:18
  • 18
    Yes! :) It's been an exhausting day, and I needed to thank myself for finally RTFM. Also, http://stackoverflow.com/help/self-answer – Ates Goral Oct 20 '16 at 21:20
  • 13
    It's not the answering own question that confused the hell out of me, it was just the comment, but hey, if it's deserved ;D – Jonnix Oct 20 '16 at 21:22
  • 1
    Could you not `ln -f /bin/bash /bin/sh`, I believe the reason is that typically nowadays `/bin/sh` is a symlink to `/bin/dash`. – ThisGuyCantEven Jun 05 '19 at 00:23
  • Important note from the official documentation: _The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘)._ – jesugmz Mar 08 '20 at 20:34
11

If /bin/bash is available in your image, you can change the shell that the docker build system uses to execute your RUN command, like this:

SHELL ["/bin/bash", "-c"]

Now, your RUN command should work unchanged.

user14391097
  • 111
  • 1
  • 3