KatPadi's Point

Docker For WordPress Dev Environment

Note: I am not a Docker expert.

These are just my personal notes for my local WP development environment using WordPress x Docker x Fig.

Here’s the final and improved version of this which will take the pain out of setting up. Just run start.sh and then your WP instance is good to go:

https://github.com/katpadi/wp-starter

So why did I try this?

I used to run Vagrant then, I took an arrow to the knee.

Anyway…

In my setup, and in most of my development cases, I only put the wp-content in repo with the exception of wp-content/uploads.

So the file system setup of the root directory is like this:

  • fig.yml
  • Dockerfile
  • wp-content/

Here are the things that I did.

Install Docker, m*therfucker.

sudo sh -c "echo deb https://get.docker.io/ubuntu docker main\ > /etc/apt/sources.list.d/docker.list" 
sudo apt-get update 
sudo apt-get install lxc-docker

Docker repository key

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

Install Fig

So a great person once told me that we need Fig for us to run the Docker easier. In short, a fig.yml basically just defines a Docker build & environment. Easier to read, easier to type.

sudo su 
curl -L https://github.com/orchardup/fig/releases/download/0.5.1/linux > /usr/local/bin/fig 
chmod +x /usr/local/bin/fig

Check to verify the installation

docker --version
fig --version

The fig.yml file

The fig.yml is different for every instance. If your setup is, say you put the whole WP in the repo, you’ll probably end up having a different one.

mysql:
  image: orchardup/mysql
  environment:
    MYSQL_DATABASE: wordpress
    MYSQL_ROOT_PASSWORD:

wordpress:
  image: wordpress
  links:
    - mysql:mysql
  ports:
    - "8888:80"
  volumes:
    - "wp-content:/var/www/html/wp-content"

Before running it

# Add the docker group if it doesn't exist
$ sudo groupadd docker

# Add the connected user "${USER}" to the docker group. You can specify which user. 
# LOG IN AND LOG OUT AFTER FOR THIS TO TAKE EFFECT
$ sudo gpasswd -a ${USER} docker

# Restart the Docker daemon.
$ sudo service docker restart

Actually running it

export MYSQL_ROOT_PASSWORD=whateverpasswordyouwant
fig up -d mysql
fig up --no-deps -d wordpress

When you encounter a problem just “fig logs” and then check. Or, “fig rm” and repeat the “Actually running it” steps.

Updated:

The Dockerfile

As you can see, there aren’t any Dockerfile in this tutorial because I didn’t modify anything in my images. Say, I want to modify something in the Apache, PHP or WordPress configs, I will just put in some shit in the Dockefile like this:

FROM wordpress

# php5 apache2 stuff
RUN echo "upload_max_filesize = 10M;" >> /etc/php5/apache2/php.ini

There are so many examples in GitHub 😛

Useful tool to enter a Docker container

docker run –rm -v /usr/local/bin:/target jpetazzo/nsenter

The above snippet downloads and installs https://github.com/jpetazzo/nsenter— a useful tool for you to “enter” a Docker container so that you can edit things like Apache and PHP stuff. An awesome example of its command simplicity is:

docker-enter <Name of WP container>

**Just “fig ps” to know the list of containers.

That’s it! ¯_(ツ)_/¯

Now I am happily developing in my local machine in a Dockerized environment. (saying “Dockerize” is such a poser thing. Sorry! haha)

PS. I did this because my Vagrant was broken. I even risked all my files to upgrade my distro for it to be fixed but it became weird completely I decided to ditch it.
I was also probably just brainwashed by a certain Docker fanboy a.k.a. @themarksteve.

Leave a Reply

Your email address will not be published. Required fields are marked *