ホストOSからコンテナの停止と起動を制御する方法
ホストOSからコンテナの停止と起動を制御するにはdocker container stopコマンド、docker container startコマンドを実行します。
コマンドの説明
$ docker container stop --help Usage: docker container stop [OPTIONS] CONTAINER [CONTAINER...] Stop one or more running containers Options: -t, --time int Seconds to wait for stop before killing it (default 10) $ docker container start --help Usage: docker container start [OPTIONS] CONTAINER [CONTAINER...] Start one or more stopped containers Options: -a, --attach Attach STDOUT/STDERR and forward signals --detach-keys string Override the key sequence for detaching a container -i, --interactive Attach container's STDIN
実行例
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 3 days ago 73.9MB centos latest 470671670cac 3 months ago 237MB centos 7.7.1908 08d05d1d5859 5 months ago 204MB $ docker container run -itd --name test01 centos:7.7.1908 /bin/bash # バックグラウンドで起動する 1f5a5464ec123ec9c2f26d645218c7025b8886c3d9ad38413b32fac613db6236 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f5a5464ec12 centos:7.7.1908 "/bin/bash" 2 seconds ago Up 1 second test01 $ docker container stop test01 # コンテナを停止する test01 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f5a5464ec12 centos:7.7.1908 "/bin/bash" 19 seconds ago Exited (137) 2 seconds ago test01 $ docker container start test01 # コンテナを起動する test01 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f5a5464ec12 centos:7.7.1908 "/bin/bash" 31 seconds ago Up 1 second test01