0
votes

I'm trying to instantiate a mysql container with a /var/lib/mysql/ directory with proper permissions and ownership for COPY binding the sql dumps from my old mysql server. But with /var/lib/mysql/ having its permissions and ownership as-is, the container exits upon mount. So I am trying to alter the permissions and ownership of this directory upon instantiation of the container. But this is proving difficult ...

Target: rwxr-xr-x mysql:root /var/lib/mysql

Configuration:

  1. Dockerfile: FROM mysql/mysql-server
    RUN chmod 755 /var/lib/mysql
    RUN chown -R mysql:root /var/lib/mysql/

  2. Images: REPOSITORY TAG IMAGE ID CREATED SIZE
    wat 1.0 09edc2e9d91d About an hour ago 381MB
    mysql/mysql-server latest 716286be47c6 2 months ago 381MB

  3. docker-compose.yml: version: "3"
    services:
    mysqlmysql-server:
    image: wat:1.0
    container_name: mysqlmysql-server

Processing ...

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b21339cf2986 wat:1.0 "/entrypoint.sh mysq…" 11 minutes ago Up 11 minutes (healthy) 3306/tcp, 33060/tcp mysqlmysql-server

b21339cf2986# ls /var/lib -al | grep -e mysql$
drwxr-x--- 7 mysql mysql 4096 Jun 27 04:06 mysql

This is wrong. I am expecting at least rwxr-xr-x and/or mysql:root by now

Teardown / Square 1 ##

$ sudo docker stop *; sudo docker rm *; sudo docker image rmi *; sudo docker pull mysql/mysql-server
$ sudo docker build --tag wat:1.0 - < mysqlmysql-server/Dockerfile-mysqlmysql-server
Successfully built 2a38f54299d8
Successfully tagged wat:1.0
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wat 1.0 2a38f54299d8 About a minute ago 381MB
mysql/mysql-server latest 716286be47c6 2 months ago 381MB

$ sudo docker-compose up -d
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b94f9e776d78 wat:1.0 "/entrypoint.sh mysq…" About a minute ago Up 59 seconds (healthy) 3306/tcp, 33060/tcp mysqlmysql-server

$ sudo docker exec -it b94f9e776d78 bash
bash-4.2# ls /var/lib -al | grep -e ql$ drwxr-x--- 6 mysql mysql 4096 Jun 27 04:27 mysql

The above is WRONG

Target: rwxr-xr-x mysql:root /var/lib/mysql

1

1 Answers

3
votes

The Docker Hub mysql/mysql-server image declares a VOLUME /var/lib/mysql. That means that subsequent Dockerfile steps can't make any further changes to that directory.

In practice you will almost always mount your own storage on that directory. When you do that the contents and permissions of that storage (either a host directory or a Docker named volume) will hide anything that the Dockerfile does. The Docker Hub mysql image page has instructions for "Running as an arbitrary user" which will let you run the container with a user ID matching a host directory's owner; those instructions may or may not work for the alternate image you're using.