1

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;
1
  • 2
    The sql init script must be copied/mounted in your db container, not in the phpmyadmin one. Note that for any initialization to take place, you need to start from scratch (i.e. empty the db data volume which can easily be done in your case with docker-compose down -v). Commented Jul 9, 2022 at 12:48

1 Answer 1

2

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.

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

1 Comment

Note also that both your script and the container entrypoint create the database CantineTest. Note the entrypoint uses the MYSQL_DATABASE as default so both create database and use CantineTest can be omitted from your DB script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.