memyselfandi
memyselfandi

Reputation: 11

Docker-compose initialize DB

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?

Upvotes: 1

Views: 4207

Answers (3)

nguyen.nguyen
nguyen.nguyen

Reputation: 1

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.

Upvotes: 0

emrhzc
emrhzc

Reputation: 1480

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/

Upvotes: 0

Doomhiker
Doomhiker

Reputation: 31

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?

Upvotes: 3

Related Questions