skip to Main Content

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


  1. Here’s the correct docker compose for you…

    version: '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:
    
    • Ensured that the volumes section under the wordpress service has content. If you don’t need to define a specific volume for the WordPress service, you can remove the volumes section entirely.
    Login or Signup to reply.
  2. 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…

    version: '3.8'
    
    services:
    
      # here is our mariadb container
      # https://hub.docker.com/_/mariadb
      db:
        image: mariadb:11.0.2
        restart: always
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress
          MYSQL_USER: wordpress
          MYSQL_PASSWORD: wordpress
    
      # here is our wordpress container
      # https://hub.docker.com/_/wordpress
      wordpress:
        image: wordpress:latest
        restart: always
        ports:
          - "80:80"
        depends_on:
          - db
        environment:
    
          # our docker wp config settings
          WORDPRESS_DB_HOST: db:3306
          WORDPRESS_DB_USER: wordpress
          WORDPRESS_DB_PASSWORD: wordpress
          WORDPRESS_DB_NAME: wordpress
    
          # our docker wp config extra php defines
          WORDPRESS_CONFIG_EXTRA: |
    
            /** disable wp core auto update */
            define('WP_AUTO_UPDATE_CORE', false);
    

    Just by running docker-compose up, or docker-compose up -d which is more handy to use as -d detaches the docker log and means you can run docker-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 run docker-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…

    version: '3.8'
    
    services:
    
      # here is our mariadb container
      # https://hub.docker.com/_/mariadb
      db:
        image: mariadb:11.0.2
        restart: always
        ports:
          - "3306:3306"
        volumes:
          # our persistent volume database mapping
          - ./db:/var/lib/mysql
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress
          MYSQL_USER: wordpress
          MYSQL_PASSWORD: wordpress
    
      # here is our wordpress container
      # https://hub.docker.com/_/wordpress
      wordpress:
        image: wordpress:latest
        restart: always
        ports:
          - "80:80"
        depends_on:
          - db
        volumes:
          # our persistent volume sub directories mapping
          - ./themes:/var/www/html/wp-content/themes
          - ./mu-plugins:/var/www/html/wp-content/mu-plugins
          - ./plugins:/var/www/html/wp-content/plugins
          - ./uploads:/var/www/html/wp-content/uploads
        environment:
    
          # our docker wp config settings
          WORDPRESS_DB_HOST: db:3306
          WORDPRESS_DB_USER: wordpress
          WORDPRESS_DB_PASSWORD: wordpress
          WORDPRESS_DB_NAME: wordpress
    
          # our docker wp config extra php defines
          WORDPRESS_CONFIG_EXTRA: |
    
            /** disable wp core auto update */
            define('WP_AUTO_UPDATE_CORE', false);
    

    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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search