Skip to content

Need help on persistence data for Mariadb in Docker Container using latest image #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fulwang opened this issue Jul 14, 2022 · 10 comments
Labels
help wanted need feedback Need feedback from user.

Comments

@fulwang
Copy link

fulwang commented Jul 14, 2022

I want to persist Mariadb's datadir to local directory on my mac, but it doesn't work if i add the bind mount parameter.
without the bind mount setup, i can see the Mariadb service can startup normally.

(base) fulwang@MacPro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker run --detach --name pmariadb --env MARIADB_ROOT_PASSWORD=123456 -v $HOME/mysql/data:/var/lib/mysql mariadb:latest
47eaaaf4d64b7ed49ecd3be932a3dc95f3c3273d27c850ebb2fa1bc8b36bc95c
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
47eaaaf4d64b docker.io/library/mariadb:latest mariadbd 30 minutes ago Exited (1) 30 minutes ago pmariadb
(base) fulwang@MacPro ~ % docker logs pmariadb
2022-07-14 06:59:25+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
chown: changing ownership of '/var/lib/mysql/': Operation not permitted
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % ls -l $HOME/mysql/data
total 0
(base) fulwang@MacPro ~ % ls -l $HOME/mysql/
total 0
drwxr-xr-x 2 fulwang staff 64 Jul 14 15:29 data
(base) fulwang@MacPro ~ %

@grooverdan
Copy link
Member

As a bit of a guess without an Mac environment, add --user $(stat -c '%u:%g' "$HOME/mysql/data") to your docker run command.

Otherwise check the owner/group as it exists within the container with:

docker run --rm  -v $HOME/mysql/data:/var/lib/mysql mariadb:latest ls -la /var/lib/mysql

@grooverdan grooverdan added need feedback Need feedback from user. help wanted labels Jul 14, 2022
@fulwang
Copy link
Author

fulwang commented Jul 14, 2022

seems no equivalent stat command option on mac as in linux, i find the uid,gid of the directory manually and run the command, still got errors. as the container already exit, can't check the owner/group within the container.

