Creating Websites with Docker

Any web developer, systems administrator or devops minded individual who is ‘worth his or her salt’ these days is certainty already familiar with Docker. If you aren’t there yet — in spite of the learning curve, which I understand can be particularly steep for people who are generally allergic to virtual terminals and the command line — the upside is so profoundly empowering that committing whatever time is necessary to learning your way around it will virtually always be a worthwhile investment.

Here’s a simple example:  With Docker (CE) and Docker Compose installed on my ‘daily workhorse’ Linux laptop and using a simple text file template (a YAML file) it takes just a few minutes to spin up a completely fresh, fully updated and totally isolated WordPress instance ‘on the fly’ — running on two dedicated containers (WP + DB) with persistent storage:

~/docker $ mkdir wpnew && cd wpnew
~/docker/wpnew $ nano docker-compose.yml

- - - - - 8< - - - - -
# docker-compose.yml
version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - /home/user/docker/wpnew/dbdata:/var/lib/mysql
       - /home/user/docker/wpnew/dbvol:/mnt
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: *******
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: **********

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - /home/user/docker/wpnew/html:/var/www/html
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: **********
- - - - - 8< - - - - -

~/docker/wpnew $ docker-compose up -d

The last command instantiates the containers. Your new WordPress installation setup page is available in your browser at http://localhost/:

Image 1: WordPress with Docker Example by Carl E. Hartung

Just select your language and click 'Continue':

Image 2: WordPress with Docker Example by Carl E. Hartung

Key sections of the container (virtual) filesystems are mounted locally:

  • '/home/user/docker/wpnew/html' is mounted at '/var/www/html' in the web server container.
  • '/home/user/docker/wpnew/dbvol' is mounted at '/mnt' in the database server container.

This means that, in addition to working on the website through the WordPress Dashboard, direct local filesystem access is available for editing the source files, too:

Image 3: WordPress with Docker Example by Carl E. Hartung

Summary

In five minutes you're ready to code and build. The containers are virtual production systems, too, so there's no cross-pollination between your local development environment and the end product. If you make a mistake, it only takes a few minutes to spin down and destroy the containers so you can recreate them, if needed, to start again.

This simple example demonstrates one of the most profound benefits of incorporating Docker technology into your workflow. It's all about efficiency. It eliminates endless hours spent configuring and troubleshooting one's development environment keeping it compatible with production. The less time a developer spends tinkering, the more time they have to spend doing what they love!

Thanks for reading!

Carl