简体   繁体   中英

WordPress on Docker: mount database volume AND import existing database?

I want to bring up a local instance of an existing WordPress install using Docker. How can I:

  1. import the existing database...
  2. AND persist changes between my container restarts?

I can do each separately but cannot get both to work. If I use the db_data volume then I seem to start off with a clean database. If I don't - it imports my database from existing.sql but then I cannot persist my changes between container restarts.

My docker-compose.yml:

version: '3.3'

services:
  db:
    image: mysql:5.7
    volumes:
      - ./db/existing.sql:/docker-entrypoint-initdb.d/existing.sql
      - db_data:/var/lib/mysql # this works for task 2 but not task 1
    restart: always
    environment:
      # credentials here

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    volumes:
      - ./code/:/var/www/html/
    environment:
      # credentials here
volumes:
  db_data: {}  # this works for task 2 but not task 1

Do I need to use --volumes-from flag as described in the docs here ?

Create an additional volume for dumps, eg have a local directory db/dump/ for SQL dumps:

      - ./db/existing.sql:/docker-entrypoint-initdb.d/existing.sql
      - ./db/dump/:/sql                                                   
      - db_data:/var/lib/mysql # this works for task 2 but not task 1

Copy your SQL dump you'd like to import to db/dump/import_this.sql . Next, attach to your MySQL db container like so:

docker exec -it "YOUR_CONTAINER_ID" mysql -uroot -p YOUR_DB_NAME

This will bring up the MySQL command line where you can now source the file:

source /sql/import_this.sql

Alternatively, you could source /docker-entrypoint-initdb.d/existing.sql , but I usually prefer having a directory rather than just one file.

Now you have the imported database persistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM