LoginSignup
21
17

More than 3 years have passed since last update.

【Ubuntu 18.04】Ansible で Docker と docker-compose をインストールする

Last updated at Posted at 2019-08-04

業務で Ansible を使い始めたので、プライベートの Ubuntu マシンも Ansible で環境構築中です
公式サイトでの手順を Ansible のお作法にのっとって書き直しただけですが、無事にインストールできました🎉

【参考】
https://docs.docker.com/install/linux/docker-ce/ubuntu/
https://docs.docker.com/compose/install/
https://gist.github.com/rbq/886587980894e98b23d0eee2a1d84933

前提

  • Ansible Roles を使用
  • sudo のパスワードは別途指定が必要
  • Docker/docker-compose のバージョンを固定したい場合はそれぞれ追加の指定が必要

できたもの

roles/docker/tasks/main.yml
---
- name: Add docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
  become: yes

- name: Install basic list of packages
  apt:
    name: "{{ packages }}"
    state: present
    update_cache: yes
  vars:
    packages:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg-agent
      - software-properties-common
  become: yes

- name: Add apt repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
  become: yes

- name: Install Docker packages
  apt:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - docker-ce
      - docker-ce-cli
      - containerd.io
  become: yes

- name: Add user to docker group
  user:
    name: "{{ ansible_env.USER }}"
    groups: docker
    append: yes
  become: yes

- name: Ensure docker service is enabled
  systemd:
    name: docker
    state: started
    enabled: yes
  become: yes

- name: Install docker-compose
  get_url:
    url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ ansible_system }}-{{ ansible_architecture }}"
    dest: /usr/local/bin/docker-compose
    mode: +x
  become: yes

一部解説

GPG Key

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

公式では上記コマンドですが、apt-key モジュールのパラメータで url が指定できるので簡潔にできます

- name: Add docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
  become: yes

Docker 用リポジトリ

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

lsb_release -cs は bionic や xenial などの ubuntu のリリースバージョンを取得するコマンドですが
Ansible では gather_facts を有効にしていれば ansible_distribution_release で取得できます

- name: Add apt repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
  become: yes

docker-compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

公式は curl なので get_url モジュールを使います
uname -s/uname -m はそれぞれ ansible_system/ansible_architecture で取得しています
また、ダウンロード先のパーミッションも同時に指定できるので1回のタスクで済みます

- name: Install docker-compose
  get_url:
    url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ ansible_system }}-{{ ansible_architecture }}"
    dest: /usr/local/bin/docker-compose
    mode: +x
  become: yes
21
17
1

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
21
17