The issue I'm facing is how to pass a command with arguments to docker run
. The problem is that docker run
does not take command plus arguments as a single string. They need to be provided as individual first-class arguments to docker run
, such as:
#!/bin/bash
docker run --rm -it myImage bash -c "(cd build && make)"
However consider the command and argument as the value of a variable:
#!/bin/bash -x
DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage "$DOCKER_COMMAND"
Unfortunately this doesn't work because docker run
doesn't understand the substitution:
+ 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, removing the quotation of DOCKER_COMMAND
:
#!/bin/bash -x
DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage $DOCKER_COMMAND
Results in:
+ docker run --rm -it myImage 'bash -c "(cd build && make)"'
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 distinct command and arguments to docker run
inside a script?