5

I want to create Docker container and import 3 databases into it. I've tried with the following code:

version: '3.3'
services:
  web:
    build:
      context: ./php56
      dockerfile: Dockerfile
    container_name: php56
    depends_on:
      - db
    volumes:
      - ../www:/var/www/html/
    ports:
      - 8000:80
  db:
    container_name: mysql
    image: mysql:5.7.21
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: dkum
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    entrypoint:
      sh -c "
        echo 'CREATE DATABASE IF NOT EXISTS dkum_joomla; CREATE DATABASE IF NOT EXISTS dkum_test;' > /docker-entrypoint-initdb.d/init.sql;
        /usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
      "
    volumes:
      - ../sql/baze/dkum.sql:/docker-entrypoint-initdb.d/dkum.sql
      - ../sql/baze/dkum_joomla.sql:/docker-entrypoint-initdb.d/dkum_joomla.sql
      - ../sql/baze/dkum_test.sql:/docker-entrypoint-initdb.d/dkum_test.sql
    ports:
      - 6033:3306

This code creates only 1 database (dkum) filled with data from the dkum.sql volume. If I remove dkum_joomla.sql and dkum_test.sql volumes then it creates 3 databases (dkum, dkum_joomla and dkum_test) with only dkum database filled with data.

Here are my SQL files. I will later expand them.

dkum.sql

CREATE TABLE dkum_table (
    DkumID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

dkum_joomla.sql

CREATE TABLE dkum_joomla_table (
    DkumJoomlaID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

dkum_test.sql

CREATE TABLE dkum_test_table (
    DkumTestID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);
6
  • Sounds like there is a problem (e.g. a syntax error) in the dkum_joomla.sql file. Commented Jul 28, 2019 at 10:08
  • I checked the file and it's fine. Commented Jul 28, 2019 at 11:17
  • keep in mind the dump will be done in order so make sure there is no thing depended Commented Jul 28, 2019 at 11:38
  • There is nothing depended in the dumps. It's very simple sql code, which generates 1 independent table in each database for now. I will later expand the databases. Commented Jul 28, 2019 at 14:43
  • Have a look at this link I don't know if this will help? github.com/abagayev/docker-bootstrap-collection/tree/master/… Commented Jul 28, 2019 at 17:14

1 Answer 1

6

Instead of creating additional databases in docker-compose file, just create them in SQL files instead:

version: '3.3'
services:
  web:
    build:
      context: ./php56
      dockerfile: Dockerfile
    container_name: php56
    depends_on:
      - db
    volumes:
      - ../www:/var/www/html/
    ports:
      - 8000:80
  db:
    container_name: mysql
    image: mysql:5.7.21
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: dkum
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    volumes:
      - ../sql/baze/dkum.sql:/docker-entrypoint-initdb.d/dkum.sql
      - ../sql/baze/dkum_joomla.sql:/docker-entrypoint-initdb.d/dkum_joomla.sql
      - ../sql/baze/dkum_test.sql:/docker-entrypoint-initdb.d/dkum_test.sql
    ports:
      - 6033:3306

dkum.sql

CREATE TABLE dkum_table (
    DkumID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

dkum_joomla.sql

CREATE DATABASE IF NOT EXISTS dkum_joomla;
USE dkum_joomla;

CREATE TABLE dkum_joomla_table (
    DkumJoomlaID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

dkum_test.sql

CREATE DATABASE IF NOT EXISTS dkum_test;
USE dkum_test;

CREATE TABLE dkum_test_table (
    DkumTestID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.