0

i followed the instructions as instructed in the offcial documentation of docker mysql image to init the database as the service starts up , but it seems mysql is ignoring the sql query

version: '3'
services:

 # nginx
  nginx-service:
    image: nginx:stable-alpine
    container_name: nginx-container
    restart: unless-stopped
    ports:
     - "8000:80"
    volumes:
     - ./application:/var/www/project
     - ./docker/nginx:/etc/nginx/conf.d
    depends_on:
      - application-service
      - database-service
    networks:
      - php-application

  # php
 application-service:
   build:
     context: .
     dockerfile: docker/php/Dockerfile
   container_name: application-container
   restart: unless-stopped
   volumes:
     - ./application:/var/www/project
   networks:
     - php-application

# mysql
database-service:
  image: mysql:5.7
  container_name: database-container
  ports:
    - "6306:3306"
  restart: unless-stopped
  environment:
    MYSQL_DATABASE: user
    MYSQL_ROOT_PASSWORD: 1234
    MYSQL_PASSWORD: 1234
    MYSQL_USER: root
  command: --log-bin=/var/lib/mysql/mysql-bin.log --binlog-format=ROW --server-id=1
  volumes:
    - ./docker/db:/docker-entrypoint-initdb.d/:ro
    - ./db-data:/var/lib/mysql:rw
  networks:
    - php-application

networks: php-application:

the sql file contains a query to create a table as you see below:

CREATE TABLE `user` (
`id` INT AUTO_INCREMENT NOT NULL,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(100) NOT NULL,
`created_at` DATETIME DEFAULT NULL,
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

when i log into the container i find the sql file

root@6d6ab4ba11e6:/# ls docker-entrypoint-initdb.d/ init_db.sql root@6d6ab4ba11e6:/#

1 Answers1

0

Did you check other questions? MySQL scripts in docker-entrypoint-initdb are not executed - /var/lib/mysql must be empty if you want SQL script in /docker-entrypoint-initdb.d applied.

JustinC
  • 31
  • 3
  • thanks for your help. i checked the logs of my database container ,and it turn out that it can't create a user root since it already exists by default . so i removed MYSQL_USER: root from docker-compose deleted container images and volumes and started a new service and it worked – user11787469 Jan 22 '21 at 19:52