Home > Net >  Pass a command string with space to docker-compose exec
Pass a command string with space to docker-compose exec

Time:04-20

Real life scenario: In my automation script, there is a cli to run barman recover on a barman container. However it's possible to pass the "ssh xxx" as a whole string arg to the --remote-ssh-command, and barman reports "barman: error: unrecognized arguments: postgres@IP -p 60022"

docker-compose exec -T barman barman recover SRV1 last /var/lib/postgresql/data/pgdata/recovered.streaming  --remote-ssh-command "ssh postgres@IP -p 60022"

# I tried with bach -c trick, still no luck.
docker-compose exec -T barman bash -c "barman recover SRV1 last /var/lib/postgresql/data/pgdata/recovered.streaming  --remote-ssh-command 'ssh postgres@IP -p 60022'"

To simplify, imagine a minimal reproducing case:

docker-compose exec -T barman bash -c "echo hello"
# (It echos nothing. But I want it to behave like the following.)

docker-compose exec -T barman echo hello
# hello

The question is, how to pass a quoted string arg through docker-compose exec?

CodePudding user response:

It feels like you need to escape the remote command twice (the first escaping is done with the double-quotes):

#!/bin/bash

docker-compose exec -T barman \
    barman recover \
    SRV1 last /var/lib/postgresql/data/pgdata/recovered.streaming \
    --remote-ssh-command "$(printf %q "ssh postgres@IP -p 60022")"

CodePudding user response:

My solution: use docker compose instead of docker-compose.

docker compose exec -T barman bash -c "echo hello"
# hello

docker-compose exec -T barman echo hello
# hello

I believe the Python version (docker-compose) doesn't process the arguments with space well. Fortunately the Go version (docker compose) doesn't have such a problem.

I tried @Fravadona 's answer, but unluckily in Alpine Linux the printf does not have %q.

  • Related