ホスト名を指定してDockerコンテナを起動する方法
ホスト名を指定してDockerコンテナを起動するには-hオプションを指定してdocker container runコマンド実行します。
オプションの説明(抜粋)
$ docker container run --help Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Options: -h, --hostname string Container host name -i, --interactive Keep STDIN open even if not attached --name string Assign a name to the container -t, --tty Allocate a pseudo-TTY
オプションの説明
- -hオプション:ホスト名を指定する
- -iオプション:Dockerコンテナ起動時に、標準入力(STDIN)を受け付ける
- -tオプション:仮想端末(pseudo-TTY)をコンテナに割り当てる
- --nameオプション:作成するコンテナに名前を付ける
- ※-i -tを同時に使用する場合は、-itオプションを使用できます
-hオプションを指定した場合
$ 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 -it --name test01 -h host01 centos:7.7.1908 /bin/bash [root@host01 /]# hostname host01
-hオプションを指定しない場合(ホスト名は自動的に割り当てられる)
$ 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 -it --name test01 centos:7.7.1908 /bin/bash [root@4ffe9636e019 /]# hostname 4ffe9636e019