Skip to content
Aditya Bhalsod
← DevOps
DevOpsBeginner

Named Volumes and Bind Mounts in a Local WordPress Stack

A beginner look at wordpress-inside-docker, showing why the database sits in a named volume, the site files in a bind mount, and which command deletes which.

  • #docker
  • #docker-compose
  • #wordpress
  • #volumes
Contents
  1. Two stores for one site
  2. Which command deletes what
  3. How WordPress finds the database
  4. Before you copy this
  5. When this fits

wordpress-inside-docker runs WordPress, MySQL 8.0 and phpMyAdmin from one Compose file. It keeps state in two different ways at once, and that difference decides what you lose when you tidy up. This post is about that.

You start it with docker compose up -d, as the readme says. WordPress then answers on host port 8080 and phpMyAdmin on 8181. The readme sends you to the WordPress setup wizard on first visit, and everything you do there ends up in one of the two stores below.

Two stores for one site

YAML
  mysql:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql

  wordpress:
    image: wordpress:latest
    volumes:
      - ./wordpress:/var/www/html

volumes:
  mysql_data:

The database uses a named volume. It is declared under the top-level volumes: key, and Docker decides where it lives on disk. WordPress uses a bind mount: the folder ./wordpress next to the Compose file is mapped onto /var/www/html. The readme says that folder holds your site files and is created automatically.

In practice, themes, plugins and uploads appear in a folder you can open in an editor, while the raw database files stay out of the way in Docker's own storage.

Which command deletes what

ActionDatabaseSite files
docker compose downkeptkept
docker compose down -vdeletedkept
Delete the wordpress folderkeptdeleted

The readme describes docker compose down -v as removing all data, including the database. It removes the named volume only. A bind mount is an ordinary folder on your machine, and Compose leaves it alone.

So after down -v and another up, you get an empty database sitting next to your old themes, plugins and uploads.

All three services also set restart: always. Docker brings them back whenever its own daemon starts, so a plain docker stop does not make the stack stay down for good. Use docker compose down when you want the containers gone.

How WordPress finds the database

YAML
  mysql:
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress

These are the same three values written twice: once to create the account, once to use it. Nothing in the file links them. Change one side and WordPress can no longer log in.

WORDPRESS_DB_HOST is the service name plus a port. How a service name becomes a host name is covered in How phpMyAdmin Finds MySQL Inside a Docker Compose Network. MYSQL_ROOT_PASSWORD is not part of that pair. WordPress never uses it. You would use it for the root login in phpMyAdmin.

Before you copy this

  • Tags. wordpress:latest takes whichever version latest points to at pull time. mysql:8.0 is pinned to a series, and phpmyadmin/phpmyadmin has no tag at all.
  • Passwords. The values are root and wordpress, in plain text. The readme itself says the setup is for development and that credentials should change before production.
  • Ports. 8080 and 8181 are mapped without a host address, so both listen on all interfaces. phpMyAdmin also sets PMA_ARBITRARY: 1, which lets its login form target any host.
  • Name clashes. The file sets container_name: mysql_container and container_name: phpmyadmin_container, and publishes 8181. The docker-mysql-phpadmin file uses the same names and the same port, so the two stacks cannot run together.
  • Start order. depends_on waits for the MySQL container to start, not for MySQL to accept connections. The file defines no health check, so the first page load straight after up can reach a database that is still starting.

When this fits

The split suits one developer on one machine who wants to edit theme or plugin files directly while treating the database as disposable.

If you never plan to touch the files by hand, I would move /var/www/html into a named volume as well. Then nothing depends on a folder beside the Compose file, and one down -v resets everything.

Found this useful? Share it with your team.

Keep reading

More from DevOps and beyond