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?


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

FROM centos:7


RUN echo "[mariadb]" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "name = MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "baseurl =" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgkey=" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgcheck=1" >> /etc/yum.repos.d/MariaDB.repo
RUN rpm --import
RUN yum install -y MariaDB-server MariaDB-client
RUN yum clean all

RUN echo "[mysqld]" > /etc/my.cnf
RUN echo "bind-address=" >> /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:

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