2

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?

montrealist
  • 5,593
  • 12
  • 46
  • 68

1 Answers1

1

Create an additional volume for dumps, e.g. 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.

Damocles
  • 1,287
  • 1
  • 7
  • 9