Skip to content
Advertisement

How to pass backtick to Dockerfile CMD?

I have a docker image running java application, its main class is dynamic, in a file called start-class. Traditionally, I started the application like this.

java <some_options_ignored> `cat start-class`

Now I want to run these applications in docker containers. This is my Dockerfile.

FROM openjdk:8
##### Ignored
CMD ["java", "`cat /app/classes/start-class`"]

I built the image and run the containers. The command actually executed was this.

$ docker ps --no-trunc | grep test
# show executed commands
"java '`cat /app/classes/start-class`"

Single quotes was automatically wrapped outside the backticks. How can I fix this??

Advertisement

Answer

You’re trying to run a shell command (expanding a sub-command) without a shell (the json/exec syntax of CMD). You need to switch to the shell syntax (or explicitly run a shell with the exec syntax). That would look like:

CMD exec java `cat /app/classes/start-class`

Without the json formatting, docker will run

sh -c "exec java `cat /app/classes/start-class`"

The exec in this case will replace the shell in pid 1 with the java process to improve signal handling.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement