skip to Main Content

I’m following the docs of mariadb. It says that the db should be created if it find a .sql in /docker-entrypoint-initdb.d.
I’m working on a Ubuntu Server in a Oracle Virtual BOX VM.
My docker-compose.yml looks like this:

version: "3.9"

services:
  db:
    image: mariadb:10
    container_name: mariadb
    ports:
      - 3306:3306
    environment:
      - MYSQL_USER=user
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_PASSWORD=password
      - MARIADB_DATABASE=database // tried with MYSQL_DATABASE and without this line

    volumes:
      - "db_data:/var/lib/mysql"
      - ".database/initdb/dump.sql:/docker-entrypoint-initdb.d/initdb.sql"
    # networks:
    #   - network

volumes:
  db_data:

My initdb.sql looks like this (the one that should work in the end looks different but out of simplicity I reduced it to the max and could not even this simple one working):

CREATE DATABASE NEWDB;

I honestly don’t know where to look or what to do now because everywhere I looked for a possible solution I found that this is the bare minimum example that should work.

I tried to restarted docker, deleted all containers, images and volumes, modified the initdb.sql into:

CREATE USER user WITH PASSWORD 'password';
CREATE DATABASE IF NOT EXISTS database;
GRANT ALL PRIVILEGES ON DATABASE database TO user;

but the database is not initialized when I docker compose up.
I looked up the container and the initdb.sql was there.

EDIT: It somehow worked, when I docker compose up with MARIADB_DATABASE=database but the script initdb.sql still doesn’t work and it’s the most important thing because it set’s up the whole database.
(NOTE: On top of that I want to set up another PHP-container that runs a PHP-script in order to collect data that is being stored in the above MariaDB-container. The MariaDB is connected with a website that calls data from the container)

2

Answers


  1. Chosen as BEST ANSWER

    I came up with a solution. I used a base image of laravel (installed the laravel project with <curl -s "https://laravel.build/project-name?with=mariadb" | sudo bash> and modified it a little bit. So here's the docker-compose.yml:

    # For more information: https://laravel.com/docs/sail
    version: '3'
    services:
        laravel.test:
            build:
                context: ./vendor/laravel/sail/runtimes/8.1
                dockerfile: Dockerfile
                args:
                    WWWGROUP: '${WWWGROUP}'
            image: sail-8.1/app
            extra_hosts:
                - 'host.docker.internal:host-gateway'
            ports:
                - '${APP_PORT:-80}:80'
                - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
            environment:
                WWWUSER: '${WWWUSER}'
                LARAVEL_SAIL: 1
                XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
                XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            volumes:
                - '.:/var/www/html'
            networks:
                - sail
            depends_on:
                - mariadb
        mariadb:
            image: 'mariadb:10'
            container_name: 'mariadb-10'
            ports:
                - '${FORWARD_DB_PORT:-3306}:3306'
            environment:
                MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
                MYSQL_ROOT_HOST: "%"
                MYSQL_DATABASE: '${DB_DATABASE}'
                MYSQL_USER: '${DB_USERNAME}'
                MYSQL_PASSWORD: '${DB_PASSWORD}'
                MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
            volumes:
                - 'sail-mariadb:/var/lib/mysql'
                - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
            networks:
                - sail
            healthcheck:
                test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
                retries: 3
                timeout: 5s
    networks:
        sail:
            driver: bridge
    volumes:
        sail-mariadb:
            driver: local
    
    

    Here you can see that the "10-create-testing-database.sh" is executed on startup. I tested this container and it created a database, so I just had to modify it a little bit and now the container creates a database and tables on container startup. Here's the "10-create-testing-database.sh":

    #!/usr/bin/env bash
    mysql --user=root --password="$MYSQL_ROOT_PASSWORD" <<-EOSQL
        CREATE DATABASE IF NOT EXISTS database_name;
        GRANT ALL PRIVILEGES ON `testing%`.* TO '$MYSQL_USER'@'%';
        
        USE database_name;
    
        CREATE TABLE IF NOT EXISTS table_name(  
        table_entries ...
        );
    EOSQL
    

    I still don't know why my initial setup did not work. The only difference I see is that this working file is a .sh and the not working one is a .sql (this does not make sence to me but it what it is).

    Dockerfile:

    FROM ubuntu:22.04
    
    LABEL maintainer="Taylor Otwell"
    
    ARG WWWGROUP
    ARG NODE_VERSION=16
    ARG POSTGRES_VERSION=14
    
    WORKDIR /var/www/html
    
    ENV DEBIAN_FRONTEND noninteractive
    ENV TZ=UTC
    
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    RUN apt-get update 
        && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 
        && mkdir -p ~/.gnupg 
        && chmod 600 ~/.gnupg 
        && echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf 
        && echo "keyserver hkp://keyserver.ubuntu.com:80" >> ~/.gnupg/dirmngr.conf 
        && gpg --recv-key 0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c 
        && gpg --export 0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c > /usr/share/keyrings/ppa_ondrej_php.gpg 
        && echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list 
        && apt-get update 
        && apt-get install -y php8.1-cli php8.1-dev 
           php8.1-pgsql php8.1-sqlite3 php8.1-gd 
           php8.1-curl 
           php8.1-imap php8.1-mysql php8.1-mbstring 
           php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap 
           php8.1-intl php8.1-readline 
           php8.1-ldap 
           php8.1-msgpack php8.1-igbinary php8.1-redis php8.1-swoole 
           php8.1-memcached php8.1-pcov php8.1-xdebug 
        && php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer 
        && curl -sLS https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - 
        && apt-get install -y nodejs 
        && npm install -g npm 
        && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null 
        && echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list 
        && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null 
        && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list 
        && apt-get update 
        && apt-get install -y yarn 
        && apt-get install -y mysql-client 
        && apt-get install -y postgresql-client-$POSTGRES_VERSION 
        && apt-get -y autoremove 
        && apt-get clean 
        && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
    
    RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.1
    
    RUN groupadd --force -g $WWWGROUP sail
    RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail
    
    COPY start-container /usr/local/bin/start-container
    COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini
    RUN chmod +x /usr/local/bin/start-container
    
    EXPOSE 8000
    
    ENTRYPOINT ["start-container"]
    
    

  2. Well I’m using the following stack and it works fine for me.

    php-apache:

    This is an Apache server that runs all my php scripts. You can place your scripts in ./src directory and it will automatically be mounted to DocumentRoot directory of the Apache server.

    db:

    This the latest docker container of MariaDb

    adminer:

    This is the lite-weight database browser which I use for creating and altering my databases. You can just visit localhost:8081 and then enter the following credentials. It becomes simpler to manage the databases this way.

    username: root
    password: example

    version: '3.8'
    services:
      php-apache:
        container_name: php-apache
        build:
          context: .
          dockerfile: Dockerfile
        image: php:8.0-apache
        volumes:
          - ./src:/var/www/html/
        ports:
          - 8080:80
    
      db:
        image: mariadb
        restart: always
        environment:
          MARIADB_ROOT_PASSWORD: example
    
      adminer:
        image: adminer
        restart: always
        ports:
          - 8081:8080
    
    Dockerfile:

    This is a simple docker container which is extended from the base php:8.0-apache image, with mysql extensions installed in it for PDO support.

    FROM php:8.0-apache
    RUN docker-php-ext-install pdo_mysql
    RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
    RUN apt-get update && apt-get upgrade -y
    
    P.S:

    Here you’ll have to create all your databases manually via GUI of Adminer. But if you prefer SQL queries via initdb.sql then be my guest. I’ve just provided this configuration as a suggestion.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search