As promised, here is part 2 of my docker & wordpress posts. Here is how you could develop wordpress themes using docker.
I’m going to quickly run through how to use docker to do wordpress theme development and not loose any changes when you stop docker of switch off your computer.
In order to make sure data persists even when your docker container is no longer running, you need to set up a volume.
You can read the official docker docs here: https://docs.docker.com/engine/admin/volumes/volumes/
I tweaked the docker-compose file below to map the container’s “wp-content” folder to your current working directory you have your docker-compose.yml file.
version: '3' services: db: image: mysql:5.7 volumes: - ./db_data:/var/lib/mysql restart: always env_file: - ./.env wordpress: depends_on: - db image: wordpress:latest volumes: - ./wp-content:/var/www/html/wp-content ports: - "8000:80" restart: always env_file: - .env volumes: db_data: wp-content:
You can view your wordpress site in your browser by going to:
1 | http://localhost:8000 |
or if you need to use an ip check your docker-machine ip by running:
1 | $ docker-machine ip |
See the previous post for how to set up the .env file.
You should see two directories listed in the same directory you have your docker-compose.yml file in.
1 2 | db_data wp-content |
This is not 100% ideal. For simplicity I’ve set up these folders in the same directory. I will explain in more detail why at the end of the post.
Creating your wordpress theme with docker volume
Now that you have a persistent volume set up you can start to tweak the theme.
You will find the themes in the ./wp-content/themes folder.
In the admin (go to http://localhost:8000/wp-admin, or http://yourmachineip:8000/wp-admin) you will see listed the themes in theĀ Appearance – Themes section.
You can easily delete themes you don’t want, and refresh and see they will no longer be listed in the Themes section in the admin.
You can add new themes and make changes to existing themes and just refresh and you will see the changes in the browser.
A better way to handle volumes:
So I mentioned above, you would not want your volumes necessarily within the same folder as your container files that you have now containerized using docker.
For one, you will want to make your final work into an image with a tag and run the image on a production ready server.
Docker-compose is for development, and should not be your final go live strategy.
Your volumes you should also either point to another container or a safe secure place on your computer / server.
If we’re thinking big picture, and thinking about the deployment part. You will have your docker-compose files separate from your projects, but maybe for simplicity sake you decide to keep them with your project files, that’s fine, but make sure you consciously think about why you want them there, what are the benefits of that structure (I’m just hypothetically asking)?
You should always have your volumes in a safe place, perhaps a dedicated server space that has recovery tactics in place like regular backups, mirrored, clustered. There are many ways to tackle secure, fail safe voluming. Make sure if you are planning a project that is for a client project that will go into the world, that you have planned ahead the entire deployment ecosystem.
A badly managed volume becomes a single point of failure, and one of the main 101s of cloud infrastructure and proper devops thinking should be to remove as many single points of failure as possible.
For the purpose of this blog post though, I’ve kept all of that out. My aim is to allow you to test the concept of wordpres and voluming with as little effort as possible.
I will create a follow up post on how to create a docker image of your code, push your tweaked code into an image, and then you can run from a fully packaged image rather than from a folder.
You’re now dabbling in the realm of containers, so you should be thinking in “image” and moving away from the thought process of “I have x amount of files to push to a server”, but like I say, I’ll touch on that in a follow up post.
Enjoy š May you experience lots of success in your containerizing.
Just a note on some struggles I had:
In order to test the volumes for this post, I delete the volumes a lot and restarted my computer. I shut down the containers and started them. I really tried to break the volumes. For the most part the volumes persisted well, I had to purposely delete the volume and all the files in order to stop it from persisting.
While I did all that my environment started to lag and I noticed the wordpress container sometimes started up before the database container, and then if I tried to load the wordpress site it did not show up. Since the wordpress container ran before the database was properly configured, I had to rerun the wordpress container to get it to connect with the database container again.