LoginSignup
131
123

More than 5 years have passed since last update.

Dockerで固定IPアドレスを使う。

Last updated at Posted at 2016-05-04

Dockerで固定IPアドレスを使う。

最近のDockerでは固定IPアドレスが普通に使えるようになっていたのでメモ。
試した環境はCentOS7です。

新しいバージョンのDockerをインストールする。

現在、CentOSのリポジトリにあるDockerが1.9.1と若干古めで固定IPアドレス設定に対応していません。
そこで、dockerオフィシャルリポジトリからインストールします。

まず、リポジトリ設定

$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

CentOSのリポジトリからインストールしていたらアンインストールする。

$ sudo yum remove docker docker-selinux

普通にyumを使ってインストール

$ sudo yum install docker-engine

サービスを起動

$ sudo systemctl start docker

簡単ですね。

ユーザ定義ネットワークの作成

デフォルトで用意されているネットワークでは固定IPアドレスオプションが使えないので、
ユーザ定義ネットワークを作成します。

$ docker network create --subnet=192.168.56.0/24 user_defined_nw

前にVirtualBOX+Vagrantで環境作っていたのでその名残で192.168.56.0/24を使っていますが、ここはお好みで。
細かいオプションは

$ docker network create --help

で見てください。

docker run

ユーザ定義ネットワークの作成が完了したら、あとはdocker runするだけです。
ポイントは--net=--ip=です。

$ docker run -it --name test --net=user_defined_nw --ip=192.168.56.100 -d centos:centos6 /bin/bash

状態の確認

割り当てられたIPアドレスの確認は

$ docker network inspect user_defined_nw

もしくは

$ docker inspect -f "{{ .NetworkSettings.Networks.user_defined_nw }}" test

でてきます。

参考
https://docs.docker.com/engine/installation/linux/centos/
https://docs.docker.com/engine/userguide/networking/work-with-networks/

131
123
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
131
123