首页 > 解决方案 > How do I set the perms of a my.cnf file to be readonly from within Docker (not at the client's OS level)?

问题描述

I'm using docker-compose v 1.27 and Docker v 19.03. I have this in my docker-compose.yml file ...

version: '3'
  

services:
  mysql:
    restart: always
    image: mysql:8.0
    cap_add:
      - SYS_NICE  # CAP_SYS_NICE
    environment:
      MYSQL_DATABASE: 'directory_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'root'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_ROOT_HOST: '%'
    ports:
      - "3406:3306"
    volumes:
      - my-db:/var/lib/mysql
      - ./mysql/mysqlconf:/etc/mysql/conf.d
    command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']

Note that I have no Dockerfile (didn't think I needed it). My "my.cnf" file, is below

davea$ cat mysql/mysqlconf/my.cnf 
bind-address = 0.0.0.0

From Docker, how do I set the permissions of the my.cnf file to be read-only? This comes into play on Windows 10 in which running "docker-compose up" results in this warning

mysqld: [Warning] World-writable config file '/etc/mysql/conf.d/my.cnf' is ignored.

Note, this answer -- https://stackoverflow.com/questions/64327260/in-docker-compose-how-do-i-set-perms-on-a-my-cnf-file-if-i-dont-have-a-dockerf, doesn't cut it, because it relies on setting th

标签: mysqldockerdocker-composepermissionsmy.cnf

解决方案


我认为这里的根本问题是您在 ext 文件系统中安装了 NTFS 目录卷。以下是一些可能有用的解决方案。


Docker 级别:使用只读卷挂载

您可以使用只读卷挂载而不是默认的读写设置。

例如,:ro在卷规范的末尾添加(只读):

    volumes:
      - ...
      - ./mysql/mysqlconf:/etc/mysql/conf.d:ro

容器级:chmod配置文件

command如果要抑制警告,可以尝试通过将配置扩展为多个命令,将运行时文件的权限设置为只读。不过,我认为这就是您所说的客户端操作系统级别。挂载不会是只读的。

例如:

    command: bash -c "
        chmod -R 0444 /etc/mysql/conf.d/ &&
        mysqld --user=root --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
        "

请注意,这与只读挂载不兼容,因为它是只读文件系统,因此您无法调整权限。


推荐阅读