Using Docker image mysql:5.7.21 I want to create a new image based of it that will already have its database initialised. (I want to use this for my acceptation environment).
I know about the /docker-entrypoint-initdb.d and I could do the fallowing:
FROM mysql:5.7.21
COPY ./init /docker-entrypoint-initdb.d
But the problem is that it will only copy the tar/sql files, but it will not execute until the first time it's run. Another disadvantage is also the image size. If you a copying for example 200MB of data inside the container then those files will stay in the image (as a layer).
So I was wondering if there is a combination I haven't thought of with multi-stage or the new --squash flag (that would enable, adding the files, executing the files, removing the files).
In my search for this problem I also found about the --datadir flag. Not sure how this could help.
edit:
So far I have the fallowing:
FROM mysql:5.7.21 as builder
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV MYSQL_DATABASE example
COPY ./init /docker-entrypoint-initdb.d
RUN head -n-2 < /usr/local/bin/docker-entrypoint.sh > /usr/local/bin/docker-entrypoint.sh \
&& docker-entrypoint.sh mysqld \
&& /etc/init.d/mysql start \
&& mysql -uroot -e 'FLUSH TABLES;' \
&& mysql -uroot -e 'show tables;' mysql \
&& mysql -uroot -e 'show tables;' example \
&& /etc/init.d/mysql stop \
# This shows the files!
&& ls -la /var/lib/mysql
# This shows no files?!!
RUN ls -la /var/lib/mysql
FROM mysql:5.7.21
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
ENV MYSQL_DATABASE example
COPY --from=builder /var/lib/mysql /var/lib/mysql
RUN ls -la
RUN ls -la /var/lib/mysql \
&& /etc/init.d/mysql start \
&& mysql -uroot -e 'show tables;' mysql \
&& mysql -uroot -e 'show tables;' example
See the above inline comments... something really strange is happening? So in the same RUN when I perform an ls -la on /var/lib/mysql I can see the files. But in a new layer (new RUN) it's empty :S