こんにちは! JQです。
前回は『OSS編~Dockerを試してみる③~』と題して、docker-pyを利用してPythonからDockerを操作して試してみました。
今回は『OSS編~Dockerを試してみる④~』と題して、AnsibleからDockerを操作して試してみたいと思います。
Ansible設定
1.Ansibleの設定
先ずはローカルに対してAnsibleを実行出来るように設定を行っておきます。
詳細はこちらの記事をご参照ください。
イメージの作成
2. docker_imageモジュールの利用
Ansibleのdocker_imageを利用してDockerのイメージを作成します。
ApacheをインストールするDockerfileを作成します。
1 2 |
$ vim Dockerfile FROM centos |
1 2 |
RUN yum -y install httpd CMD /usr/sbin/apachectl -D FOREGROUND |
次に作成したDockerfileを作成するymlファイルを作成します。
「path」にはDockerfileがあるディレクトリを指定します。
1 2 3 4 5 6 7 8 |
$ vim docker.yml - name: Docker hosts: test remote_user: ec2-user sudo: yes tasks: - name: check or build image docker_image: path="/home/ec2-user/" name="devcentos/apache" state=present |
実行してみます。
1 2 3 4 5 6 7 8 9 10 11 12 |
$ ansible-playbook docker.yml PLAY [Docker] ***************************************************************** GATHERING FACTS *************************************************************** ok: [ec2-user@localhost] TASK: [check or build image] ************************************************** changed: [ec2-user@localhost] PLAY RECAP ******************************************************************** ec2-user@localhost : ok=2 changed=1 unreachable=0 failed=0 |
確認してみます。
1 2 3 4 5 6 7 8 |
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE devcentos/apache latest 65d1bef282cd 4 minutes ago 373 MB <none> <none> e10082e3a7a4 2 hours ago 373 MB <none> <none> 64abf57c8eff 2 hours ago 373 MB centos centos6 0b443ba03958 6 days ago 297.6 MB centos latest 0b443ba03958 6 days ago 297.6 MB centos 6.4 539c0211cd76 12 months ago 300.6 MB |
3. docker (manage)モジュールの利用
次にdocker(manage)モジュールを利用してコンテナを起動してみます。
次の様にtasksを変更します。
1 2 3 4 5 6 7 8 9 10 11 |
$ vim docker.yml - name: Docker hosts: test remote_user: ec2-user sudo: yes tasks: - name: run devcentos/apache docker: image=devcentos/apache ports=8080:80 - name: Display IP address and port mappings for containers debug: msg={{inventory_hostname}}:{{item['HostConfig']['PortBindings']['80/tcp'][0]['HostPort']}} with_items: docker_containers |
実行してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
$ ansible-playbook docker.yml PLAY [Docker] ***************************************************************** GATHERING FACTS *************************************************************** ok: [ec2-user@localhost] TASK: [run devcentos/apache] ************************************************** changed: [ec2-user@localhost] TASK: [Display IP address and port mappings for containers] ******************* ok: [ec2-user@localhost] => (item={u'HostsPath': u'/var/lib/docker/containers/ad0b7d44630fb53a77b46bb5e829d42d622c9f9761c5fb30bb0d02ff335581d2/hosts', u'Created': u'2014-04-23T09:27:44.89622808Z', u'Image': u'65d1bef282cdaa5e2ec59f44d0cdece7e0098b848df4014a8cabbe972c7c5ad1', u'Args': [u'-c', u'/usr/sbin/apachectl -D FOREGROUND'], u'Driver': u'devicemapper', u'HostConfig': {u'ContainerIDFile': u'', u'Links': None, u'LxcConf': None, u'PortBindings': {u'80/tcp': [{u'HostIp': u'0.0.0.0', u'HostPort': u'8080'}]}, u'Binds': None, u'PublishAllPorts': False, u'Privileged': False}, u'Id': u'ad0b7d44630fb53a77b46bb5e829d42d622c9f9761c5fb30bb0d02ff335581d2', u'State': {u'Ghost': False, u'Pid': 30186, u'Running': True, u'FinishedAt': u'0001-01-01T00:00:00Z', u'StartedAt': u'2014-04-23T09:27:45.647950876Z', u'ExitCode': 0}, u'ExecDriver': u'native-0.1', u'ResolvConfPath': u'/etc/resolv.conf', u'Volumes': {}, u'Path': u'/bin/sh', u'HostnamePath': u'/var/lib/docker/containers/ad0b7d44630fb53a77b46bb5e829d42d622c9f9761c5fb30bb0d02ff335581d2/hostname', u'Config': {u'Env': [u'HOME=/', u'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'], u'Hostname': u'ad0b7d44630f', u'Entrypoint': None, u'Dns': None, u'Memory': 0, u'OnBuild': None, u'OpenStdin': False, u'User': u'', u'CpuShares': 0, u'AttachStdout': False, u'NetworkDisabled': False, u'WorkingDir': u'', u'Cmd': [u'/bin/sh', u'-c', u'/usr/sbin/apachectl -D FOREGROUND'], u'StdinOnce': False, u'PortSpecs': None, u'AttachStdin': False, u'Volumes': None, u'MemorySwap': 0, u'VolumesFrom': u'', u'Tty': False, u'AttachStderr': False, u'Domainname': u'', u'Image': u'devcentos/apache', u'ExposedPorts': None}, u'VolumesRW': {}, u'NetworkSettings': {u'Bridge': u'docker0', u'PortMapping': None, u'Gateway': u'172.17.42.1', u'IPPrefixLen': 16, u'IPAddress': u'172.17.0.2', u'Ports': {u'80/tcp': [{u'HostIp': u'0.0.0.0', u'HostPort': u'8080'}]}}, u'Name': u'/angry_brattain'}) => { "item": { "Args": [ "-c", "/usr/sbin/apachectl -D FOREGROUND" ], "Config": { "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": [ "/bin/sh", "-c", "/usr/sbin/apachectl -D FOREGROUND" ], "CpuShares": 0, "Dns": null, "Domainname": "", "Entrypoint": null, "Env": [ "HOME=/", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "ExposedPorts": null, "Hostname": "ad0b7d44630f", "Image": "devcentos/apache", "Memory": 0, "MemorySwap": 0, "NetworkDisabled": false, "OnBuild": null, "OpenStdin": false, "PortSpecs": null, "StdinOnce": false, "Tty": false, "User": "", "Volumes": null, "VolumesFrom": "", "WorkingDir": "" }, "Created": "2014-04-23T09:27:44.89622808Z", "Driver": "devicemapper", "ExecDriver": "native-0.1", "HostConfig": { "Binds": null, "ContainerIDFile": "", "Links": null, "LxcConf": null, "PortBindings": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "8080" } ] }, "Privileged": false, "PublishAllPorts": false }, "HostnamePath": "/var/lib/docker/containers/ad0b7d44630fb53a77b46bb5e829d42d622c9f9761c5fb30bb0d02ff335581d2/hostname", "HostsPath": "/var/lib/docker/containers/ad0b7d44630fb53a77b46bb5e829d42d622c9f9761c5fb30bb0d02ff335581d2/hosts", "Id": "ad0b7d44630fb53a77b46bb5e829d42d622c9f9761c5fb30bb0d02ff335581d2", "Image": "65d1bef282cdaa5e2ec59f44d0cdece7e0098b848df4014a8cabbe972c7c5ad1", "Name": "/angry_brattain", "NetworkSettings": { "Bridge": "docker0", "Gateway": "172.17.42.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "PortMapping": null, "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "8080" } ] } }, "Path": "/bin/sh", "ResolvConfPath": "/etc/resolv.conf", "State": { "ExitCode": 0, "FinishedAt": "0001-01-01T00:00:00Z", "Ghost": false, "Pid": 30186, "Running": true, "StartedAt": "2014-04-23T09:27:45.647950876Z" }, "Volumes": {}, "VolumesRW": {} }, "msg": "ec2-user@localhost:8080" } PLAY RECAP ******************************************************************** ec2-user@localhost : ok=3 changed=1 unreachable=0 failed=0 |
確認してみます。
1 2 3 |
$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad0b7d44630f devcentos/apache:latest /bin/sh -c /usr/sbin About a minute ago Up About a minute 0.0.0.0:8080->80/tcp angry_brattain |
いかがでしたでしょうか?
次回もお楽しみに!!!