Oyabi
Oyabi

Reputation: 914

Mariadb multi stage container with an existing database

I need to create a container with an existing database and operate some actions on it before distribute it. In production, I've got some really big databases:

[root]# ls /home/db-backup/test/prepare/26_06_2020/full/
ibdata1        
ib_logfile0    
ib_logfile1    
ibtmp1         
mysql
performance_schema
my_existing_database1
my_existing_database2
my_existing_database3

I just need to publish a container with my_existing_database1 for my team.

I tried many Dockerfile but I can't find a way do to it and I don't understand why. Here a simplified Dockerfile :

FROM mariadb:latest as builder

ENV MYSQL_ALLOW_EMPTY_PASSWORD yes

# for easier debug, i will remove that in prod
RUN sed -i '/\[mysqld\]/a plugin-load-add = auth_socket.so' /etc/mysql/my.cnf

WORKDIR /initialized-db

COPY ibdata1 .
COPY mysql ./mysql
COPY performance_schema ./performance_schema
COPY my_existing_database1 ./my_existing_database1
COPY db-init.sh /docker-entrypoint-initdb.d/

RUN chown mysql:mysql . \
  && chmod 660 ibdata1 \
  && chmod +x /docker-entrypoint-initdb.d/db-init.sh

RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db", "--aria-log-dir-path", "/initialized-db"]

# No file named test
RUN ls /initialized-db/

FROM mariadb:latest
COPY --from=builder /initialized-db /var/lib/mysql

As you can see, I try to execute my script db-init.sh. Simplified version :

#!/bin/bash
set -e -x

mysql -u root -e "CREATE USER 'test'@'%' IDENTIFIED BY 'test';"
touch /initialized-db/test

Unfortunetly, my script is not executed as the file test is not created. I try to bypass the /usr/local/bin/docker-entrypoint.sh with my own script (copy/paste of the file with some edit) but it's not working either (user and file test not created).

Can you help me on this one please?

Upvotes: 4

Views: 472

Answers (1)

mvorisek
mvorisek

Reputation: 3418

You should preferably mount a directory for the data and distribute it then extra.

If you must distibute it with the container itself, you should be able to edit the config of that database to use a custom data directory and initialize your databases in it with RUN and COPY.

Upvotes: 2

Related Questions