linux - How to change user of docker service?
问题描述
I'm having problem because i've installed & started docker as a "bad_user". The problem is that this container generates static files (its jekyll/jekyll image), and those files are owned by "bad_user" so i cannot edit them (i know i could add myself to bad_user group or own the dir by chown -R but it would be painful to do every time, and it just bugs me :).
I have tried to reinstall docker & removing /etc/docker directory without any effect. Every time i reinstall it (docker service/manager) runs as "bad_user" and overwrites directory owner.
My question is: Would that be possible to make docker running under "docker" user ? I have already created that user with that group (yes, i have reinstalled docker-ce under that user already).
Im working on debian-based distro.
I guess in my case its docker daemon issue, somehow when its syncrhonizing shared volume files it gives permission to bad_user instead of user who is running container.
PS. This is the command i run if that matters:
docker run --rm -p 8000:8000 \
--volume="/home/docker/blog:/srv/jekyll" \
-it tocttou/jekyll:3.5 \
jekyll serve --watch --port 8000
解决方案
Okay i figured it out. It turns out that when you run linux container that creates some files on the shared volume (the -v argument makes shared volume), the file permissions will be for user with grup id = 1000 and id = 1000. In my case user with id=1000 was "bad_user". If you want to workaround that you can use --user and specify user id that you're running under. The key is to remember that linux permissions are just numbers, for host filesystem number 1000 is (in my case) "bad_user" and 10001 is "docker_user". If you check permissions from inside of the container you'll might see that user id = 1000 means very different user than on your host system.
I hope that next people who will encounter this issue will find that userful.
You can find more information here: https://dille.name/blog/2018/07/16/handling-file-permissions-when-writing-to-volumes-from-docker-containers/
推荐阅读
- docker - 如何将正确的上下文传递给 docker-compose build?
- javascript - 函数启动时 JavaScript 中的秒表
- axios - 笑话:延迟 axios 拦截器处理程序
- c# - 如何将两个参数传递给 ASP.NET Core MVC 中的控制器?
- vba - shellcont/shell 图像
- masonry - TypeError: $(...).masonry 在 JS 中使用 init 时不是函数
- typescript - Typescript 4.1.5 传播运算符问题
- python - 如何计算不同行对的平均值并从数据框中删除 N-1 行?
- twilio - 仅限本地参与者的 Twilio 屏幕捕获
- django - /profile/ ModelForm 处的 ValueError 未指定模型类。错误django