I am incredibly new to Docker, WordPress and Containers. I need to build a draft for a website and I want to use Docker containers as, for some reason, my VM does not work (cannot configure the web interface). I am following (copy-pasting, literally) this tutorial https://www.hostafrica.com/blog/wordpress/install-wordpress-docker-windows-linux/ on ubuntu.
When I get to create a yml file, I just copy pasted the info that is on the tutorial but I keep receiving error messages when I use the command ‘docker-compose up -d’. The error is ‘services must be a mapping’. I have googled and realised that this could be due to the lack of indentation in the text file, but I really don’t have any skills at all to even understand how to change the indentation. I gave it a try and the error message changes slightly, so I almost sure this could be the cause of it? I was wondering if anyone would be able to help me with this. I have copied the content of the yml file below
version: '3.3'
services:
database:
image: mysql:5.7.37
volumes:
- database_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: myrootpassword
MYSQL_DATABASE: wp_db
MYSQL_USER: admin
MYSQL_PASSWORD: admin
wordpress:
depends_on:
- database
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: admin
WORDPRESS_DB_NAME: wp_db
volumes:
database_data: {}
2
Answers
Here’s the correct docker compose for you…
That tutorial link you provided is not a good one. It’s really not that complicated when using Docker Desktop app and Docker Compose.
For starters, their
docker-compose.yml
examples are not even indented correctly, most decent IDE’s will show this as invalid YAML/YML formatting.YML’s should be formatted with 2 space indents for each block (just like @Ziyad Ansari answer). Anyway, not sure formatting is your problem, though might be.
Also MySQL is pretty old, use MariaDB instead, which is forked from MySQL image, and is a more up to date image of MySQL.
Anyway, to get WordPress running locally using Docker Compose is easy. First just install Docker Desktop.
Now don’t over complicate database credentials, as these are only used locally, and makes it easier to remember for every local WordPress project you create. Makes is super easy when connecting to local database in your IDE.
Also, you don’t need to define
volumes
to use in the database image block.Here is the bare minimum you need in a
docker-compose.yml
file to fire up a local WordPress environment…Just by running
docker-compose up
, ordocker-compose up -d
which is more handy to use as-d
detaches the docker log and means you can rundocker-compose down
straight after in the same terminal window.But be aware, as soon as you
docker-compose down
, this will destroy everything, when using the config above.This is where Docker Compose gets awesome, you can map container volumes to your project directory so you can make database, themes, plugins, uploads etc persistent.
Mapping volume data so it is persistent, means everytime you
docker-compose down
your running project environment, next time your rundocker-compose up -d
on your local Docker WordPress project, all your environment data will be exactly as you left off.Here is an persistent
docker-compose.yml
example showing you how to map volumes from your containers to make data persistent…This is just the tip of the ice berg, see more extended examples of
docker-compose.yml
configuration files, enabling you to handle everything necessary when working locally with WordPress using Docker.https://github.com/joshmoto/docker-wordpress-meetup-demo
The
README.md
is pretty rough, this was just to help Docker beginners from an online demo I did. If you got any questions let me know.Happy Dockering!