title: docker: run mysql container with specific uid:gid
body: <p>i am building an app stack where mysql is one of the services. I want all services to run as uid 1000:1000, so i dont have any trouble with permissions on the host system.</p>
  
  <p>Now i have problems doing this with the official mysql docker image.</p>
  
  <p>Here is what i do:</p>
  
  <pre><code>FROM mysql:5.7
  RUN addgroup --gid 1000 app \
   &amp;&amp; adduser --gecos "" --home /home/app --shell /bin/bash --disabled-password --uid 1000 --gid 1000 app \
   &amp;&amp; adduser app mysql \
   &amp;&amp; adduser mysql app
  RUN chown -R app:app /var/lib/mysql &amp;&amp; chmod -R g+wrx /var/lib/mysql
  COPY mysql.cnf /etc/mysql/conf.d/99-docker.cnf
  COPY .my.cnf /home/app/.my.cnf
  COPY .root.cnf /root/.my.cnf
  RUN chmod 0664 /etc/mysql/conf.d/99-docker.cnf
  USER app
  EXPOSE 3306
  </code></pre>
  
  <p>And the service in my compose file:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>version: "3"
  
  services:
  
    mysql:
      build:
        context: ./docker/mysql
        dockerfile: Dockerfile
      env_file:
        - .env
      volumes:
        - ./var/mysql:/var/lib/mysql
      user: 1000:1000
      ports:
        - 3307:3306
      command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
  </code></pre>
  
  <p>The env file contains some vars for mysql:</p>
  
  <pre><code>MYSQL_ROOT_PASSWORD=root
  MYSQL_DATABASE=app
  MYSQL_USER=app
  MYSQL_PASSWORD=app
  </code></pre>
  
  <p>the .my.cnf files contain credentials</p>
  
  <p>the mysql config contains some cache settings, sql-mode and:</p>
  
  <pre><code>[mysqld]
  user=app
  </code></pre>
  
  <p>when starting the service with: <code>docker-compose up --build mysql</code> i get the following error:</p>
  
  <pre><code>mysql_1  | Initializing database
  mysql_1  | mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied)
  mysql_1  | 2020-04-09T11:11:25.664285Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  mysql_1  | 2020-04-09T11:11:25.664345Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
  mysql_1  | 2020-04-09T11:11:25.666725Z 0 [ERROR] --initialize specified but the data directory exists and is not writable. Aborting.
  mysql_1  | 2020-04-09T11:11:25.666744Z 0 [ERROR] Aborting
  mysql_1  | 
  pixel3_mysql_1 exited with code 1
  </code></pre>
  
  <p>When running <code>docker-compose run mysql bash</code> and inspecting the files i see, that the /var/lib/mysql directory is root-owned.</p>
  
  <p>In the documentation (<a href="https://hub.docker.com/_/mysql" rel="nofollow noreferrer">https://hub.docker.com/_/mysql</a>) it says i can run the image with another user but to me it seems this is not possible.</p>
  
  <p>Do i have to override the entrypoint to chown the directories over and over again or what is my mistake?</p>