Docker: how to use SQL file in Directory
Just add a volume mapping to map a local folder to the /docker-entrypoint-initdb.d container folder, for example : ./init-db:/docker-entrypoint-initdb.d
. This file will be loaded on the first container startup.
Considering the docker-compose.yml bellow :
- drop your sql files into
/path-to-sql-files-on-your-host
host folder) - run
docker-compose down -v
to destroy containers and volumes - run
docker-compose up
to recreate them.
-
version: '3'services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql - /path-to-sql-files-on-your-host:/docker-entrypoint-initdb.d 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: wordpressvolumes: db_data:
According to the MySQL-Docker documentation, you have a couple of options.
After the 'db' docker is running, simply connect to it using the mysql client, and import your .sql dump. Read the section 'Connect to MySQL from the MySQL command line client' If you where not using docker, you would restore this backup like this
mysql fooDB < fooDB_dump.sql
It will be similar with docker commands.Docs, "Initializing a fresh instance", says "...it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d" That looks more like what you want. Just copy your .sql file into that location within the docker image and then it will automatically parse it.