0

I have one windows and one ubuntu 22.04 OS on my device. I have a special shared partition on my harddisk that I can enter from both of the OS'es.

On the shared partition I have a docker setup and I would like it to store the mysql data (volume) in a folder inside the host folder (./mysql_data)

The disk is mounted ntfs and thus, has as permission for all files and folders 1000:1000. Now, that is not docker (gid=999) so I have a hard time getting all permissions correct. Even when I set the -user 1000:1000 flag, issues keep piling up all the time:

  • corrupt files
  • not being able to login into phpmyadmin
  • permission issues

What would be a correct configuration for both the partition mount and the docker file?

partition mount (/etc/fstab):

UUID=xxx /mnt/share ntfs defaults,uid=1000,gid=999,fmask=0022,dmask=0000 0 0

docker-compose.yml:

version: "3.1"
services:
    www:
        build:
            context: .
            dockerfile: Dockerfile.lamp
        user: 1000:1000
        ports: 
            - "${WEBSERVER_PORT}:80"
        volumes:
            - ./www:/var/www/html/
        links:
            - db
        networks:
            - default
    db:
        image: mysql:8.0
        user: 1000:1000
        ports: 
            - "3306:3306"
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} 
        volumes:
            - ./sql:/docker-entrypoint-initdb.d
            - ./conf:/etc/mysql/conf.d
            - mysql_data:/var/lib/mysql
        networks:
            - default
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        links: 
            - db:db
        ports:
            - ${PHPMYADMIN_PORT}:80
        networks:
            - default
        environment:
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
            UPLOAD_LIMIT: 64M 
volumes:
    mysql_data:
      driver: local
      driver_opts:
          type: 'none'
          o: 'bind'
          device: './mysql_data'
networks:
    default:

Any suggestions to how I could successfully achieve this? given that a mount has issues with permissions and will only set permissions on all files as given by the /etc/fstab file or default 1000:1000

Mart
  • 475
  • 4
  • 21

1 Answers1

1

Why default is 1000:1000?

The reason it set 1000:1000 as default is used to prevent docker created a file/folder own by root, once it own by root lot of situation will cause permission error if any process want to create or write file/folder, but if own by 1000:1000, you can change owner easily to which you want.

And also, set to 1000:1000 doesn't means you or any user have permissions to acceess with it, it still followed Linux permission rules, 1000:1000 is just Linux normal level user gid:uid count where it start with.

How mysql work around?

For example, Here is the mysql8.0 image (https://github.com/docker-library/mysql/blob/master/8.0/Dockerfile.oracle).

You can see it create a mysql:mysql user at beginning.

RUN set -eux; \
    groupadd --system --gid 999 mysql; \
    useradd --system --uid 999 --gid 999 --home-dir /var/lib/mysql --no-create-home mysql

And it changed these folders owner to mysql user on line 85, and it's the folder where mysql data stored by.

# ensure these directories exist and have useful permissions
# the rpm package has different opinions on the mode of `/var/run/mysqld`, so this needs to be after install
    mkdir -p /var/lib/mysql /var/run/mysqld; \
    chown mysql:mysql /var/lib/mysql /var/run/mysqld; \
# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
    chmod 1777 /var/lib/mysql /var/run/mysqld; \

What should you do?

Change your volume folder owner to which the container user, in mysql container you should change owner to mysql:mysql, or mysql won't able to write.

In other container do as same, change owner to which user your use to write/read, but docker compose can't volume as a specific user, so you might need try this: https://stackoverflow.com/a/56990338.

Notice that in your case user flag is not needed.

Charlie
  • 287
  • 11
  • Thank you. But I think (or i don't understand) there is still a small issue here in this specific case. Since I'm working in a mounted partition. There is only one owner (user & group) allowed for all elements on that partition. I cannot change the owner of a specific folder/file in that partition or i would have to do so for the whole partition – Mart Feb 17 '23 at 08:29
  • @Mart **In different container you can assign same folder to different user**. For example, in mysql container should be mysql owner, in nginx with php-fpm container should be www-data owner, and you should applied according to your actual environment. – Charlie Feb 18 '23 at 01:33
  • Which you said about "I cannot change the owner of a specific folder/file" is not true, you can just run `chown group:user filename` to change specific file owner after you mounted. – Charlie Feb 18 '23 at 01:33