2

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
5
  • why not login to the mysql container and import the dump (which is already mounted to the container) using command line ? Commented Feb 10, 2021 at 16:12
  • 2
    I am looking to make this available to a wider team audience. Automating this step would really make it a lot less involved. Commented Feb 10, 2021 at 16:53
  • Update the question with the container logs.Which extension has the sql file ? If you has written the file on windows remember to change the CRLF to LF for linux image. Docs says: Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d Commented Feb 10, 2021 at 17:25
  • Updated, thank you @Max Commented Feb 10, 2021 at 18:03
  • @hewe feel free to copy your answer to the answer section. adding MYSQL_USER and MYSQL_PASSWORD containing your wordpress user credentials helps automation too. Commented Feb 11, 2021 at 4:48

2 Answers 2

3

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.

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

Comments

0

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...

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.