Skip to content
Jason Greathouse edited this page Jul 10, 2015 · 7 revisions

Installing docker:

Ubuntu

cd ~/docker
curl -sSL https://get.docker.io/ubuntu/ | sudo sh

Add your user to docker group:

sudo usermod -G docker -a vagrant

Log out and log back in.

OSX:

jgreat/osx-docker

Windows

Install file: TODO Install vagrant/ubuntu-vm

Starting an pre-made docker

Start mysql

docker run -d --name wp-mysql \
-e MYSQL_DATABASE=wp \
-e MYSQL_USER=wpuser \
-e MYSQL_PASSWORD=wppass \
-e MYSQL_ROOT_PASSWORD=rootpw \
-p 172.17.42.1:3306:3306 \
-v /data/mysql:/var/lib/mysql \
mysql

Show running docker containers

docker ps

Show full details for a container

docker inspect wp-mysql

Stop a running docker container

docker stop wp-mysql

Show stopped/exited containers

docker ps -a

Start a saved container

docker start wp-mysql

Show logs for running docker

docker logs -f wp-mysql

Grab ubuntu:14.04

docker pull ubuntu:14.04

Lets setup wordpress

Download and extract wordpress:

mkdir -p ~/docker/wp
cd ~/docker/wp
wget -O wordpress.tar.gz https://wordpress.org/latest.tar.gz
tar xvzf wordpress.tar.gz

Edit wp-config.php and set database to use environmental variables

cd wordpress
cp wp-config-sample.php wp-config.php
vi wp-config.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', getenv('MYSQL_DB'));

/** MySQL database username */
define('DB_USER', getenv('MYSQL_USER'));

/** MySQL database password */
define('DB_PASSWORD', getenv('MYSQL_PWD'));

/** MySQL hostname */
define('DB_HOST', getenv('MYSQL_HOST'));

Make an apache config that will overwrite the apache default

mkdir -p .docker/apache2/sites-enabled
vi .docker/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>

  ServerAdmin webmaster@jgreat.me
  ServerName jgreat.me

  DocumentRoot /var/www/wp/wordpress
  <Directory /var/www/wp/wordpress>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride All 
     Require all granted
  </Directory>

  ErrorLog "|/bin/cat"
  CustomLog "|/bin/cat" combined
  LogLevel warn

</VirtualHost>

Create an entrypoint script to configure linked database

mkdir -p .docker/bin
vi .docker/bin/entry.sh
#!/bin/bash
# This script is used when linking dockers for development.
# Docker doesn't have a great way to set runtime environment variables, if 
# linking dockers so...
set -e

if [ ! -z "${MYSQL_PORT_3306_TCP_ADDR}" ]; then 
  export MYSQL_HOST=$MYSQL_PORT_3306_TCP_ADDR
fi
if [ ! -z "${MYSQL_ENV_MYSQL_DATABASE}" ]; then 
  export MYSQL_DB=$MYSQL_ENV_MYSQL_DATABASE
fi
if [ ! -z "${MYSQL_ENV_MYSQL_USER}" ]; then 
  export MYSQL_USER=$MYSQL_ENV_MYSQL_USER
fi
if [ ! -z "${MYSQL_ENV_MYSQL_PASSWORD}" ]; then 
  export MYSQL_PWD=$MYSQL_ENV_MYSQL_PASSWORD
fi

# Execute the commands passed to this script
exec "$@"

Make entry.sh executable

chmod +x .docker/bin/entry.sh

Create a script to start the app

vi .docker/bin/start-app.sh
#!/bin/bash
apachectl -DFOREGROUND

Make start-app.sh executable

chmod +x .docker/bin/start-app.sh

Make a Dockerfile

cd ~/docker/wp
vi Dockerfile
#Wordpress Apache Docker Image

FROM ubuntu:14.04
MAINTAINER Jason Greathouse

#install apache, php and mysql client
RUN apt-get update && \
    apt-get install -y php5 libapache2-mod-php5 php5-mysql php5-cli mysql-client 

#clean up apt files
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/*

#add wordpress files
ADD . /var/www/wp

#add apache vhost config (overwrite default)
ADD .docker/apache2/sites-enabled/000-default.conf /etc/apache2/sites-enabled/000-default.conf

#add entrypoint and start up scripts
ADD .docker/bin /usr/local/bin

#Change the working directory to the app root
WORKDIR /var/www/wp

#entrypoint script to set env vars when linking containers for dev
ENTRYPOINT ["/usr/local/bin/entry.sh"]

#Default command to run on start up
#/usr/sbin/apachectl -DFOREGROUND
CMD ["/usr/local/bin/start-app.sh"]

Build the docker container

docker build -t wp .

Run the container to test

docker run -it --rm \
--link wp-mysql:mysql \
--publish 80:80 \
wp

Find your VM IP and point your Web Browser at it.

Varies on OS/VM. vagrant: vagrant ssh ip addr

Running in stage or prod with environmental variables and a tag

Tag the version of wp (latest tag)

docker tag wp jgreat/wp

Tag with version:

docker tag jgreat/wp jgreat/wp:1.0.5

Push the tag to the registry

docker push jgreat/wp

On your production/testing host:

This will live behind a local proxy.

docker run -d \
--publish 127.0.0.1:5000:80 \
-e MYSQL_HOST=mysql.example.com \
-e MYSQL_DB=wp-prd \
-e MYSQL_USER=wp-prd \
-e MYSQL_PWD=t9hutiZygiukngnwB+m0x \
jgreat/wp:1.0.5

Cleaning up

Removing old containers (including associated volumes)

docker stop wp-mysql
docker rm -v wp-mysql

Remove images (all containers using the image must be removed)

docker rmi mysql

Show Images

docker images 
REPOSITORY                     TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
jgreat/wp                      1.0.5               b11f325ababc        28 hours ago        334.5 MB
jgreat/wp                      latest              b11f325ababc        28 hours ago        334.5 MB
wp                             latest              b11f325ababc        28 hours ago        334.5 MB
ubuntu                         14.04               c4ff7513909d        2 weeks ago         213 MB
ubuntu                         latest              c4ff7513909d        2 weeks ago         213 MB

Delete all unused images

docker images | awk '{print $3}'| xargs docker rmi
Deleted: a9f968d6c5e283176ddf7cdc41398612c0013dae6d6293e61ece13912985d6df
Deleted: 00f534926ee33d9c3b46157de9de081a6f0ebf9fafabb679d5f4fa0ea4c7cb46
...
Deleted: 8ed955b5caa1af67a1d08a88e19c1c9b72967a800ed6cc62285f2a97ee4c292d
Deleted: 70a06ea1c45a067981ace6f8c58aaabe6c10c55c5152039bdd7070574bdd42e6