Search code examples
shelldockerenvironment-variablesdocker-entrypoint

Docker exec command in entrypoint script fails


Following the suggestion on Execute a script before CMD (I needed to clear out a temp dir before a container is re-launched, to fix a bug), I modified my docker file from using a CMD, to entrypoint as follows:

ENTRYPOINT ["/app/entrypoint.sh", "/usr/bin/java -Dlog4j.configurationFile=/app/resources/LINUX/${LOG4J_FILE} -Dpa.config=/app/resources/LINUX/${CONFIG_FILE} -jar /app/app.jar"]

and in entrypoint file end after the rm -rf command:

exec "$@"

But docker is unable to launch the container, it exits, container log shows:

+ exec '/usr/bin/java -Dlog4j.configurationFile=/app/resources/LINUX/${LOG4J_FILE} -Dpa.config=/app/resources/LINUX/${CONFIG_FILE} -jar /app/app.jar' /app/entrypoint.sh: line 7: /usr/bin/java -Dlog4j.configurationFile=/app/resources/LINUX/${LOG4J_FILE} -Dpa.config=/app/resources/LINUX/${CONFIG_FILE} -jar /app/app.jar: No such file or directory - what does this mean? what is wrong?


Solution

  • You need to break the JSON-array-format command line into separate words. Since you’ve explicitly told Docker that the “command” part of this is a single word, it’s looking for a binary file in /usr/bin named java -Dlog4j.configurationFile=..., with the spaces and options and all as part of the filename, and not finding it.

    You typically don’t want to embed the command you want to run in ENTRYPOINT, especially if you’re using this wrapper layout. Make ENTRYPOINT just be the name of the script ending in exec "$@"; it must use JSON-array syntax. Make CMD be the actual command you want to run, in whichever syntax is more convenient. (If you’re trying to expand environment variables, the shell-oriented syntax might be better.)

    ENTRYPOINT ["/app/entrypoint.sh"]
    CMD /usr/bin/java \
      -Dlog4j.configurationFile=/app/resources/LINUX/${LOG4J_FILE} \
      -Dpa.config=/app/resources/LINUX/${CONFIG_FILE} \
      -jar /app/app.jar
    

    I’ve found this pattern to be sufficiently common and useful that I’d generally recommend using ENTRYPOINT only for this sort of wrapper script; prefer putting the command to start your application as CMD, even if you don’t have an entrypoint wrapper.