Skip to content

Instantly share code, notes, and snippets.

@bcremer
Created December 12, 2018 20:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcremer/9ea822259214fa1161e15bc2203a20d9 to your computer and use it in GitHub Desktop.
Save bcremer/9ea822259214fa1161e15bc2203a20d9 to your computer and use it in GitHub Desktop.
Multi-Stage Dockerfile to prepopulated MySQL Image
## See: https://serverfault.com/a/915845/17736
FROM percona/percona-server:5.7 as builder
USER root
RUN mkdir -p /initialized-db && chown -R mysql:mysql /initialized-db
# That file does the DB initialization but also runs mysql daemon, by removing the last line it will only init
RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/docker-entrypoint.sh"]
# needed for intialization
ENV MYSQL_ROOT_PASSWORD=apidev
USER mysql
COPY *.sql /docker-entrypoint-initdb.d/
# Need to change the datadir to something else that /var/lib/mysql because the parent docker file defines it as a volume.
# https://docs.docker.com/engine/reference/builder/#volume :
# Changing the volume from within the Dockerfile: If any build steps change the data within the volume after
# it has been declared, those changes will be discarded.
RUN ["/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db"]
FROM percona/percona-server:5.7
COPY --from=builder /initialized-db /initialized-db
USER root
RUN chown -R mysql:mysql /initialized-db
USER mysql
CMD ["--datadir", "/initialized-db"]
@brittanynavin
Copy link

In Line 16 why are you putting the sql file into the /docker-entrypoint-initdb.d/ directory and not /initialized-db (which you later set as your data dir in stage #2)?

@brittanynavin
Copy link

Disregard, Line 16 actually takes the .sql and places it in /docker-entrypoint-initdb.d/ which is actually used to initalize the DB when the /docker-entrypoint.sh script is executed.

Thanks for this example, it helped me in my implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment