The problem I am facing is how to pass a command with arguments to docker run. The problem is that it docker rundoes not accept command arguments plus as one line. They should be represented as individual arguments of the first class for docker run, for example:
#!/bin/bash
docker run --rm -it myImage bash -c "(cd build && make)"
However, consider the command and argument as the value of a variable:
DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage "$DOCKER_COMMAND"
Unfortunately, this does not work because it docker rundoes not understand substitutions:
+ docker run --rm -it myImage 'bash -c "(cd build && make)"'
docker: Error response from daemon: oci runtime error: exec: "bash -c \"(cd build && make)\"": stat bash -c "(cd build && make)": no such file or directory.
A slight change by deleting the quote DOCKER_COMMAND:
DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage $DOCKER_COMMAND
Results in:
+ docker run
build: -c: line 0: unexpected EOF while looking for matching `"'
build: -c: line 1: syntax error: unexpected end of file
How can I expand a string from a variable so that it is passed as a separate command and arguments in an docker runinside script?