23

Quickstart: Compose and WordPress proposes the following docker-compose.yml

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - dbdata:/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
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    dbdata:

For persisting database data, a volume is created:

  • The docker volume db_data persists any updates made by Wordpress to the database.

but nothing is mentioned about the wordpress container...

Questions:

  1. should I follow the same approach and create volumes for the wordpress container, in order to persist the data that are going to be added (by posts, uploads, themes)?
  2. If yes, which paths / directories should I point to?
tgogos
  • 16,343
  • 14
  • 77
  • 108

1 Answers1

34

Maybe I've found something...

volumes:
   - wp-content:/var/www/html/wp-content

According to this article:

...wp-content contains all user-supplied content. Basically anything you can upload to your site ends up here. That doesn’t include anything you write, mind you. Those things are stored in the WordPress database.

However, as long as you have both the database and your wp-content folder, you can always get your site back, even if everything else was lost.

This is also applied here: Setting up WordPress with Docker


To try it out:

version: '3.3'

services:
  db:
   image: mysql:5.7
   volumes:
     - dbdata:/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:
     - wp-content:/var/www/html/wp-content
   ports:
     - "8000:80"
   restart: always
   environment:
     WORDPRESS_DB_HOST: db:3306
     WORDPRESS_DB_USER: wordpress
     WORDPRESS_DB_PASSWORD: wordpress

volumes:
  dbdata:
  wp-content:
Community
  • 1
  • 1
tgogos
  • 16,343
  • 14
  • 77
  • 108
  • 2
    you also need to add it to volumes block at the bottom. – chovy Oct 18 '19 at 06:01
  • Thanks @chovy, I added the relative `yaml`. So, for those who want to use [named volumes](https://docs.docker.com/compose/compose-file/#volume-configuration-reference) they have to add it to the "top-level" `volumes` key too. – tgogos Oct 18 '19 at 08:05
  • Now I wonder how to solve this using Docker swarm and some CDN, like aws s3 or digital ocean spaces. – holms Jun 09 '20 at 23:16
  • 1
    @chovy Why is it necessary to add to volumes block? – caliph Jan 06 '21 at 19:05