0

I have a docker-compose.yml file like this

version: '2'

services:

  wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_NAME: my_wp
      WORDPRESS_DB_USER: my_user
      WORDPRESS_DB_PASSWORD: my_pass
    volumes:
      - ./src:/var/www/html

  mysql:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: my_pass
    volumes:
      - ./db:/etc/mysql

I can run my containsers using the command docker-compose up and it works fine, but shows the default wordpress site with empty db to setup. What I want is, I have a db dump file my_db.sql which I want to load into mariadb when its initializing. How can I do that? and where do I need to put my my_db.sql file to be picked by mariadb container?

0

2 Answers 2

1

You have 3 options:

  1. Mount your .sql file to /docker-entrypoint-initdb.d/

    version: '2'
    
    services:
    
      wordpress:
        image: wordpress
        ports:
          - 8080:80
        environment:
          WORDPRESS_DB_NAME: my_wp
          WORDPRESS_DB_USER: my_user
          WORDPRESS_DB_PASSWORD: my_pass
        volumes:
          - ./src:/var/www/html
    
      mysql:
       image: mariadb
        environment:
          MYSQL_ROOT_PASSWORD: my_pass
        volumes:
          - ./db:/etc/mysql
          - ./my_db.sql:/docker-entrypoint-initdb.d/my_db.sql
    
  2. Use build directive in your docker-compose file and point to a dockerfile with following content:

    FROM mysql
    COPY my_db.sql /docker-entrypoint-initdb.d
    
  3. Create a dummy container, Mount /my/own/datadir:/var/lib/mysql, then restore the database manually using the docker exec, then you can use the host directory when ever you want that db in a container.

Sign up to request clarification or add additional context in comments.

2 Comments

how exactly I need to update my docker-compose.yml file?
Added example to the answer.
0

Mount the folder with your sql files to /docker-entrypoint-initdb.d on the container in the volumes section of the db definitions. Any .sql (and .sql.gz) files will be automatically run in.

See the Initializing a fresh instance section of the docs for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.