The Second Coolest Thing: A New API for you (v1)

This weekend, I had some free time to work on something I’ve thought about for a while… an API to build and push a configurable WordPress Docker Image.

Right this moment, there are a couple new API endpoints alive on this blog:

  1. https://www.withinboredom.info/wp-json/docker/v1/file
  2. https://www.withinboredom.info/wp-json/docker/v1/build

/file will give you a Dockerfile, while /build will build and push you a new Docker image, configured just for you.

Using them, are simple. That’s the best part.

Simply POST to https://www.withinboredom.info/wp-json/docker/v1/build (you can also hit the /file endpoint to preview the Dockerfile) this JSON to recreate this blog’s configuration:


{
	"username": "withinboredom",
	"password": "***",
	"email": "***",
	"repo": "withinboredom/wordpress",
	"plugins": [
		{
			"type": "wporg",
			"slug": "jetpack",
                        "version": "latest"
		},
		{
			"type": "wporg",
			"slug": "batcache"
		},
		{
			"type": "wporg",
			"slug": "wp-maintenance-mode"
		},
		{
			"type": "wporg",
			"slug": "cookie-notice"
		},
		{
			"type": "wporg",
			"slug": "disqus-comment-system"
		},
		{
			"type": "wporg",
			"slug": "vaultpress"
		},
		{
			"type": "wporg",
			"slug": "wp-code-highlightjs"
		},
		{
			"type": "wporg",
			"slug": "wp-stateless"
		}
	]
}

This will return a JSON response:


{
  "output": [
    "Step 1/11 : FROM withinboredom/scalable-wordpress:4.7.3-apache\n",
    null,
    null,

    "***SNIP***",

    " :: Layer already exists",
    " :: Layer already exists",
    " :: Layer already exists",
    " :: Layer already exists",
    " :: Pushed",
    " :: Pushed",
    " :: Pushed",
    " :: gGC2OhQMfqqu: digest: sha256:08b25a9a9ff7f28f46bde052295259f9f305dbc9d34a31c78b20b00b5fd64e47 size: 7433",
    " :: "
  ],
  "image": "withinboredom/wordpress:gGC2OhQMfqqu"
}

You’re not limited to WordPress.org, you can also give it urls or specify the specific version of a plugin to install. It will build your specific container for you and push it to Docker Hub. (Credentials are not stored, btw).

Handily, the request will block until this is complete (it’s not asynchronous). I did this on purpose, so you’d know when it was done without having to ask. Also, this is “kind of hack weekend” project, so I didn’t go all-out. I only had an hour or two a night.

With the new image and tag it returns to you, you can immediately pull and deploy anywhere.

This will likely be improved upon over time:

  1. Intercept plugin/theme/core installations and upgrades, and build a new image instead of installing a file
  2. Automatic deployment when running in Docker Swarm
  3. Docker Swarm control panel

These are the things that the WordPress platform can easily handle… I’ll be surprised if you don’t see them sooner than later.

Feel free to view the code (or contribute) on GitHub.