While containers and jails have existed for decades, Docker brought them to the mainstream with better tooling and improved developer experience. Containers are a great way to ensure your applications run in the same environment no matter where they are hosted.
Another benefit of containers is that you can upgrade the operating system quite easily on the container images and use blue-green deployments to roll it in production. Contrast this with standalone servers, where changing the OS is a lot more involved process.
Docker lets you do cool things with WordPress by merely adding a few lines to the service definitions.
Here are some examples:
- Elasticsearch cluster on demand – You can use docker images and get a production-ready elasticsearch cluster in minutes.
- Instant headless environment – You can run a node.js server in a separate container and use themes like Frontity for a blazing fast WordPress site.
- Secure SSH/SFTP access – You can launch a separate container to provide SSH/SFTP access to the team and keep server configuration private.
- Ephemeral staging sites – You can launch staging sites in separate containers and discard them after use. This comes in handy for visual regression testing.
When it comes to using Docker in production for hosting WordPress sites, here are some considerations:
- Because of the ephemeral nature of containers, all media needs to be offloaded to s3 or a CDN. This way, when you launch more containers, they still have access to the same uploads.
- DB needs to be separated from the web server. Because you’ll need more than 1 container as you scale out, including DB and web server in the same container won’t work. Ideally, you should use a hosted DB service like aurora or RDS.
- Depending on how frequently your code changes, you’ll need to decide whether to bundle your wp code as part of the container or copy it on shared storage and mount it inside containers. One benefit of mounting shared storage is that you can update plugins, themes, and core using wp-admin, and those changes reflect everywhere instantly. Bundling code with containers means you need to build and launch new containers every time you update a plugin. While this sounds like a hassle, one upside to this is that if something breaks on the site, you can restart the container and get the code back to working condition.
Apart from this, I recommend building your own containers using Dockerfiles to avoid reliance on mystery images that are a potential security risk. If you plan to bundle the code with your docker images, consider using a private container registry to avoid making your code public.