Reputation: 179
I spent most morning trying to figure out not only how to copy an initial SQL dump into the container, but also how to auto-import (execute) the dump into the DB. I have read countless other posts, none of which seem to work. I have the following docker compose file:
version: '3.8'
services:
db:
image: mariadb:10.5.8
restart: always
container_name: database
environment:
MYSQL_ROOT_PASSWORD: default
volumes:
- db-data:/var/lib/mysql
- ./db-init:/docker-entrypoint-initdb.d
volumes:
db-data:
The SQL dump is found in the db-init folder. I got the docker-entrypoint-initdb.d
from the official docs on DockerHub.
After docker-compose up
, the SQL is correctly copied into the docker-entrypoint-initdb.d
but is never ran against the DB, aka the dump is never imported and the DB remains empty.
I have tried placing the volumes
directive around in the docker compose file as this was suggested in another post. From what I've read, the SQL dump should be imported automatically when mounting the volume.
Is there no way to accomplish this via the docker-compose.yml only?
Edit: Switching the version to 2.x did not work
EDIT2: Container logs:
2021-02-10 17:53:09+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/wordpress.sql
ERROR 1046 (3D000) at line 10: No database selected
Upvotes: 2
Views: 2178
Reputation: 39
I don't have enough points to comment the answer above, so will have to add it like this (hope to save someone from unnecessary t-shooting): newer versions of MariaDB also support environment variables named like MARIADB_
in addition to the ones named MYSQL_
. Docs URL above also points to the page that proposed both naming conventions as valid. I completely got used to the newer naming convention and forgot that in the current project I'm actually using older MariaDB version (that doesn't support MARIADB_
naming style). So I was just wondering for a whole hour why MariaDB acts as if I haven't defined the database env var to use when loading the dump...
Upvotes: 0
Reputation: 1032
From your logs, a quick google search pointed to this post. Adding MYSQL_DATABASE
to the environment should solve the issue and the .sql should then be imported correctly on startup.
Final docker-compose should look like this:
services:
db:
image: mariadb:10.5.8
restart: always
container_name: database
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: default
volumes:
- db-data:/var/lib/mysql
- ./db-init:/docker-entrypoint-initdb.d/
Maybe not worded as strongly as it should be, but the docs mention this: SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
Upvotes: 3