mysqldockermy.cnf

How to override standard mysql 5.7 container configuration


I cannot replace the standard /etc/my.cnf file in a standard mysql 5.7 container.

I am running a mysql 5.7 container (standard Docker HUB image). I want to replace the etc/my.cnf file to enable master-slave operations. I want to achieve this by mounting the volume from the docker-compose.yml file.

The new my.cnf should be:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Replication and mirroring settings
server-id = 1
auto_increment_increment = 3
auto_increment_offset = 0
log_bin           = /var/log/mysql/log-bin
log_bin_index     = /var/log/mysql/log-bin.index
binlog_format     = row
expire_logs_days  = 10
log_slave_updates

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

In docker-compose.yml I have the following lines:

mysql:
    image: mysql/mysql-server:5.7
    container_name: rucio-mysql
    volumes:
      - ./mysql-config/my-master.cnf:/etc/my.cnf
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_ROOT_HOST=%

The paths are correct, but the container gets started and exits immediately.

Thanks in advance,

Slid


Solution

  • You cannot specify files as volumes, it doesn't work so. Volumes are directories.

    If your config is constant and is not expected to change between container executions, simply inherit your own container from mysql/mysql-server:5.7 and COPY new config into it:

    Dockerfile

    FROM mysql/mysql-server:5.7
    COPY my.cnf /etc/
    

    and use your new image in docker-compose.

    If configuration is a matter of chages, better use volumes, but you will have to manually overwrite it upon container start. Something like this:

    docker-compose.yml

    mysql:
        image: mysql/mysql-server:5.7
        container_name: rucio-mysql
        volumes:
          - ./mysql-config:/configs
        environment:
          - MYSQL_ROOT_PASSWORD=pass
          - MYSQL_ROOT_HOST=%
        command: ["cp", "-f", "/configs/my-master.cnf", "/etc/my.cnf"]