Docker compose Install Wordpress using Ansible
Published: 2022-03-13 12:27:39

**Create ansible role**

```
roles
├── files
│   └── docker-compose.yml
│   
├── vars
│    └── main.yml
├── dockerwordpressplaybook.yml
└── readme.md
```
**Add docker-compose.yml in files directory**

```
version: '3'

services:
  # Database
  db:
    container_name: mysql_container
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    container_name: phpmyadmin_container
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    container_name: wordpress_container
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:
```
**Add main.yml in vars directory**

```
---
admin_user: dev
docker_required_packages:
  - "apt-transport-https"
  - "ca-certificates"
  - "curl"
  - "gnupg-agent"
  - "software-properties-common"
  - "python3-pip"
  - "python3-setuptools"
docker_gpg_url: https://download.docker.com/linux/ubuntu/gpg
docker_repo: deb https://download.docker.com/linux/ubuntu focal stable
docker_packges:
  - "docker-ce"
  - "docker-ce-cli"
  - "containerd.io"
docker_compose_url: https://github.com/docker/compose/releases/download/1.28.2/docker-compose-Linux-x86_64
```
**Add dockerwordpressplaybook.yml**

```
---
- hosts: docker
  vars_files:
    - vars/main.yml

  tasks:
  - name: Install aptitude using apt
    apt: name=aptitude state=latest update_cache=yes force_apt_get=yes

  - name: Install required system packages for Docker
    apt: name={{ docker_required_packages }} state=latest update_cache=yes

  - name: Add Docker GPG key
    apt_key:
      url: "{{ docker_gpg_url }}"
      state: present

  - name: Add Docker repository
    apt_repository:
      repo: "{{ docker_repo }}"
      state: present

  - name: Install Docker
    apt: name={{ docker_packges }} state=latest update_cache=yes

  - name: Install Python Docker module
    pip:
      name: docker

  - name: Add adminstrator to docker group
    user:
      name: "{{ admin_user }}"
      groups: docker
      append: yes
      createhome: yes

  - name: Install Docker Compose
    get_url:
      url: "{{ docker_compose_url }}"
      dest: /usr/local/bin/docker-compose
      mode: u+x,g+x,o+x

  - name: Set up docker-compose file
    template:
      src: "files/docker-compose.yml"
      dest: "/home/{{ admin_user }}/wordpress/"

  - name: Set permission for docker daemon
    file:
      path: /var/run/docker.sock
      mode: '0770'

  - name: docker compose up
    shell:
      chdir: "/home/{{ admin_user }}/wordpress/"
      cmd: docker-compose up -d
```
Use the below commands to see images,containers,volumes

```
docker images
docker ps -a
docker volume list
```
To shutdown the container use the command shown below
```
docker-compose down 
```
Since we use below command in docker-compose.yml file you get the access to edit the wordpress files.If we remove the containers volume will not removed.
```
volumes: ['./:/var/www/html']
```
For example if we try to add media in wordpress, the default size would be 2 MB.we can change the size by editing .htaccess file.

```
# BEGIN WordPress
php_value memory_limit 100M
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
# END WordPress
```
To remove containers along with volumes use the command below

```
docker-compose down --volumes
```