2

I have to execute scripts inside a docker container, example

composer install

or

bin/another-script another-script-arg-1 another-script-arg-2

in order to execute this command inside the container I have to execute something like (using docker-compose)

sudo docker-compose exec container-name sh -c 'bin/another-script another-script-arg-1 another-script-arg-2'

I want write a simple script that allow me save some typos and extra strokes for running just variances of the previous command, so I wrote an script

#!/usr/bin/env bash
sudo docker-composer exec container-name sh -c "'bin/another-script $@'"

So I can execute outside, just

bin/another-script another-script-arg-1 another-script-arg-2

but doesnt work, just tellme something like 'bin/another-script' not found (inside container). I know this happens when the single quotes are not passed around the -c '' part. I have tried different stuff

#!/usr/bin/env bash
sudo docker-composer exec container-name sh -c \'bin/another-script $@\'

or

#!/usr/bin/env bash
sudo docker-composer exec container-name sh -c "'bin/another-script $@'"

or

#!/usr/bin/env bash
sudo docker-composer exec container-name sh -c "'"bin/another-script "$@""'"

Edit: the internal path of the script bin/another-script is not a problem because container move to the working dir, any case I've tested with the absolute path getting the same result /app/bin/another-script

#!/usr/bin/env bash
sudo docker-composer exec container-name sh -c "'/app/bin/another-script $@'"

but nothing works, any advice?

  • The path is not a problem I can pass the absolute path and have the same result – rkmax Feb 14 at 14:54
3

After a lot of trial and error. I found a way that worked I dont know if it's the best or correct way

#!/usr/bin/env bash

container_cmd="bin/another-script $@"
sudo docker-composer exec container-name sh -c "$container_cmd"

I'd like to know why single quotes are not necessary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.