Help us understand the problem. What is going on with this article?

AnsibleでDockerコンテナを起動するまでのPlaybook

More than 5 years have passed since last update.

Playbookのすにぺっと

タイトルの通りのすにぺっと。pipのところがイマイチですが、ひとまずこれで通った。以下はVirtualBox上のVMをvagrant upした後に適用したもの。

  • 対象ホスト:Ubuntu14.04
  • Dockerをインストール
  • Dockerイメージの起動
sample.yml
---
- hosts: all

  vars:
    myuser: docker

  user: vagrant
  sudo: yes

  tasks:
  - name: Install python-pip
    apt: pkg=python-pip state=latest update_cache=yes

  - name: Install Docker
    shell: wget -qO- https://get.docker.com/ | sh  creates=/etc/default/docker

  - name: Add a docker user
    user: name={{myuser}} shell=/bin/bash password=$1$3FCsXQtq$EKWn6gqWXj6Gto9ogr.1s. groups=sudo,docker append=yes
  - name: Set a public key for docker user
    authorized_key: user={{myuser}} key="{{ lookup('file','./files/secret.key.pub') }}"

  - name: Upgrade pip
    command: /usr/bin/easy_install -U pip
  - name: install docker-py # 1.2.x has a bug
    pip: name=docker-py version=1.1.0

  - name: Run a phusion/baseimage container
    docker:
      name: test-container
      image: phusion/baseimage:latest
      pull: missing
      command: "/bin/bash"
      state: started
      tty: True

ansible-playbook の完了後、コンテナが起動した。

docker@vagrant-ubuntu-trusty-64:~$ docker ps
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
25b9a48b3568        phusion/baseimage:latest   "/bin/bash"         52 seconds ago      Up 51 seconds                           test-container
docker@vagrant-ubuntu-trusty-64:~$

docker@vagrant-ubuntu-trusty-64:~$ docker exec -it test-container bash
root@25b9a48b3568:/#

考察中

コンテナのビルドはDockerfileでやらせて(作っておいて)、AnsibleはRunだけさせるような感じがシンプルかな。コンテナに対してもAnsibleでプロビジョンするのもできなくないけど、わかりずらくそこはダルそう。できるだけ運用なんかシンプルな方がいい。

参考

tksarah
tis
創業40年超のSIerです。
https://www.tis.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした