LaBrie LaBrie - 10 months ago 83
MySQL Question

Can't MariaDB inside docker container

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
...

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download