Docker コマンド
table:option
-d バックグラウンド起動
-p ポートマッピング
コンテナ外:コンテナ内
8080:80
-P ポートの自動マッピング
-t Docker コンテナ内の標準出力とホスト側の出力をつなげるもの
-i ホスト側の入力を Docker コンテナの標準出力をつなげるもの
キーボードから入力した文字はコンテナ内のプロセスに送られ bash などでコマンドを実行できる
-v ボリュームのバインド
ホストパス:コンテナパス
起動(例 : nginx)
code:shell
$ docker run -d nginx
コンテナ修了時に自動的にコンテナを削除するには run 時に --rm オプションをつける
停止
code:shell
コンテナ確認
起動しているコンテナ
code:shell
$ docker ps
コンテナ一覧(未起動含む)
code:shell
$ docker ps -a
全てのコンテナを修了
code:shell
$ docker kill $(docker ps -a)
コンテナの削除
コンテナ
code:shell
$ docker ps -a
$ docker rm <container name>
code:shell
$ docker rm docker ps -a -q
すべての修了しているコンテナを削除
code:shell
$ docker rm $(docker ps -a -q)
動作しているコンテナを含めて、すべてのコンテナを削除
code:shell
$ docker rm -f $(docker ps -a -q)
コンテナでシェルを実行
コンテナ内にログイン
code:shell
$ docker exec -it centos /bin/bash
ENTRYPOINT が設定されている場合には上書きする必要がある
code:shell
$ docker exec -it --entrypoint=bash my-docker-image
コンテナをホスト名を指定して実行
code:shell
$ docker run --rm -h testhost -it centos /bin/bash
testhost
root ユーザでログイン
code:bsah
$ docker exec -it --user root abcdefg /bin/bash
コンテナのデタッチ・アタッチ
デタッチ
コンテナが起動してないことを確認
code:shell
$ docker ps | grep -i centos
$
起動とデタッチ( Ctr + p + q を実行)
code:shell
$ docker run --rm -it centos /bin/bash
デタッチされていることを確認
code:shell
$ docker ps | grep -i centos
72ec6c952222 centos "/bin/bash" 16 seconds ago Up 15 seconds cocky_villani
アタッチ
code:shell
$ docker attach 72ec6c952222
実行中のコンテナにシェルで入る
run
code:shell
$ docker ps | grep -i nginx
208273a0c4d9 nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 80/tcp quizzical_cohen
コンテナに入る
code:shell
$ docker exec -it 208273a0c4d9 /bin/bash
root@208273a0c4d9:/# nginx -V
nginx version: nginx/1.15.7
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f 25 May 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.15.7/debian/debuild-base/nginx-1.15.7=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
コンテナの実行ユーザを指定
code:shell
$ docker run --rm -it -u daemon centos /bin/bash
bash-4.2$ whoami
daemon
コンテナの作業ディレクトリを指定
code:shell
$ docker run --rm -it -w /tmp centos /bin/bash
/tmp
コンテナにホストのファイル・ディレクトリをマウント
ディレクトリをマウントして起動
code:shell
$ docker run --rm -it -v /tmp/testdir:/testdir centos /bin/bash
マウント確認
code:shell
test.md
test
コンテナ内でファイル作成
code:shell
/testdir/test02.md
ログアウト(コンテナは削除される)
ホスト側でファイルが残っていることを確認
code:shell
$ cat testdir/test02.md
test02
### コンテナで ulimit によるリソース制御
- 指定方法は --ulimit リソース名=ソフトリミット:ハードリミット
- 1 MB に制限
- 1 MB 作成してみると正常
`
$ docker run --rm -it --ulimit fsize=1000000:1000000 centos dd if=/dev/zero of=file bs=1 count=1000000
1000000+0 records in
1000000+0 records out
1000000 bytes (1.0 MB) copied, 2.74646 s, 364 kB/s
`
- 2 MB 作成してみるとエラーになる
`
$ docker run --rm -it --ulimit fsize=1000000:1000000 centos dd if=/dev/zero of=file bs=1 count=2000000
dd: error writing 'file': File too large
1000001+0 records in
1000000+0 records out
1000000 bytes (1.0 MB) copied, 2.10842 s, 474 kB/s
`
### コンテナの CPU 制限
- 制限方法
- --cpuset-cpus
- CPU コアの指定
- --cpu-quota , --cpu-period
- CPU 時間の指定
- --cpu-shares
- CPU の相対量の指定
### コンテナのメモリ使用量制限
- メモリ使用量は物理メモリとスワップメモリの両方を個別に指定できる
- 制限方法
- --memory, -m
- 利用する物理メモリ量を指定
- --memory-swap
- 物理メモリとスワップメモリを合わせた量を指定
- 省略すると --memory-swap は --memory の 2 倍に設定される
- 制限なしの動作
`
$ docker run --rm centos dd if=/dev/zero of=/dev/null bs=100M count=1
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 0.0440207 s, 2.4 GB/s
`
- 物理メモリ 50 MB, スワップメモリをなしにして実行すると異常修了することを確認
`
$ docker run --rm -it --memory=50m --memory-swap=50m centos dd if=/dev/zero of=/dev/zero bs=100M count=1
$ echo $status
137
`
- 物理メモリ 50 MB, スワップメモリを 100 MB して実行すると正常に動作することを確認
`
$ docker run --rm -it --memory=50m --memory-swap=150m centos dd if=/dev/zero of=/dev/zero bs=100M count=1
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 0.344234 s, 305 MB/s
`
### コンテナのプロセス数の制限
- 制限方法
- Kernel4.3 以降
- --pid-limits
- Kernel4.3 以前
- ulimit
- 3 個に制限してみる
- --ulimit nproc=3:3
- プロセス数を 20 個に制限し、sleep コマンドを 20 回実行すると fork に失敗していることを確認
`
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: Resource temporarily unavailable
`
### コンテナと Docker ホスト間でのファイルコピー
`
$ sudo docker cp コンテナID:/path/to/file /tmp/ `
### ログの表示
`
`
- 出力開始日の指定
`
$ docker logs --since="2018-03-13T23:00:00+09:00" test-new
`
- tailf
`
`
- ログの表示ができるのは、 Docker がログ記録用に json-file, journald のいずれかを使用している場合のみ
`
$ docker info | grep "Logging Driver"
Logging Driver: json-file
`
- タグ付け
`
$ docker tag centos:latest reno:latest
`