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 ?
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
).
You are missing two things:
CREATE DATABASE testDB
command.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With