Docker CMD on Jelastic with parameters containing spaces

If you are trying to run your own NGINX container on Jelastic platform, you will probably have a CMD like this in your Dockerfile:

CMD ["nginx", "-g", "daemon off;"]

And you will get this error:

nginx: invalid option: "off"

TL;DR

Use the Shell form of CMD. It works on Jelastic as well.

CMD nginx -g "daemon off;"

Now if you want to get a better understanding of how Docker translates the CMD command, depending on the form used: Shell/Exec, keep reading.

# Exec form : json array of command params
CMD ["nginx", "-g", "daemon off;"]
# Shell form : command as called in terminal
CMD nginx -g "daemon off;"

Following are a few failed attempts at solving this issue.

Attempt 1. Exec Form: Works on most Docker but not jelastic

CMD ["nginx", "-g", "daemon off;"]

Jelastic has trouble with it. Getting :

nginx: invalid option: "off"

the daemon off; part of the CMD gets passed as multiple args in jelastic (i.e. daemon, off;). Probably because they copy it somehow and dont escape stuff.

Failed Attempt 2. Passing daemon off; into conf file

RUN echo "daemon off;" >> /etc/nginx/conf.d/nginx.conf
CMD ["nginx"]

Source Source Source

adding this to conf does not solve the issue, when we run CMD ["nginx"], it starts in the background which makes the docker container exis.

REMEMBER: Docker needs to have a process running in the foreground, and unless we pass -g "daemon off;" along nginx, Nginx will run in the background.

Explanation

Failed Attempt 3. Exec Form singlequoted

CMD ["nginx", "-g", "'daemon off;'"]

That's how it is packaged in the docker image:

"Cmd": [
  "nginx",
  "-g",
  "'daemon off;'"
]

And we are greeted with an error on exec:

[emerg] 1#1: unexpected end of parameter, expecting ";" in command line
nginx: [emerg] unexpected end of parameter, expecting ";" in command line

Failed Attempt 4. Exec Form escaped double quotes

CMD ["nginx", "-g", "\"daemon off;\""]

Packaged as:

"Cmd": [
  "nginx",
  "-g",
  "\"daemon off;\""
]

Error:

[emerg] 1#1: unexpected end of parameter, expecting ";" in command line
nginx: [emerg] unexpected end of parameter, expecting ";" in command line

Failed Attempt 5. Exec Form Double escape the space

CMD ["nginx", "-g", "daemon\\ off;"]

Packaged as:

"Cmd": [
    "nginx",
    "-g",
    "daemon\\ off;"
]

Error:

[emerg] 1#1: unknown directive "daemon\ off" in command line
nginx: [emerg] unknown directive "daemon\ off" in command line

Failed Attempt 6. Exec Form single escape the space

CMD ["nginx", "-g", "daemon\ off;"]

Packaged as:

"Cmd": [
    "/bin/sh",
    "-c",
    "[\"nginx\", \"-g\", \"daemon\\ off;\"]"
]

Error:

/bin/sh: [nginx,: not found

Failed Attempt 7. Shell form quoted pass whole thing single escaped

CMD "nginx -g daemon\ off;"

Packaged as:

"Cmd": [
    "/bin/sh",
    "-c",
    "\"nginx -g daemon\\ off;\""
],

Error:

/bin/sh: nginx -g daemon\ off;: not found

Explanation: the whole thing is considered a single command without params

Failed Attempt 8. Shell form unquoted: Not work

CMD nginx -g daemon off;
"Cmd": [
    "/bin/sh",
    "-c",
    "nginx -g daemon off;"
],

Error:

nginx: invalid option: "off"

Explanation: the whole thing is passed as command + 3 params What we need is command + 2 params

Final Attempt 9. Shell form quoted: works in Docker and Jelastic

CMD nginx -g "daemon off;"
"Cmd": [
    "/bin/sh",
    "-c",
    "nginx -g \"daemon off;\""
],

Thanks