Home > Enterprise >  How to send a command properly into docker container?
How to send a command properly into docker container?

Time:01-07

I can execute the command below in my terminal succesfully.
command:

gdalwarp -s_srs " datum=WGS84  no_defs  geoidgrids=egm96-15.gtx" -t_srs " datum=WGS84  no_def" input.tif output.tif

Now, I want to store this command into a variable and expand this command inside a docker container.

My script run.sh looks like the following. I first store my target command into mycommand and run the container with the command as input.

mycommand=$@;
docker run -ti --rm osgeo/gdal:ubuntu-small-latest /bin/bash -c "cd $(pwd); ${mycommand}"

And then I execute the run.sh as following.

bash run.sh gdalwarp -s_srs " datum=WGS84  no_defs  geoidgrids=egm96-15.gtx" -t_srs " datum=WGS84  no_def" input.tif output.tif

Issue:

  1. I was hoping everything after bash run.sh can be store literally into the mycommand variable.
  2. And inside the docker container, the mycommand can be expand and execute literally. But it looks like that the double quote in my original command will be lost during this process.

Thank you.

CodePudding user response:

You could pass the command as argument and then invoke "$@" inside the shell. I prefer mostly single quotes.

docker run -ti --rm osgeo/gdal:ubuntu-small-latest \
    /bin/bash -c 'cd '"$(pwd)"' && "$@"' -- "$@"

If only you want cd just let docker change the directory with -w. In Bash $PWD will be faster then pwd command.

docker run ... -w "$PWD" image "$@"

Research: when to use quoting in shell, difference between single and double quotes, word splitting expansion and how to prevent it, https://mywiki.wooledge.org/Quotes , bash arrays, https://mywiki.wooledge.org/BashFAQ/050 .

  •  Tags:  
  • Related