Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to build a mariaDB base in a Dockerfile

I am currently blocked, I want to build a data base when I build a Dockerfile. But I got an error and i don't understand why. Thanks for your help.

Here my Dockerfile

FROM mariadb:latest

WORKDIR /sql-files

ADD sql/ /sql-files/

ENV MYSQL_ROOT_PASSWORD test123
ENV MYSQL_DATABASE testDB
ENV MYSQL_USER toto
ENV MYSQL_PASSWORD test123

RUN apt-get update && apt-get -y install vim && chmod +x insertDB.sh && sh insertDB.sh

EXPOSE 3306

Here my insertDB.sh

#!/bin/sh

/usr/bin/mysqld_safe --skip-grant-tables &
sleep 5
mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD $MYSQL_DATABASE < test.sql

and finaly my test.sql script

CREATE TABLE IF NOT EXISTS test (
  id int NOT NULL AUTO_INCREMENT,
  name varchar(32) NOT NULL,
  PRIMARY KEY (id)
);

INSERT INTO test (name) VALUES
('Toto'),
('Jack'),
('Titi');

So when i execute my docker file with the command docker build -t maria . I get this error:

Processing triggers for libc-bin (2.19-18+deb8u10) ...
171112 18:12:53 mysqld_safe Logging to syslog.
171112 18:12:54 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
ERROR 1049 (42000): Unknown database 'testDB'
The command '/bin/sh -c apt-get update && apt-get -y install vim && chmod +x insertDB.sh && sh insertDB.sh' returned a non-zero code: 1

What am i missed ?

like image 293
Lovebug Avatar asked Sep 18 '25 12:09

Lovebug


2 Answers

Using mariadb image from dockerhub it's simpler than what you tried.

As explained in "Initializing a fresh instance section":

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.

So it's enough you add just test.sql script to /docker-entrypoint-initdb.d

Then your Dockerfile must run mysqld as command.

So, I would leave only test.sql in local sql subdirectory and I would use this Dockerfile:

FROM mariadb:latest

ADD sql/ /docker-entrypoint-initdb.d

ENV MYSQL_ROOT_PASSWORD test123
ENV MYSQL_DATABASE testDB
ENV MYSQL_USER toto
ENV MYSQL_PASSWORD test123

RUN apt-get update && apt-get -y install vim

EXPOSE 3306

CMD ["mysqld"]

Then build your image

docker build -t maria .

and run the container (may be useful to map port)

docker run --name mariadb -ti -d -p 3306:3306 maria

It creates testDB and user toto, as you correctly defined using the environment variables in Dockerfiles (remind that you can override them as -e parameters in the docker run).

like image 157
gile Avatar answered Sep 21 '25 01:09

gile


You are missing two things:

  1. You have to create the database using CREATE DATABASE testDB command.
  2. You have to create the user using CREATE USER 'toto' command.

You have to do this before you run test.sql script. The docker image you are using comes completely empty - it does not know about the database or user you are trying to use there.

If you do the above steps and still get permission denied, you should also include a GRANT statement which will give toto the necessary permissions on the database testDB. Check MariaDB documentation for details.

like image 37
jurez Avatar answered Sep 21 '25 02:09

jurez