Curious about how to work with docker and wordpress? Here is a quick walk through on how to use docker to run wordpress and mysql using docker compose.
In the next post I’ll show how you can can volume the themes and edit themes in your development process. And in a follow up post I will show how to run your own created wordpress image in kubernetes and in minikube. 🙂
You can get a fairly generic yml file from the official docker website:
https://docs.docker.com/compose/wordpress/
I took the configurations and made a few small tweaks:
docker-compose.yml
version: '3' services: db: image: mysql:5.7 volumes: - ./db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: mysql_root_password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress env_file: ./.env volumes: db_data:
The above configuration will set up wordpress and mysql with the variable values in the “environment” sections. If will map wordpress from the interal port 80 to external port 8000. It also creates a volume mount from the internal location of “/var/lib/mysql” to the external location of “./db_data”. This will help with persistent data storage (you won’t loose your data if your container stops).
By “internal” I mean within the container’s environment and by “external” I mean the environment running the docker container, in my case this would be my mac.
Then run the docker compose script:
1 | $ docker-compose up -d |
Check the docker images are running:
1 | $ docker ps |
You can then view the wordpress install by checking your docker ip and then pointing to the external port (in this case it will be 8000).
Get your docker’s ip:
1 | $ docker-machine ip |
The first time you run this, you will be asked to fill in some details about your wordpress blog. The next time you run it, it should all be prepopulated (provided you haven’t deleted your “db_data” folder).
You can also test that the data is persistent by delete the docker containers and images and then downloading the images again and running the docker-compose script. If everything starts up the same way you left it, then your persistent data is working.
I made improvements to the script by pulling out the environment variables into a .env file. This will help make the containers for customizable. I’ve commented out the environment variables in the docker-compose file so you can see what they were.
docker-compose.yaml (v2)
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 ports: - "8000:80" restart: always env_file: ./.env volumes: db_data:
Your .env file:
MYSQL_ROOT_PASSWORD=mysql_root_password MYSQL_DATABASE=wordpress MYSQL_USER=wordpress MYSQL_PASSWORD=wordpress WORDPRESS_DB_HOST=db:3306 WORDPRESS_DB_USER=wordpress WORDPRESS_DB_PASSWORD=wordpress
To test
Check if your .env variables imported correctly by running the docker exec command:
1 | docker exec -it CONTANER-ID bash |
Then inside the container you can run:
1 | echo $WORDPRESS_DB_USER |
If that returns just an empty line, you need to just check the env_file settings and the contents of the file.
Check your container id by running:
1 | docker ps |
Some useful scripts
Stopping all containers (this will stop ALL, so use with caution):
1 | $ docker stop $(docker ps -aq) |
Removing all containers (use with caution, this will remove all containers):
1 | $ docker rm $(docker ps -aq) |
Deleting all images (use with caution! this will remove all images):
1 | $ docker rmi $(docker images -q) |
Github
View this code on github:
https://github.com/CariZa/DockerComposeWordpress