Using WordPress with Docker and Docker-compose is easy and anybody can understand this small tutorial and how it should work.
Everybody who knows Docker loves to use it and not get his head around the problems with versions compatibility. If you know docker and don’t love it, sorry to tell you but I think you haven’t been using docker at it’s full capacity.
How to get started with WordPress and Docker
First of all make sure you have installed Docker. With it Docker-compose also (for some OS it is automatically installed). Once you have Docker installed and running we can create the docker-compose file! We will use docker-compose because it is easier for us to start multiple services than run 2 docker commands for 2 separate containers.
In order to run WordPress with Docker you just need to create a docker-compose.yml file containing the following
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
To comment a little the docker-compose file, we have 2 services, database and the wordpress itself. They know one about the other because of the naming and the the same network they use. Here you should pay attention and check if you have more db running with the same exposed port. If you will have more services with the same port, you will have a hard time finding your issue and experience some weird behaviors (this applies for the ones who use Traefik in order to server multiple DNS from the same ip).
This code snippet is from the Docker official site. But it’s missing some stuff which we will add in the next minute.
If you are in. hurry and you don’t want to upload big files (themes included), then you can Run it. But it’s better to improve your WordPress configuration.
Improve post/upload size
We have to create and uploads.ini and add a volume line for wordpress.
file_uploads = On
upload_max_filesize = 256M
post_max_size = 256M
memory_limit = 256M
Update docker-compose.yml with
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wordpress_data:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
Run it!
Now it’s time to start playing! Run the following command
docker-compose up
Wait for the database to finish initializing and go up to your https://localhost:8000
Friendly reminder
Don’t forget to update the passwords because these are standard and hackers will try these ones 😉