Docker version 17.12.0-ce, build c97c6d6
when i use the bellow command:
docker run -e mysqld -e MYSQL_ROOT_PASSWORD=admin -v /home/epccweb/test:/var/lib/mysql mysql:latest
i get:
chown: changing ownership of /var/lib/mysql/: Operation not permitted
And i try a within a bash
docker run -e mysqld -e MYSQL_ROOT_PASSWORD=admin -v /home/epccweb/test:/var/lib/mysql -it mysql /bin/bash
Although i enter the container, when i serice mysql start, there's also something wrong:
No directory, logging in with HOME=/
mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied)
2018-03-26T13:03:45.052726Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-03-26T13:03:45.055608Z 0 [ERROR] --initialize specified but the data directory exists and is not writable. Aborting.
2018-03-26T13:03:45.055637Z 0 [ERROR] Aborting
No directory, logging in with HOME=/
./usr/bin/mysqld_safe: 647: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/035e8b45fb7c.err: Permission denied
Logging to '/var/lib/mysql/035e8b45fb7c.err'.
/usr/bin/mysqld_safe: 144: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/035e8b45fb7c.err: Permission denied
/usr/bin/mysqld_safe: 1: eval: cannot create /var/lib/mysql/035e8b45fb7c.err: Permission denied
/usr/bin/mysqld_safe: 906: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/035e8b45fb7c.err: Permission denied
/usr/bin/mysqld_safe: 144: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/035e8b45fb7c.err: Permission denied
i check my own directory use ll
drwxrwxr-x 2 epccweb epccweb 4096 Mar 26 20:33 test
after i
chmod 777 /home/epccweb/test
there is nothing wrong.
Do I have any better ways?thanks
Since there is not a supervisor or init system running in the container, things like service mysql start will not do what you expect. There is a bunch of setup that is done by the docker-entrypoint.sh script that is not done when you start the container with the bash process. There is also the problem of bash being pid 1, so once it exits, that container exits and all child processes — like those spawned by an init script — are killed.
As for the chown failing that is often caused by Docker for Mac/Windows (or Boot2Docker/Docker Toolbox) folder sharing from the Docker virtual machine to the Host OS. The workaround is to just run the container as the owner of the directory you are trying to use:
$ docker run -d -e MYSQL_ROOT_PASSWORD=admin -v /home/epccweb/test:/var/lib/mysql --user 1000:50 mysql:5.7
$ # or whatever user and group id that the container sees on the mounted folder:
$ docker run -it --rm -v /home/epccweb/test:/var/lib/mysql mysql:5.7 ls -aln /var/lib/mysql
It can also be SELinux like in https://github.com/docker-library/mysql/issues/198#issuecomment-277158022.
@yosifkit thanks for your reply, but i still have problem.
Yean, I noticed the difference for the entry point of docker before.There are some chown command.
And I tried map host user to docker user:
docker run -d -e MYSQL_ROOT_PASSWORD=admin -v /home/epccweb/test:/var/lib/mysql --user 2034:50 mysql:5.7
the container seems to stop immediately.
So I start with bash:
docker run -it -e MYSQL_ROOT_PASSWORD=admin -v /home/epccweb/test:/var/lib/mysql --user 2034:50 mysql:5.7 /bin/bash
service mysql start
And I got result:
su: Cannot determine your user name.
...............................
Since there is not a supervisor or init system running in the container, things like
service mysql startwill not do what you expect.
If you drop the /bin/bash, the image will start mysql by default (as mentioned above, service ... start will _not_ work).
the container seems to stop immediately.
Did you check the container logs? There should be some clue there as to what's failing (which is likely still permissions, I'd imagine).
Given that this permissions error is a limitation of the file sharing in Docker for Mac/Windows (or something environmental), I'm going to close (since there's not anything we can do to fix this in the image).
For further help debugging, I'd recommend trying the Docker Community Forums, the Docker Community Slack, or Stack Overflow.
Most helpful comment
Since there is not a supervisor or init system running in the container, things like
service mysql startwill not do what you expect. There is a bunch of setup that is done by thedocker-entrypoint.shscript that is not done when you start the container with thebashprocess. There is also the problem ofbashbeing pid 1, so once it exits, that container exits and all child processes — like those spawned by an init script — are killed.As for the
chownfailing that is often caused by Docker for Mac/Windows (or Boot2Docker/Docker Toolbox) folder sharing from the Docker virtual machine to the Host OS. The workaround is to just run the container as the owner of the directory you are trying to use:It can also be SELinux like in https://github.com/docker-library/mysql/issues/198#issuecomment-277158022.