Skip to content
jgreat edited this page Sep 20, 2014 · 7 revisions

Installing docker:

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: boot2docker install + virtualbox additions support: jgreat.me - instructions

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 \
docker.jgreat.me:5000/mysql

Show running docker containers

docker ps
CONTAINER ID        IMAGE                                COMMAND                CREATED             STATUS              PORTS                    NAMES
bd7d7e9c92c7        registry:0.8.1                       "/bin/sh -c 'exec do   28 hours ago        Up 16 seconds       0.0.0.0:5000->5000/tcp   registry                      
7307657d671a        docker.jgreat.me:5000/mysql:latest   "/entrypoint.sh mysq   28 hours ago        Up 32 seconds       3306/tcp                 wp-mysql   

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
CONTAINER ID        IMAGE                                COMMAND                CREATED             STATUS                      PORTS                    NAMES
bd7d7e9c92c7        registry:0.8.1                       "/bin/sh -c 'exec do   28 hours ago        Up 2 minutes                0.0.0.0:5000->5000/tcp   registry
7307657d671a        docker.jgreat.me:5000/mysql:latest   "/entrypoint.sh mysq   28 hours ago        Exited (0) 59 seconds ago                            wp-mysql

Start a saved container

docker start wp-mysql

Show logs for running docker

docker logs -f wp-mysql

Grab ubuntu:14.04

docker pull docker.jgreat.me:5000/ubuntu:14.04

Run docker linked to mysql to show vars

docker run -it --rm \
--link wp-mysql:mysql \
docker.jgreat.me:5000/ubuntu /bin/bash

root@1c7a42f39ab6:/# env
MYSQL_ENV_MYSQL_DATABASE=wp
HOSTNAME=1c7a42f39ab6
MYSQL_ENV_MYSQL_ROOT_PASSWORD=rootpw
TERM=xterm
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.44:3306
MYSQL_ENV_MYSQL_USER=wpuser
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
MYSQL_ENV_MYSQL_PASSWORD=wppass
MYSQL_ENV_MYSQL_VERSION=5.6.20
SHLVL=1
HOME=/
MYSQL_NAME=/agitated_shockley/mysql
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_PORT_3306_TCP_ADDR=172.17.0.44
MYSQL_ENV_MYSQL_MAJOR=5.6
MYSQL_PORT=tcp://172.17.0.44:3306
_=/usr/bin/env

Lets setup wordpress

Download and extract wordpress:

mkdir -p ~/docker/wp
cd ~/docker/wp
wget http://docker.jgreat.me:8080/wordpress-3.9.2.zip
unzip ./wordpress-3.9.2.zip

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'));

define('AUTH_KEY',         'k9>Y`y?DaH)3;,o.x@fVR{.r~B$.UiaNC]:ok`I6]>8uhM?`M8_twl{(8,nJyo4:');
define('SECURE_AUTH_KEY',  '5xS&;v?^8{x<erjd_/xL#(1h|@V#|vt}f|vuV~qez+8Cw4UoL&TYPt[j! #l8LA5');
define('LOGGED_IN_KEY',    'oBN-AAgENvKhG/-YcYVFCL09}k61?71Z[j+240@#`~A(AX#Bx-1,|Q>I;Fp4>MbN');
define('NONCE_KEY',        'QI.=>Kz~ R>*0OexK]Dhb;-*0.y4]9s?Pd(@;RC(i0(+R=o3@EPr-~x+0S02%Y.B');
define('AUTH_SALT',        'Q_tpXS&Qj/?:e|3|Dx~gc+A4Vl!>5L9IYBte|>a62|y$fI^ze/zPhM t>=/8{h{B');
define('SECURE_AUTH_SALT', '|BYvFtQUJ.EI,+$1t[,#cpXqi:Nr`bqoCYm|@o[0_<rF.4N}l-`A:[N@+rCb;>J|');
define('LOGGED_IN_SALT',   '+J#i+l4RBfg`B[9VMa^[<O{_qyyP{yqo;{{4VIoZ/Hq7DcN-z_B6:}+ow1jM]A%N');
define('NONCE_SALT',       'rQ*7}Nmrku[O_ #IOBY)1xkAOg&A+o+J!9mpy nj6MeDfqd2(m+d5$kk7</>f3u-');

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 [email protected]
  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 docker.jgreat.me:5000/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 Vagrant ip and show wp

Development with docker

Use --volume to mount your working directory on the host in the docker container

docker run -it --rm \
--link wp-mysql:mysql \
--publish 80:80 \
--volume ~/docker/wp:/var/www/wp \
wp

Running in stage or prod with environmental variables and a tag

Tag the version of wp (latest tag)

docker tag wp docker.jgreat.me:5000/wp

Tag with version:

docker tag docker.jgreat.me:5000/wp docker.jgreat.me:5000/wp:1.0.5

Push the tag to the registry

docker push docker.jgreat.me:5000/wp

On your production/testing host:

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 \
docker.jgreat.me:5000/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 docker.jgreat.me:5000/mysql
docker images 
REPOSITORY                     TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.jgreat.me:5000/wp       1.0.5               b11f325ababc        28 hours ago        334.5 MB
docker.jgreat.me:5000/wp       latest              b11f325ababc        28 hours ago        334.5 MB
wp                             latest              b11f325ababc        28 hours ago        334.5 MB
docker.jgreat.me:5000/ubuntu   14.04               c4ff7513909d        2 weeks ago         213 MB
docker.jgreat.me:5000/ubuntu   latest              c4ff7513909d        2 weeks ago         213 MB
docker images | grep none | awk '{print $3}'| xargs docker rmi
Deleted: a9f968d6c5e283176ddf7cdc41398612c0013dae6d6293e61ece13912985d6df
Deleted: 00f534926ee33d9c3b46157de9de081a6f0ebf9fafabb679d5f4fa0ea4c7cb46
...
Deleted: 8ed955b5caa1af67a1d08a88e19c1c9b72967a800ed6cc62285f2a97ee4c292d
Deleted: 70a06ea1c45a067981ace6f8c58aaabe6c10c55c5152039bdd7070574bdd42e6

Clone this wiki locally