Ansible&Docker

2019-10-10

let's install Docker and Docker Compose on vagrant virtual machine(guest system) by Ansible Playbook.

outline

we can’t talk server development without Docker recently. in here, it’s difficult to explain all details about Docker. we will introduce Docker by explain it little by little when we need. we will explain how to install Docker and Docker Compose on vagrant by Ansible Playbook in this blog post.

this blog is based on the assumption that you have done three blogs below.

set Docker in Ansible Playbook

below is the directory structure for vagrant virtual machine(guest system) we created until now.

|-- ansible
|    |-- init
|    |    |-- tasks
|    |    |    |-- main.yml
|    |-- playbook.yml
|-- Vagrantfile

we’ll define role about Docker installation and add Ansible Playbook in here. create docker/tasks/main.yml file in ansible folder.

|-- ansible
|    |-- init
|    |    |-- tasks
|    |    |    |-- main.yml
|    |-- docker
|    |    |-- tasks
|    |    |    |-- main.yml
|    |-- playbook.yml
|-- Vagrantfile

modify playbook.yml that is Ansible Playbook’s start point like below.

---
- hosts: localhost
  connection: local
  roles:
    - init
    - docker

add below codes to docker/tasks/main.yml we created.

---
- name: Install docker
  shell: curl https://get.docker.com | sh

- name: Modify privilege
  become: true
  shell: usermod -aG docker $USER

- name: Change privilege of docker
  become: true
  file: dest=/usr/bin/docker mode=+x

- name: python docker / docker-compse module
  pip:
    name:
        - docker
        - docker-compose

let’s see Ansible commands one by one.

- name: Install docker
  shell: curl https://get.docker.com | sh

this command is to install Docker by using Docker install script.

- name: Modify privilege
  become: true
  shell: usermod -aG docker $USER

- name: Change privilege of docker
  become: true
  command: chmod +x /usr/bin/docker

this is to change user and permission of Docker.

- name: python docker / docker-compse module
  pip:
    name:
        - docker
        - docker-compose

this command is to install python docker module and Docker Compose

execute Ansible

execute below Ansible command in virtual machine(guest system) to execute Docker installation role we added in Ansible Playbook above.

vagrant ssh

sudo ansible-playbook /vagrant/ansible/playbook.yml

we executed Ansible Playbook because we already have virtual machine(guest system). if we make new environment, Ansible Playbook is automatically executed because we already added provision shell.

execute below vagrant command in local machine(host system) for testing.

vagrant destroy
vagrant up

check Docker is installed

execute below commands to check Docker is installed well on virtual machine(guest system) by Ansible Playbook.

vagrant ssh

docker --version
docker-compose --version

completed

we saw how to install Docker and Docker Compose by Ansible Playbook. now, we can make development environment via Docker. next blog post, we will introduce how to configure Laravel development environment by Docker and Docker Compose.

Buy me a coffeeBuy me a coffee
Posts