Theo
Theo

Reputation: 2832

How to pre-configure and prefill official MySQL docker container?

I want to build a new MySQL image based on the official MySQL docker container image. I want to reduce the number of parameters I need to add when running the image. (eg. -e MYSQL_USER, -e MYSQL_DATABASE and even -e MYSQL_ROOT_PASSWORD='rootsecret') that already includes my settings for the global variables and my Create Database SQL file in the docker-entrypoint-initdb.d folder.

How do I add all my settings and create a new image to simply docker run mysql:config1 docker run mysql:config2 and so on?

Upvotes: 0

Views: 1344

Answers (3)

Theo
Theo

Reputation: 2832

The following has to be written into the Dockerfile:

FROM mysql:latest
LABEL Name=mylabel Version=0.0.1
COPY path/to/sh/sql/sql.gz/files /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD='rootpassword' 

As stated in the documentation on the official docker website:

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order.

Upvotes: 1

Bogdan Stoica
Bogdan Stoica

Reputation: 4529

You could build your own mysql docker image using a docker file, configure username, password and everything else you might need, build that image, upload it to the docker hub and then when you launch a new docker container you just use the previously built container.

An example of a Docker file to build an ubuntu image with a mysql server inside would be something like bellow (save it to a file called Dockerfile):

FROM ubuntu:latest
    RUN apt-get update \
    && apt-get install -y apt-utils \                                           
    && { \
        echo debconf debconf/frontend select Noninteractive; \
        echo mysql-community-server mysql-community-server/data-dir \
            select ''; \
        echo mysql-community-server mysql-community-server/root-pass \
            password 'Desired-Password'; \
        echo mysql-community-server mysql-community-server/re-root-pass \
            password 'Desired-Password'; \
        echo mysql-community-server mysql-community-server/remove-test-db \
            select true; \
    } | debconf-set-selections \
    && apt-get install -y mysql-server mysql-client

Then build your mysql docker container like this (you have to be in the folder where the Docker file was/is saved):

docker build my-ubuntu-mysql-docker

Then you have to push it to the docker hub and then you can use it to start a new docker container like this:

docker run -d -p 2222:22 -p 3306:3306 --name my-ubuntu-mysql-docker ...

Where 2222 is local ssh port mapped to ssh port 22 of the docker container and 3306 is local mysql port mapped to the mysql port of the docker container.

I hope this helps!

Upvotes: 1

Sergiu
Sergiu

Reputation: 3195

What you you would want to do is to modify Mysql's image entry point and please note that you do not need to pass all the parameters, most of them are optional

Upvotes: 0

Related Questions