LoginSignup
2
1

More than 3 years have passed since last update.

[Docker・Ansible]15分でWebサーバ構築

Last updated at Posted at 2019-09-16

Docker及びAnsibleを使用し、コマンド一つで仮想環境上にWebサーバを構築しようと思います。
15分以内で構築できることを目標としましょう。

前提条件は以下の通りです。

  • ホスト環境はMacOS
  • VirtualBox、Vagrant、Ansibleがホスト環境にインストールされていること

では早速やっていきます。

ディレクトリ・ファイルを作成する(2 ~ 3分)

以下のようなディレクトリ構成を作成してください。

.
├── Vagrantfile
├── ansible.cfg
├── hosts
├── playbook.yml
└── roles
    ├── docker
    │   └── tasks
    │       └── main.yml
    └── docker-compose
        ├── tasks
        │   └── main.yml
        └── template
            └── docker-compose.yml

ファイルのコピペ(3 ~ 4分)

上記の構造で作成できたら各ファイルの中身をコピペしてください。

  • Vagrantfile
Vagrant.configure(2) do |config|
 config.vm.define "WebServerByDocker" do |node|
    node.vm.box = "bento/ubuntu-18.04"
    node.vm.hostname = "WebServerByDocker"
    node.vm.network :private_network, ip:"192.168.5.5"
    node.vm.provider "virtualbox" do |vb|
      vb.customize ["modifyvm", :id, "--memory", "1024"]
    end
    node.vm.provider "virtualbox" do |vb|
      vb.gui = false
    end
  end
end
  • ansible.cfg
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
  • hosts
[WebServer]
192.168.5.5
  • playbook.yml
---
- hosts: WebServer
  become: no
  roles:
    - {role: 'docker', tags: 'docker'}
    - {role: 'docker-compose', tags: 'docker-compose'}
  • roles/docker/tasks/main.yml
- name: apt-get update
  become: yes
  apt:
    update_cache: yes

- name: install required package
  become: yes
  apt:
    name: "{{item}}"
  with_items:
    - apt-transport-https
    - ca-certificates
    - curl
    - software-properties-common

- name: install GPO key
  become: yes
  shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

- name: add repository
  become: yes
  shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

- name: apt-get update
  become: yes
  apt:
    update_cache: yes

- name: install docker-ce
  become: yes
  apt:
    name: docker-ce
  • roles/docker-compose/tasks/main.yml
- name: install docker-compose
  become: yes
  shell: curl -L "https://github.com/docker/compose/releases/download/1.12.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

- name: change right of docker-compose
  become: yes
  shell: chmod +x /usr/local/bin/docker-compose

- name: copy docker-compose.yml
  become: yes
  copy:
    src: template/docker-compose.yml
    dest: /home/vagrant/
    mode: 644

- name: container up
  become: yes
  shell: docker-compose up -d
  • roles/docker-compose/template/docker-compose.yml
version: "3"
services:
  WebServer:
    image: nginx:alpine
    ports:
      - 8888:80

コマンドの実行(6 ~ 7分)

仮想環境の立ち上げ、Ansibleの実行を行います。
作成したディレクトリのルートディレクトリで実行してください。
仮想環境でのDocker、Docker-compose のインストールに時間がかかります。

$ vagrant up
$ ansible-playbook -i hosts playbook.yml -u vagrant -k
SSH password: vagrant
.
.
.
192.168.5.5                : ok=11   changed=10   unreachable=0    failed=0

Webサーバにアクセス

Chromeなどを使用し、Webサーバが立ち上がっているか確認します。
192.168.5.5:8888 を入力してください。

スクリーンショット 2019-09-17 0.35.01.png

nginx が無事起動しています!

おわりに

いかがだったでしょうか。
今の時代Webサーバ一つ立てるのも苦労がありませんね。
Docker、Docker-composeの詳細はまた別の記事で触れようと思います。
Ansibleについては簡単に概要や一通りの流れをまとめているのでそちらを参照してみてください。

2
1
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
2
1