17

I'm having an issue when starting the db service with docker compose:

 version: '3'
 services:

 # Mysql DB
    db:
        image: percona:5.7
        #build: ./docker/mysql
        volumes:
          - "./db/data:/var/lib/mysql"
          - "./db/init:/docker-entrypoint-initdb.d"
          - "./db/backups:/tmp/backups"
          - "./shared/home:/home"   
          - "./shared/root:/home"  
        restart: unless-stopped
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: db_name
            MYSQL_USER: user
            MYSQL_PASSWORD: pass
       ports:
       - "3307:3306"

I have tried everything with no luck:

"./db/data:/var/lib/mysql:rw"

Creating a dockerfile and create from build instead of image:

FROM percona:5.7

RUN adduser mysql
RUN sudo chown mysql /var/lib/mysql
RUN sudo chgrp mysql /var/lib/mysql

Also I have tried to add a user on db service:

user: "1000:50"

But any of those could solve that.. What I'm missing?

MySQL 5.7 installation error `mysqld: Can't create/write to file '/var/lib/mysql/is_writable'`

Albeis
  • 1,544
  • 2
  • 17
  • 30

7 Answers7

14

I had to change ./db/data user:group to 999:999, so docker user is who is making the changes.

sudo chown 999:999 ./db/data
Albeis
  • 1,544
  • 2
  • 17
  • 30
  • 4
    Was getting the same problem. This solution solved the problem, but seem to not be the best way to achieve that. On my host, uid 999 is used by systemd-coredump user, and mysql user inside the container. For my actual point of view, it's working for this user, with mysql image, but will be quickly inapropriate if you have extra services. Right must be managed in a better way. If anyone have information to add, feel free. – rSim Jun 11 '20 at 17:06
  • why don't do you use: `sudo chown 999:root ./db/data`? – alper Jun 03 '22 at 18:03
3

Make sure that the user who is running docker has access to ./db/data

# Not in the dockerfile
sudo chown $(whoami) ./db/data
sudo chgrp $(whoami) ./db/data

Docker tells you that you don't have the permissions, it might also mean that you need to verify that you shared volume ./db/data need to have the correct permissions.

callmemath
  • 8,185
  • 4
  • 37
  • 50
2

According Dockerfile Percona 5.7 images runs under CentOS v8 and user mysql. Check the user ID (uid) and group ID (gid) inside container:

user@host$ docker run --rm -t percona:5.7.29 sh -c 'id'
uid=999(mysql) gid=999(mysql) groups=999(mysql)

Default user inside container uses 999 uid and gid. Than change your directory rights to 999:999:

sudo chown 999:999 ./db/data

This is an addition to Albeis answer.

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
1

I spent a whole day with a similar (almost exactly) problem. I also changed ownership of the related files, only to see them get wiped out and come back with permissions issues. I changed the ownership of my curl-installed docker-compose executable. I didn't receive a bit of reprieve until adding the volumes to the .dockerignore, as was suggested in this Github issue reply.

fireside68
  • 55
  • 10
0

I suffered this issue and it took quite some time to figure out what the culprit was.

In my case, I have a dual boot system Winblowz-Linux. My code of the problematic project was on a Windows filesystem.

Once I cloned the project into my Linux drive, on a ext4 filesystem, the problem went away.

progonkpa
  • 3,590
  • 9
  • 31
  • 50
0

need permission to execute scripts in directory

sudo chown 999:999 ./db/data
sudo chmod +x ./db/data
0

Instead of changing the permissions to match the container user id you can map them while building the container: docker-compose build --build-arg UID=$(id -u) --build-arg GID=$(id -g)

Dockerfile:

RUN usermod  --uid $UID mysql
RUN groupmod --gid $GID mysql
br4nnigan
  • 646
  • 6
  • 13