2
1

More than 3 years have passed since last update.

docker exec で ヒアドキュメントを使いたいんじゃ

Posted at

タイトルのような状況に遭遇してしまった場合

以下のように実行する。

$ docker exec -i postgres /usr/local/bin/psql db -U db_user <<EOT
select version();
EOT

                                        version
---------------------------------------------------------------------------------------
 PostgreSQL 11.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.2.0) 8.2.0, 64-bit
(1 row)

ヒアドキュメントを入力とするので -t オプション(疑似TTYの割り当て)は不要
つけた場合はエラーになる。

$ docker exec -it postgres /usr/local/bin/psql db -U db_user <<EOT
select version();
EOT
the input device is not a TTY

以下のように sh -c で実行することもできる。

$ docker exec -it postgres sh -c "/usr/local/bin/psql db -U db_user <<EOT
select version();
EOT"

                                        version
---------------------------------------------------------------------------------------
 PostgreSQL 11.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.2.0) 8.2.0, 64-bit
(1 row)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1