Skip to content

Commit

Permalink
add wordpress docker file, makefile build/push instructions and docke…
Browse files Browse the repository at this point in the history
…r-compose for build/testing #7 (#16)

fix typo and break long CLI command

rename env variables; set default for docker repo

finish work in the docs for the wordpress image

remove readme.md, use a single pattern to replace esc character
  • Loading branch information
alanlima authored Jul 28, 2020
1 parent bc8ab57 commit ac8e44e
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docker/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
WORDPRESS_DB_HOST=
WORDPRESS_DB_USER=
WORDPRESS_DB_PASSWORD=
WORDPRESS_DB_NAME=
DOCKER_REPOSITORY=
AWS_ACCOUNT_ID=
AWS_REGION=
3 changes: 3 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARG WP_VERSION=5.4-php7.2-apache

FROM wordpress:${WP_VERSION}
41 changes: 41 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
include .env

COMMIT_SHA ?= $(shell git rev-parse --short HEAD)
AWS_REGION ?= $(shell cat ~/.aws/config | grep -m 1 region | sed 's/region = //')
AWS_ACCOUNT_ID ?= $(shell aws sts get-caller-identity --query "Account" --output text)

DOCKER_REPOSITORY ?= devops-wordpress
DOCKER_REGISTRY_URL ?= ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com

C_GREEN ?= \e[32m
C_RESET ?= \e[0m

build:
@echo "\n === 🏭 Building Wordpress image\n"
@docker-compose build wordpress
@echo "\n === ✅ Done"
.PHONY:build

push:
@echo "\n === 🔖 Tagging image with sha ${C_GREEN}${COMMIT_SHA}${C_RESET}"
@docker tag devops-wordpress:latest ${DOCKER_REGISTRY_URL}/${DOCKER_REPOSITORY}:${COMMIT_SHA}

@echo "\n === 🚀 Pushing into repository\n"
docker push ${DOCKER_REGISTRY_URL}/${DOCKER_REPOSITORY}:${COMMIT_SHA}
@echo "\n === ✅ Done"
.PHONY:push

ecr-login:
@echo "\n === 🔐 Login to ECR docker registry: ${C_GREEN}${DOCKER_REGISTRY_URL}${C_RESET}\n"

@aws ecr get-login-password \
--region ${AWS_REGION} \
| docker login \
--username AWS \
--password-stdin ${DOCKER_REGISTRY_URL}

@echo "\n === ✅ Done"
.PHONY:ecr_login

deploy: ecr-login build push
.PHONY: deploy
32 changes: 32 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.8"
services:

wordpress:
image: ${DOCKER_REPOSITORY}
build:
context: .
ports:
- 8080:80
volumes:
- wordpress:/var/www/html
environment:
WORDPRESS_DB_HOST: ${WORDPRESS_DB_HOST}
WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
WORDPRESS_DB_NAME: ${WORDPRESS_DB_NAME}
depends_on:
- db

db:
image: mysql:5.7
environment:
MYSQL_DATABASE: ${WORDPRESS_DB_NAME}
MYSQL_USER: ${WORDPRESS_DB_USER}
MYSQL_PASSWORD: ${WORDPRESS_DB_PASSWORD}
MYSQL_ROOT_PASSWORD: root#secret
volumes:
- db:/var/lib/mysql

volumes:
wordpress:
db:
117 changes: 117 additions & 0 deletions docs/docker-wordpress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!-- Start Document Outline -->

* [Introduction](#introduction)
* [Environment variables](#environment-variables)
* [.env file template:](#env-file-template)
* [make instructions](#make-instructions)
* [build](#build)
* [push](#push)
* [ecr-login](#ecr-login)
* [deploy](#deploy)
* [Local Tests](#local-tests)

<!-- End Document Outline -->

# Introduction

This document describes the environment variables used for the wordpress image and some make instruction which can be used for automation.

- [Wordpress Dockerfile](../docker/Dockerfile)
- [Makefile](../docker/Makefile)

The base wordpress image used for this project will be [5.4-php7.2-apache](https://hub.docker.com/_/wordpress/).

# Environment variables

| Key | Description | Mandatory |
|-----------------------|------------------------------------------|------------------------------------------|
| **WORDPRESS_DB_HOST** | The database host name for the Wordpress to connect.<br>***Example:*** `db` | **true** |
| **WORDPRESS_DB_USER** | The database user name for the Wordpress to connect.<br>***Example:*** `wordpress-user` | **true** |
| **WORDPRESS_DB_PASSWORD** | The database user password for the Wordpress to connect.<br>***Example:*** `wordpress-secret` | **true** |
| **WORDPRESS_DB_NAME** | The database name for the Wordpress to connect.<br>***Example:*** `wordpress-db` | **true** |
| **DOCKER_REPOSITORY** | The name of the repository which the image is build and it will be pushed.<br>***Example:*** `devops-wordpress` | **false**<br>***default:*** `devops-wordpress` |
| **AWS_ACCOUNT_ID** | The AWS Account Id used to login to ECR and to tag and push the image.<br>***Example:*** `027422937416` | **false**<br>***default:*** it will read from the aws default profile if not specified |
| **AWS_REGION** | The AWS region where the ECR is located.<br>***Example:*** `ap-southeast-2` | **false**<br>***default:*** it will read from the aws default profile it not specified |

## **.env** file template:

The `.env` should be located in the same directory of `docker-compose` and `Makefile`. Alternatively, those variables can be set in the local machine.

```bash
WORDPRESS_DB_HOST=
WORDPRESS_DB_USER=
WORDPRESS_DB_PASSWORD=
WORDPRESS_DB_NAME=
DOCKER_REPOSITORY=
AWS_ACCOUNT_ID=
AWS_REGION=
```

# `make` instructions

Make instruction will be using environment variables to execute the commands.

Environment variables will be evaluated in the following order, with later source taking precedence over earlier ones:

- **System environment**, set using `export` command
- **`.env` file** located in the same folder that `Makefile`
- At the time that the command is executed. Ex. `AWS_ACCOUNT_ID=027422937416 make ecr-login`

### build

Build a new docker image:

```bash
make build
```

Required variables:

- **DOCKER_REPOSITORY**

### push

Tag the latest image with the commit sha and push it to the container registry (ECR).

```bash
make push
```

*The commit sha will be generated at run time using the instruction: `git rev-parse --short HEAD`*

Required variables:

- **AWS_ACCOUNT_ID**
- **AWS_REGION**
- **DOCKER_REPOSITORY**

### ecr-login

Using the default aws profile, authenticate with the ECR registry and add the authentication token to `docker login`.

```bash
make ecr-login
```

Required variables:

- **AWS_ACCOUNT_ID**
- **AWS_REGION**
- **DOCKER_REPOSITORY**

## deploy

Execute the following commands in order:

1. [**build**](#build)
2. [**push**](#push)
3. [**ecr-login**](#ecr-login)

# Local Tests

The [docker-compose](../docker/docker-compose.yaml) file, which is used to build the docker image, can be used for local tests running the command:

```bash
# to initiate the local server and run the wordpress and mysql containers
$ docker-compose up
```

0 comments on commit ac8e44e

Please sign in to comment.