Database 2021-09-12

Encountered 'Permission denied' Error When Starting MySQL Container on Ubuntu 20.04.2 LTS

Fix MySQL container permission denied errors on Ubuntu 20.04 by properly configuring docker-compose user UID/GID mappings.

Read in: ja
Encountered 'Permission denied' Error When Starting MySQL Container on Ubuntu 20.04.2 LTS

Overview

When attempting to start a MySQL container on Ubuntu 20.04.2 LTS, the following error occurs, causing the container to fail to start.

Could not open file '/var/log/mysql/mysql-error.log' for error logging: Permission denied”

Dockerfile

The Dockerfile where the issue occurred.

docker-compose.yml (partial excerpt)

version: '3.2'
services:
  mysql:
    container_name: "example-mysql"
    env_file: ./mysql/.env
    build:
        context: "./mysql"
        dockerfile: "Dockerfile"
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/initdb.d:/docker-entrypoint-initdb.d
      - ./mysql/log:/var/log/mysql

Dockerfile

FROM --platform=linux/amd64 mysql:8.0.26

ADD ./conf.d/my.cnf /etc/mysql/conf.d/my.cnf

CMD ["mysqld"]

Checking the Mount Source

$ ls -la mysql
drwxrwxrwx  7 systemd-coredump example-app 4096 Sep 12 23:10 data

An unfamiliar user named systemd-coredump appears.

systemd-coredump

Checking the user on the host, systemd-coredump has uid 999.

$ cat /etc/passwd | grep systemd-coredump
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin

The user inside the MySQL container likely has uid 999, which is probably the cause?

Solution

Add user: 1000:1000 to docker-compose.yml.

docker-compose.yml (partial excerpt)

version: '3.2'
services:
  mysql:
    container_name: "example-mysql"
    env_file: ./mysql/.env
    build:
        context: "./mysql"
        dockerfile: "Dockerfile"
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/initdb.d:/docker-entrypoint-initdb.d
      - ./mysql/log:/var/log/mysql
    user: 1000:1000

It might be better to pass the uid and gid from the host instead of hardcoding them.

Thoughts

This issue did not occur on Docker for Mac, so I'm glad I was able to notice it.

References

Tags: Ubuntu MySQL Tips
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
β˜• Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles