1

I need to run MariaDB inside existing Docker container.

Building and installation works just fine, but when Docker executes

RUN mysql < init.sql

to load DB schema I get

Can't connect to MySQL server (111 Connection refused)

However when I run the container and execute

docker exec -it silly_allen /bin/bash -c "mysql < init.sql" 

it works just fine.

What might be the problem?

Thanks!

EDIT: Here's part of Dockerfile related to DB.

FROM centos:7

WORKDIR /root

...
RUN echo "[mariadb]" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "name = MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "baseurl = http://yum.mariadb.org/10.1/centos7-amd64" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgcheck=1" >> /etc/yum.repos.d/MariaDB.repo
RUN rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
RUN yum install -y MariaDB-server MariaDB-client
RUN yum clean all

RUN echo "[mysqld]" > /etc/my.cnf
RUN echo "bind-address=0.0.0.0" >> /etc/my.cnf

RUN /etc/init.d/mysql restart

ADD init.sql /root
RUN mysql < /root/init.sql
...
LaBrie
  • 225
  • 1
  • 2
  • 6

3 Answers3

2

According to Docker's best practices, you should be having 1 container per process that you want to run.

Also, there's an official mariadb image which allows you to mount a directory as volume, that could contain SQL dumps. These dumps are auto-imported when the container gets created, so this might prove to be handy.

I'd suggest instead of having one very large dockerfile, you break it up in separate services with docker-compose

If you do however want to keep this the way it is, I'd suggest you move the ADD init.sql ... part to the top, and concatenate the server starting up part and the dump import, because each RUN command is a separate layer with Docker. So you'd need something like what's described in the answer of this StackOverflow question:

RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
  sleep 5 && \
  mysql -u root -e "CREATE DATABASE mydb" && \
  mysql -u root mydb < /root/init.sql

So that the server initializes and the dump gets imported in one layer

Fotis
  • 1,322
  • 16
  • 30
0

From what I can see, you are trying to run mysql < init.sql before starting the database. The error shows that this command requires the database to be running.

To solve this problem, add a startup script into you container containing:

mysqld
mysql < init.sql

And change your Dockerfile CMD to call this script.

yamenk
  • 46,736
  • 10
  • 93
  • 87
0

This way is right:

# cat Dockerfile 
...
ADD init.sql /tmp
ADD initdb.sh /tmp
RUN /tmp/initdb.bash
CMD ["/usr/bin/mysqld_safe --datadir=/var/lib/mysql"]

And the script:

# cat dump/initdb.bash 
#!/bin/bash

set -e
set -x

mysqld_safe --datadir='/var/lib/mysql' --user=root &
until mysqladmin ping >/dev/null 2>&1; do
   sleep 0.2
done

mysql -e 'create database init;' && \
mysql init < /tmp/init.sql && \
echo "Successfully imported" && exit 0
Quarind
  • 244
  • 2
  • 8