1.8 KiB
1.8 KiB
docker container run
docker container runは、イメージをもとにコンテナを起動するためのコマンド
基本構文
docker container run [OPTION]... IMAGE [COMMAND [ARG]...]
使用例
コンテナに名前を付ける
コンテナ終了時に、コンテナを自動削除する
バックグラウンドで実行
標準入力を接続
ubuntu@localhost:~$ echo hello | docker container run --rm ubuntu cat
ubuntu@localhost:~$ echo hello | docker container run --rm --interactive ubuntu cat
hello
ubuntu@localhost:~$
-iを指定することで、ホストの標準入力をコンテナのPID1のプロセスの標準入力に接続する-iのロングオプションは--interactive- コンテナのstdout/stderrはデフォルトでdockerクライアントのstdout/stderrに接続する
コンテナ内のプロセスに疑似端末を割り当てる
実行例1:
tsubanyan@localhost:~$ docker container run --rm --tty ubuntu tty
/dev/pts/0
tsubanyan@localhost:~$ docker container run --rm ubuntu tty
not a tty
tsubanyan@localhost:~$
実行例2:
tsubanyan@localhost:~$ echo "hello" | docker container run --rm -i ubuntu cat
hello
tsubanyan@localhost:~$ echo "hello" | docker container run --rm --tty ubuntu cat
^C
^C
^C
got 3 SIGTERM/SIGINTs, forcefully exiting
tsubanyan@localhost:~$
-tで疑似端末を割り当てても、-iなしだとホスト側からの入力はできない
実行例3:
tsubanyan@localhost:~$ docker container run --rm -it ubuntu bash
root@93ef64b84948:/# exit
exit
tsubanyan@localhost:~$
- 対話的に操作できるセッションを作りたい場合は、
-iと-tの療法を指定する - 慣習的に、
-itという指定の仕方がよくされる