Kern Kern - 1 month ago 25
MySQL Question

Docker Mysql container root password [mysqld listens port 0]

Today I'm trying to make my Docker environment working !

In this matter I've encountered quite a problem : my MySQL container, extending the MySQL official Docker image, seems to fail to create the root account, despite the setting of the

MYSQL_ROOT_PASSWORD
environment variable in my
docker-compose`.yml
.

I copy here my Docker files :

docker-compose.yml

Most of the environment variables are used in scripts and applications independantly from the MySQL server. Only
MYSQL_ROOT_PASSWORD
deserves interest (perhaps this statement is the cause of my failure to make this work..! ).

mysql:
container_name: my_mysql
build: mysql
environment:
- MYSQL_DATABASES=my-database
- MYSQL_ROOT_PASSWORD=root
- MYSQL_HOST=127.0.0.1
- MYSQL_PORT=33306
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_MY_DATABASE=my-database
ports:
- "33306:3306"
volumes:
- "./volumes/mysql:/var/lib/mysql"


mysql/Dockerfile

The dos2unix command is meant to convert Windows line endings to Unix.

The custom entrypoint is named differently to avoid overidding the default mysql entrypoint script.

FROM mysql:5.7
MAINTAINER Wonderful Dev <wonderful.dev@wonderful-company.com>

RUN apt-get update && apt-get install -y dos2unix

COPY conf.d/custom.cnf /etc/mysql/conf.d/
COPY docker-entrypoint-initdb.d/databases.sh /docker-entrypoint-initdb.d/databases.sh
COPY my-entrypoint.sh /my-entrypoint.sh

RUN dos2unix /docker-entrypoint-initdb.d/databases.sh && dos2unix /my-entrypoint.sh && dos2unix /etc/mysql/conf.d/custom.cnf && apt-get --purge remove -y dos2unix && rm -rf /var/lib/apt/lists/*

RUN chmod a+x /docker-entrypoint-initdb.d/databases.sh && chown root:root /docker-entrypoint-initdb.d/databases.sh
RUN chmod a+x /my-entrypoint.sh && chown root:root /my-entrypoint.sh

ENTRYPOINT ["/entrypoint.sh", "/my-entrypoint.sh"]

CMD ["mysqld"]


mysql/my-entrypoint.sh



This chmod command is meant to avoid this kind of error

#!bin/bash

chmod 664 /etc/mysql/conf.d/custom.cnf

exec "$@"


mysql/config/custom.cnf



[mysqld]
default-storage-engine=INNODB
init-connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
bind-address = 0.0.0.0
skip-external-locking
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
query_cache_limit = 1M
query_cache_size = 16M
expire_logs_days = 10
max_binlog_size = 100M

[mysqldump]
quick
quote-names
max_allowed_packet = 16M

[isamchk]
key_buffer = 16M


All this stuff is working, except that I haven't any root access, blocking me from creating databases or mysql users for my apps !

Thank you for any advice :D !

EDIT: After days of investigation, and an issue in
docker-library/mysql
repository, we found out that one of the problems was a deprecated configuratio key in custom.cnf.

key_buffer => key_buffer_size


Now the container runs a few seconds after building, then crash.

The main clue in the logs is that line :

Version: '5.7.15' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)


I already tried a proposed solution to configure manually the port in custom.cnf but it does not work. The container tries to connect to MySQL and crash due to the port.

The problem was not pretty visible because when launching the container a second time, the initialization was skipped, the good port was configured, and the server worked fine.

The thing is that due to the crash, the end of the initialization was not executed, including my scripts and the databases creation along with users.

So I'd like to find out why this damn mysqld is listening on port 0 the first time I launch the container after building.

If you have a clue, I'd be glad to here about it !

Answer

Finally I figured it out, thanks to the guys on this issue : https://github.com/docker-library/mysql/issues/82

The culprit was the MYSQL_HOST environment variable, which was causing the mysqld --initialize-insecure command fail.

The solution is to replace 127.0.0.1 with localhost, which produces the following docker-compose.yml :

mysql:
    container_name: my_mysql
    build: mysql
    environment:
        - MYSQL_DATABASES=my-database
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_HOST=localhost
        - MYSQL_PORT=33306
        - MYSQL_USER=user
        - MYSQL_PASSWORD=password
        - MYSQL_MY_DATABASE=my-database
    ports:
        - "33306:3306"
    volumes:
        - "./volumes/mysql:/var/lib/mysql"

I hope it will help some people :D !