最近用docker-compse做中间件部署。

发现使用volume映射目录后,初始化时创建文件报错导致容器启动失败问题。

提示是没有访问权限。

如下:

mariadb 10:07:26.86 INFO  ==> 
mariadb 10:07:26.86 INFO  ==> Welcome to the Bitnami mariadb container
mariadb 10:07:26.86 INFO  ==> Subscribe to project updates by watching https://github.com/bitnami/containers
mariadb 10:07:26.86 INFO  ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
mariadb 10:07:26.86 INFO  ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
mariadb 10:07:26.87 INFO  ==> 
mariadb 10:07:26.88 INFO  ==> ** Starting MariaDB setup **
mariadb 10:07:26.89 INFO  ==> Validating settings in MYSQL_*/MARIADB_* env vars
mariadb 10:07:26.90 WARN  ==> You set the environment variable ALLOW_EMPTY_PASSWORD=yes. For safety reasons, do not use this flag in a production environment.
mariadb 10:07:26.90 INFO  ==> Initializing mariadb database
mariadb 10:07:26.91 INFO  ==> Updating 'my.cnf' with custom configuration
mariadb 10:07:26.92 INFO  ==> Setting user option
mariadb 10:07:26.93 INFO  ==> Setting slow_query_log option
mariadb 10:07:26.94 INFO  ==> Setting long_query_time option
mariadb 10:07:26.95 INFO  ==> Using persisted data
mariadb 10:07:26.95 INFO  ==> Running mysql_upgrade
mariadb 10:07:26.96 INFO  ==> Starting mariadb in background
2024-07-20 10:07:27 0 [Note] Starting MariaDB 11.4.2-MariaDB source revision 3fca5ed772fb75e3e57c507edef2985f8eba5b12 as process 59
2024-07-20 10:07:27 0 [ERROR] mysqld: Can't create/write to file './ddl_recovery.log' (Errcode: 13 "Permission denied")
2024-07-20 10:07:27 0 [ERROR] DDL_LOG: Failed to create ddl log file: ./ddl_recovery.log
2024-07-20 10:07:27 0 [ERROR] Aborting

这个需要手动将映射的目录赋权,而且要看容器使用的那个用户组。

先找到对应的镜像的Dockerfile

记录一个docker volume映射目录创建文件报错问题_运维

这里用bitnami/mysql的镜像演示。

入图,Dockerfile中指定了user 1001。这也是为了收缩容器权限,为了数据安全。

所以如果1001没有目录权限,会导致创建目录错误。

给绑定映射目录赋权:

sudo chown -R 1001:1001 /szxc/mysql
sudo chmod -R 755 /szxc/mysql

然后重启容器就可以了。