'Docker and MariaDB/MySQL — Permanently Editing my.cnf to enable remote access

I am running Docker on a Macintosh, and have installed the MariaDB image. I would like to access it from another machine on the LAN.

I understand that the solution is to enable bind-address=0.0.0.0 (or something similar) in /etc/mysql/my.cnf. I executed docker exec -it mariadb bash, installed Joe text editor (because I am much more familiar with it than Vi or Nano), and edited the file.

The problem is that when I restart the Docker image,it has forgotten all the changes, and it doesn’t work.

Am I missing a step, or is this not the way to go about it?



Solution 1:[1]

Containers are throw-away by design and, as you noticed, any modifications are lost when you run fresh one.

You have two options:

First one is described here: Docker: editing my.cnf in when building the image (just mount your custom config and be done).

Second option is to make your custom container image based on official image + your modification, something like this:

Dockerfile:

# Lets say mariadb v10.3.28... Change for what you want.
FROM mariadb:10.3.28

# there is already `#bind-address=0.0.0.0` in /etc/mysql/my.cnf
# we use sed and replace it with `bind-address=0.0.0.0`)
RUN sed -i "s/#bind-address=0.0.0.0/bind-address=0.0.0.0/g" /etc/mysql/my.cnf && \
    # and, for example, lets change `max_allowed_packet` too.
    sed -i "s/max_allowed_packet.*/max_allowed_packet=512M/g" /etc/mysql/my.cnf;

(rule of thumbs is "make as many steps in single RUN as possible" to save image layers)

then build it:

$ cd /where/my/dockerfile/is
$ docker build . -t mymysql

and run it:

# In newer mariadb it should be `-e MARIADB_ROOT_PASSWORD=`
# And maybe you should mount datadir somewhere `-v /my/own/datadir:/var/lib/mysql`
$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw mymysql

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1