20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VM上のDockerコンテナに固定ipを割り振ってホスト側から接続出来るようにする

Last updated at Posted at 2014-08-01

確認環境

HOST: Mac OSX 10.9
Docker HOST: ubuntu 14.0.4

VirtualBoxにホストオンリーネットワークアダプターを追加(Mac側)

$ ADAPTER=$(VBoxManage hostonlyif create | egrep -o "vboxnet[0-9]+")
$ VBoxManage hostonlyif ipconfig $ADAPTER --ip 192.168.56.1 --netmask 255.255.255.0
$ VBoxManage dhcpserver add --ifname $ADAPTER --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254
$ VBoxManage dhcpserver modify --ifname $ADAPTER --enable

vagrantでdockerの動くubuntuを用意(Mac側)

$ vagrant box add ubuntu-14.04-amd64-vbox https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box

$ mkdir ubuntu-docker
$ cd ubuntu-docker
$ vagrant init ubuntu-14.04-amd64-vbox

$ sed -i "" -e 's/# config.vm.network "private_network", ip: "192.168.33.10"/config.vm.network "private_network", ip: "192.168.56.254"/g' ./Vagrantfile

ブリッジネットワークの設定(ubuntu側)

$ vagrant up
$ vagrant ssh
$ sudo su
$ apt-get install -y bridge-utils arping
$ curl -sSL https://get.docker.io/ubuntu/ | sudo sh
$ sed -i -e '$d' /etc/rc.local
$ echo "/sbin/brctl addbr docker1" >>  /etc/rc.local
$ echo "/sbin/ip addr add 192.168.0.1/24 dev docker1" >> /etc/rc.local
$ echo "/sbin/ifconfig docker1 up" >> /etc/rc.local
$ echo "/sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 ! -d 192.168.0.0/24 -j MASQUERADE" >> /etc/rc.local
$ echo "exit 0" >> /etc/rc.local

pipeworkのインストール(ubuntu側)

$ cd /usr/local/bin
$ wget https://raw.githubusercontent.com/jpetazzo/pipework/master/pipework
$ chmod +x pipework

設定を反映するためにリブートする(Mac側)

$ vagrant reload

ルーティングを設定(Mac側)*

$ sudo route add -net 192.168.0.0/24 192.168.56.254

ssh-serverが動作するイメージを用意する(ubuntu側)

$ docker pull sylvainlasnier/ssh

コンテナ起動(ubuntu側)

$ CID=$(docker run -d sylvainlasnier/ssh)

コンテナにipを割り振る(ubuntu側)

$ pipework docker1 $CID 192.168.0.2/24@192.168.0.1

sshログインしてみる(Mac側)

$ ssh root@192.168.0.2

ホスト側からDockerを操作出来るようにする

Dockerの起動オプションを変更(ubuntu側)

$ sed -i -e 's/DOCKER_OPTS=/DOCKER_OPTS="-H=tcp:\/\/0.0.0.0:4243"/g' /etc/init/docker.conf
$ service docker restart

zshenv等に下記を追加

export DOCKER_HOST=tcp://192.168.56.254:4243

boot2dockerみたいにMac側からdockerを操作出来るようになる

20
20
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
20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?