(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker run --detach --name pmariadb --user $(stat -c '%u:%g' "$HOME/mysql/data") --env MARIADB_ROOT_PASSWORD=123456 -v $HOME/mysql/data:/var/lib/mysql mariadb:latest
stat: illegal option -- c
usage: stat [-FLnq] [-f format | -l | -r | -s | -x] [-t timefmt] [file ...]
Error: invalid reference format
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % stat -x $HOME/mysql/data
File: "/Users/fulwang/mysql/data"
Size: 64 FileType: Directory
Mode: (0755/drwxr-xr-x) Uid: ( 501/ fulwang) Gid: ( 20/ staff)
Device: 1,13 Inode: 4197408 Links: 2
Access: Thu Jul 14 15:29:34 2022
Modify: Thu Jul 14 15:29:34 2022
Change: Thu Jul 14 15:29:34 2022
Birth: Thu Jul 14 11:43:25 2022
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker run --detach --name pmariadb --user 501:20 --env MARIADB_ROOT_PASSWORD=123456 -v $HOME/mysql/data:/var/lib/mysql mariadb:latest
58c0b47b6f653bd9b34d23d079965850a8fac1207b9690b92da9b8d28b679023
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58c0b47b6f65 docker.io/library/mariadb:latest mariadbd About an hour ago Exited (1) About an hour ago pmariadb
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker logs pmariadb
2022-07-14 10:19:26+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
2022-07-14 10:19:26+00:00 [Note] [Entrypoint]: Initializing database files
2022-07-14 10:19:26 0 [Warning] Can't create test file /var/lib/mysql/58c0b47b6f65.lower-test
2022-07-14 10:19:26 0 [ERROR] mariadbd: Can't create/write to file './ddl_recovery.log' (Errcode: 13 "Permission denied")
2022-07-14 10:19:26 0 [ERROR] DDL_LOG: Failed to create ddl log file: ./ddl_recovery.log
2022-07-14 10:19:26 0 [ERROR] Aborting

Installation of system tables failed! Examine the logs in
/var/lib/mysql/ for more information.

The problem could be conflicting information in an external
my.cnf files. You can ignore these by doing:

shell> /usr/bin/mariadb-install-db --defaults-file=~/.my.cnf

You can also try to start the mysqld daemon with:

shell> /usr/sbin/mariadbd --skip-grant-tables --general-log &

and use the command line tool /usr/bin/mariadb
to connect to the mysql database and look at the grant tables:

shell> /usr/bin/mysql -u root mysql
mysql> show tables;

Try 'mysqld --help' if you have problems with paths. Using
--general-log gives you a log in /var/lib/mysql/ that may be helpful.

The latest information about mysql_install_db is available at
https://mariadb.com/kb/en/installing-system-tables-mysql_install_db
You can find the latest source at https://downloads.mariadb.org and
the maria-discuss email list at https://launchpad.net/~maria-discuss

Please check all of the above before submitting a bug report
at https://mariadb.org/jira
(base) fulwang@MacPro ~ %

@fulwang
Copy link
Author

fulwang commented Jul 14, 2022

My mac is running MacOS 12.4 Monterey (Chip: Apple M1 Pro)

@grooverdan
Copy link
Member

Guess stat -c is a gnuism.

Did you look at:

docker run --rm  -v $HOME/mysql/data:/var/lib/mysql mariadb:latest ls -la /var/lib/mysql

Or:

docker run --rm  -v $HOME/mysql/data:/var/lib/mysql mariadb:latest stat -x /var/lib/mysql

The uid/gid it appears as inside the container may be different.

I assume you have file shared this path like https://stackoverflow.com/questions/58341846/how-to-mount-volume-to-docker-on-osx.

@grooverdan
Copy link
Member

@fulwang
Copy link
Author

fulwang commented Jul 15, 2022

Guess stat -c is a gnuism.

Did you look at:

docker run --rm -v $HOME/mysql/data:/var/lib/mysql mariadb:latest ls -la /var/lib/mysql

Here is the output:

(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(base) fulwang@MacPro ~ % docker run --rm -v $HOME/mysql/data:/var/lib/mysql mariadb:latest ls -la /var/lib/mysql
total 0
drwxr-xr-x. 2 root nogroup 64 Jul 14 13:53 .
drwxr-xr-x. 1 root root 68 May 23 18:42 ..
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(base) fulwang@MacPro ~ % docker run --rm -v $HOME/mysql/data:/var/lib/mysql mariadb:latest ls -la /var/lib/
total 0
drwxr-xr-x. 1 root root 68 May 23 18:42 .
drwxr-xr-x. 1 root root 41 Apr 28 12:15 ..
drwxr-xr-x. 1 root root 29 May 23 18:41 apt
drwxr-xr-x. 1 root root 98 May 23 18:42 dpkg
drwxr-xr-x. 2 root root 6 Apr 18 10:28 misc
drwxr-xr-x. 2 root nogroup 64 Jul 14 13:53 mysql
drwxr-xr-x. 1 root root 106 May 23 18:41 pam
-rw-r--r--. 1 root root 0 Apr 28 11:58 shells.state
drwxr-xr-x. 1 root root 40 May 23 18:40 systemd
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % ls -la $HOME/mysql/data
total 0
drwxr-xr-x 2 fulwang staff 64 Jul 14 21:53 .
drwxr-xr-x 3 fulwang staff 96 Jul 14 11:43 ..
(base) fulwang@MacPro ~ %
(base) fulwang@MacPro ~ % ls -la $HOME/mysql/
total 0
drwxr-xr-x 3 fulwang staff 96 Jul 14 11:43 .
drwxr-x---+ 56 fulwang staff 1792 Jul 14 21:52 ..
drwxr-xr-x 2 fulwang staff 64 Jul 14 21:53 data
(base) fulwang@MacPro ~ %

@fulwang
Copy link
Author

fulwang commented Jul 15, 2022

I assume you have file shared this path like https://stackoverflow.com/questions/58341846/how-to-mount-volume-to-docker-on-osx.

Just use bind mount (persist the mariadb datadir to macos local filesystem), no docker desktop installed and not using volumes

@grooverdan
Copy link
Member

So summarizing:

  • your local volume has user:group fulwang:staff
  • when shared into the docker this maps to root:nogroup
  • mariadb container runs in two modes, --user root (default) and non-root
  • when in root, it attempts to chown the volume, and fails
  • when in non-root, it assumes the volume permissions map to the running user, and fail.

While changing the root behaviour to continue as root if it can't chown the volume is possible (and might have consequences), I'd rather use other options if possible.

https://docs.docker.com/desktop/mac/ recommends using named volume/data container for database volumes. Is there are reason you aren't doing this?

The other option in the docker docs is VirtioFS. I don't know if it will present user/groups differently or permit chown of them.

@fulwang
Copy link
Author

fulwang commented Jul 18, 2022

Thanks @grooverdan!

One question, do you mean during the Mariadb container startup it will try root/non-root mode automatically in the background?
Maybe the workaround in this thread can work in this situation(99)

And I'd try the named volume option later. no particular requirements in my project for selecting the persisting solution, i just try the options we have and make some comparisons before the real deployment.

@grooverdan
Copy link
Member

One question, do you mean during the Mariadb container startup it will try root/non-root mode automatically in the background?

Not so much background, as early stages of the entrypoint like what the 99 [issue did|https://github.com/neam/docker-stack/blob/develop/stacks/neam/debian-php-nginx.dna-project-base/stack/localdb/run.sh].

The workaround around will fail in singularity (like #401) as the non-volume is mounted RO during runtime (that I need tests for).

So yes, please use named volumes like the Docker documents recommend. If you do any virtiofs test I'm interested in the results.

@grooverdan grooverdan closed this as not planned Won't fix, can't repro, duplicate, stale Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted need feedback Need feedback from user.
Development

No branches or pull requests

2 participants