简体   繁体   中英

Pass a command string with space to docker-compose exec

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 ?

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")"

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM