简体   繁体   中英

mysql docker container : create database, user and execute commands automatically

I have followed this post create database and table automatically with docker-compose to execute a script at startup of my container. The database is created and a user i defined in the docker-compose is created, my database is not populated like i have set in the sql script.

Can anyone help me?

here is the dockerfile:

FROM phpmyadmin
COPY DockerCreateAllTablesDBwithData.sql /docker-entrypoint-initdb.d/initdb.sql

Here is the docker-compose.yml:

---
version: '3.7'

services:
  db:
    image: mysql:8-debian
    container_name: db
    restart: always
    networks:
      - network_app
    cap_add:
      - SYS_NICE
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=password
      - MYSQL_DATABASE=CantineTest
      - TZ='Europe/Paris'
    volumes:
      - mydatavolume:/var/lib/mysql
  phpmyadmin:
    build:
      context: .
      dockerfile: ./Dockerfile
    container_name: phpmyadmin
    restart: always
    ports:
      - 8080:80
    depends_on:
      - db
#    volumes:
#      - ./data/certbot/conf:/etc/letsencrypt
    networks:
      - network_app
    environment:
      - PMA_HOST=db
      - TZ="Europe/Paris"
#volumes:
#  frontendbuild:
#    name: frontendbuild
networks:
  network_app:
    name: network_app
volumes:
  mydatavolume:

And here is the DockerCreateAllTablesDBwithData.sql:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Bdd :  `CantineTest`
--

create database CantineTest;
use CantineTest;

DROP TABLE IF EXISTS `Alias`;
CREATE TABLE `Alias` (
  `AliasID` smallint(5) UNSIGNED NOT NULL,
  `AliasName` varchar(50) NOT NULL,
  `AliasDescription` varchar(255) DEFAULT NULL,
  `AliasMailingList` mediumtext NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

You have added the DB script to a wrong place. Must be added to the docker-composer where you have MySQL. Add as a volume

volumes:
  - ./mysql-dump:/docker-entrypoint-initdb.d 

Create a mysql-dump folder where you have docker-compose and add your SQL script to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM