1

I have a docker-compose file

version: '3.7'

services:        
  db:
      image: mysql:latest
      container_name: mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 12345
      volumes:
        - ./docker/db:/docker-entrypoint-initdb.d
      ports:
        - "3306:3306"

and a MySQL script to initialize the DB.

CREATE DATABASE `aaa`;
USE `aaa`;


CREATE TABLE `building` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `start_date` date DEFAULT NULL,
  `end_date` date DEFAULT NULL,
  `open` tinyint(4) DEFAULT NULL,
  `address_type` varchar(30) DEFAULT NULL,
  `address_name` varchar(45) DEFAULT NULL,
  `address_number` varchar(45) DEFAULT NULL,
  `cap` char(5) DEFAULT NULL,
  `city` varchar(45) DEFAULT NULL,
  `province` varchar(45) DEFAULT NULL,
  `state` varchar(45) DEFAULT NULL,
  `req_amount` float DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Actually Docker ignore the file INIT_DB.SQL as stated:

mysql | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/DB_INIT.SQL

I want to initialize the DB with that file. I've also tried that: tomcat + mysql + war using docker-compose.yml.

Where's the mistake?

5
  • docker-entrypoint.sh recognizes *.sh, *.sql and *.sql.gz files as init files. *.SQL is not among them (Linux is case-sensitive), so it is ignored. Commented Aug 22, 2019 at 12:55
  • github.com/docker-library/mysql/blob/… Commented Aug 22, 2019 at 12:56
  • Ok, it makes sense. I renamed it but it still give the same error :( Commented Aug 22, 2019 at 12:59
  • you need to delete the containers and images and recreate them Commented Aug 22, 2019 at 13:21
  • I did it, i've used docker-compose rm -vf Commented Aug 22, 2019 at 13:31

3 Answers 3

3

This works for me:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - "3306:3306"

    environment:
      TZ: "America/Halifax"
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: formulary

    volumes:
    - ./mysql-init.sql:/docker-entrypoint-initdb.d/mysql-init.sql
    - ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf

The only difference between what you put and what I put is that I put the source file/target file (not just the directories). As well, are you sure ./docker/db contains your file?

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

2 Comments

my ./docker/db/INIT_DB.sql exists, adding the complete path to INIT_DB.sql it says: mysql | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/INIT_DB.sql mysql | ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
Initializing a container results in ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device. when a mounted file is missing from the host due to docker's behavior to create an empty directory for any missing mounted file. -> it sounds like you have a typo in your filename, or it's not readable. Or if you ran this docker-compose file before the sql file existed and created an empty dir of that name.
0

got the same errror with the comments below the first answer (..is a directory). what actually worked for me;

postgres:
  volumes:
    - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d/

Comments

0

First, you create Dockerfile for mysql to create docker-entrypoint-initdb.d/ folder:

FROM mysql:latest

RUN mkdir -p /docker-entrypoint-initdb.d/

Secondly, update docker-compose:

version: '3.7'

services:        
  db:
      build:
         context: .
         dockerfile: ./db/Dockerfile
      container_name: mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 12345
      volumes:
        - ./docker/db:/docker-entrypoint-initdb.d/
      ports:
        - "3306:3306"

It works for me.

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.