Passing a command with arguments as a string to launch dockers

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:

#!/bin/bash -x
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:

#!/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 separate command and arguments in an docker runinside script?

+4
1

docker run, :

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

, :

DOCKER_COMMAND='bash -c "(cd build && make)"'
docker run --rm -it myImage "$DOCKER_COMMAND"

$DOCKER_COMMAND COMMAND. Docker , bash -c "(cd build && make)", , . "- ". , .

$DOCKER_COMMAND, ( , ):

docker
run
--rm
-it
myImage
bash
-c
"(cd
build
&&
make)"

, bash script "(cd, , unexpected EOF while looking for matching "error. Bash's -c` , - , , 4.

:

DOCKER_COMMAND='cd build && make'
docker run --rm -it myImage bash -c "$DOCKER_COMMAND"

( , , .)

, docker run bash, bash -c ( $DOCKER_COMMAND).

+3

Source: https://habr.com/ru/post/1654308/


All Articles