LoginSignup
6
2

More than 3 years have passed since last update.

起動中のDockerコンテナのstdinに入力する

Last updated at Posted at 2019-12-15

概要

こんにちは、haniokasaiです。
Dockerでdocker attach するとき、インタラクティブに操作できるのはpid 1だけです。1
通常それで困りませんが、私は困りました。
私のコンテナではDockerfileはシェルスクリプトを呼び出し、そのシェルスクリプトがPHP CGIを実行します。その場合は、phpのpidは1ではないので入力するすべがありません。(出力はattachで見れます。)
ですので、stdinに直接入力してあげました。

環境

  • Ubuntu 18.04 on XFS
  • Docker

ホスト to コンテナのstdin

注意
docker attachはしていても構いませんが、interactiveにはしないでください。
していると、stdinは「Text file busy」と返して成立しません。
attach時には以下のようにします。(この場合はattachではstdinを受けません。attachかファイル直接入力かどちらかを選ぶということ。)

attach
docker -H DOCKERHOST attach コンテナ名 --no-stdin

まず、topで標準入力先のpidを確認します。

# docker top zzzzzzzz_container
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                25312               25287               0                   16:09               ?                   00:00:00            /bin/sh -c sh /minecraft/resources/run-Main.sh
root                25366               25312               0                   16:10               ?                   00:00:00            sh /minecraft/resources/run-Main.sh
root                25434               25366               0                   16:10               ?                   00:00:00            sh /minecraft/resources/run-BE-BDS.sh
root                25472               25434               13                  16:10               ?                   00:14:22            /minecraft/bin/bedrock_server

標準入力をコマンドで実行します

# echo "help" > /proc/25472/fd/0

helpをbedrock_serverに標準入力すると、ヘルプがちゃんと返されます。

# docker logs zzzzzzzz_container --tail=10
[2019-12-15 08:09:39 INFO] Player disconnected: hanicraft, xuid: 2535460621431466
§2--- Showing help page 1 of 18 (/help <page>) ---
/? [command: CommandName]
/? <page: int>
/alwaysday [lock: Boolean]
/changesetting allow-cheats <value: Boolean>
/changesetting difficulty <value: Difficulty>
/changesetting difficulty <value: int>
/clear [player: target] [itemName: Item] [data: int] [maxCount: int]
§2Tip: Use the <tab> key while typing a command to auto-complete the command or its arguments

コンテナ to コンテナのstdin

送信元コンテナ作成

docker create --name コンテナ名 --memory=3500mb --network="host" --storage-opt size=2G  --cap-add SYS_PTRACE  -v /proc:/newproc:ro

注目ポイントは

  • SYS_PTRACE権限の追加
  • ホストの/procを/newprocとして読み取り専用でマウント

あとはさっきと同じです。

コンテナ内
# echo "help" > /newproc/25472/fd/0

利用して作ったライブラリ

@itsu_dev と作りました。
いつくんがtopのスクレイピングしてくれているので興味深いでしょう。

参考文献

https://serverfault.com/questions/178457/can-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi
https://orebibou.com/2016/04/linux%E3%81%A7%E5%8B%95%E4%BD%9C%E4%B8%AD%E3%81%AE%E3%83%97%E3%83%AD%E3%82%BB%E3%82%B9%E3%81%AE%E5%87%BA%E5%8A%9B%E5%86%85%E5%AE%B9%E3%82%92%E3%81%BF%E3%82%8B/

脚注

6
2
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
6
2