Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: MySQL refuses host IP unless I restart it manually

Tags:

docker

mysql

Here is my Dockerfile:

FROM debian:jessie-backports

RUN apt-get update --yes && apt-get upgrade --yes
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ 
  git \
  curl \
  build-essential\
  libssl-dev\
  ca-certificates\
  mysql-server\
  redis-server\
  elasticsearch

USER root

ENV HOME /root

# MYSQL SETUP
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/#bind-address = 0.0.0.0/" /etc/mysql/my.cnf
RUN sed -i -e"s/^#max_connections\s*=\s*100/max_connections = 200/" /etc/mysql/my.cnf
RUN echo "\n[mysqld]\nskip-grant-tables\n" >> /etc/mysql/my.cnf
VOLUME ["/var/lib/mysql", "/var/log/mysql"]

EXPOSE 3306

# REDIS SETUP
RUN echo "daemonize yes\nbind 0.0.0.0" >> /etc/redis/redis-serve.conf

RUN sed 's/^daemonize no/daemonize yes/' -i /etc/redis/redis.conf \
 && sed 's/^bind 127.0.0.1/bind 0.0.0.0/' -i /etc/redis/redis.conf \
 && sed 's/^# unixsocket /unixsocket /' -i /etc/redis/redis.conf \
 && sed 's/^# unixsocketperm 755/unixsocketperm 777/' -i /etc/redis/redis.conf \
 && sed '/^logfile/d' -i /etc/redis/redis.conf

VOLUME ["/var/lib/redis", "/var/log/redis"]

EXPOSE 6379 6380

# ELASTICSEARCH SETUP

RUN sed 's/^#START_DAEMON=true/START_DAEMON=true/' -i /etc/default/elasticsearch

VOLUME ["/opt/elasticsearch/data", "/opt/elasticsearch/logs"]

EXPOSE 9200 9300

ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod 777 /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

The entrypoint:

#!/bin/bash

/etc/init.d/mysql start
/etc/init.d/redis-server start
/etc/init.d/elasticsearch start
/bin/bash

And the command I'm running to start the container:

docker run -i -t -p 3306:3306 -p 6379:6379 -p 9200:9200 -p 9300:9300 assethost

I want to connect to the MySQL server running in the container from the host, but this is what happens when I try to connect:

ERROR 1130 (HY000): Host '192.168.99.1' is not allowed to connect to this MySQL server

Both Redis and Elasticsearch expose themselves just fine. MySQL will only begin accepting external hosts if I restart it manually from the Bash shell that comes up from the entrypoint when I run the container. I have to run /etc/init.d/mysql restart. Only then, can I successfully connect from the host using the MySQL client.

Note that I am using docker-machine, so I have to give the MySQL client the host IP in order to connect.

How can I be able to connect from the host to the MySQL instance running in the container without having to manually restart MySQL?

Docker version 1.12.1, build 23cf638

like image 835
Ravenstine Avatar asked Mar 14 '17 03:03

Ravenstine


People also ask

How do I make my Docker container talk to localhost?

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.

Is MySQL Docker persistent?

The MySQL Docker image is configured to store all its data in the /var/lib/mysql directory. Mounting a volume to this directory will enable persistent data storage that outlives any single container instance. Using this command to start your MySQL container will create a new Docker volume called mysql .

How do I start MySQL server in Docker?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

What IP is 172.17 0.1 Docker?

Listen to Connections in the Docker Network The bridge connection docker0 – with IP address 172.17. 0.1 – is created by Docker at installation time. Because the host and all containers are connected to that network, our application only needs to listen to it.


1 Answers

I figured out a way to get the skip-grant-tables option to be applied on start.

Workaround

In docker-entrypoint.sh, I changed /etc/init.d/mysql start to

HOME=/etc/mysql /usr/bin/mysqld_safe > /dev/null 2>&1 &

Other changes

Since there was already a [mysqld] section in my.cnf, I changed the following line in Dockerfile RUN echo "\n[mysqld]\nskip-grant-tables\n" >> /etc/mysql/my.cnf

to

RUN sed -i '/\[mysqld\]/ a skip-grant-tables\nskip-name-resolve' /etc/mysql/my.cnf

I added skip-name-resolve so mysql doesn't attempt to resolve names for local ip addresses.

Caveats

The workaround no longer works when /etc/init.d/mysql restart is executed, but /etc/init.d/mysql stop; /etc/init.d/mysql start preserved the 'skip-grant-tables' option.

like image 73
Mark Mucha Avatar answered Sep 19 '22 23:09

Mark Mucha