We have interesting problem using Dockerfile. We have base image which doesn't have shell (it was removed by security reason) and we can use only Dockerfile directives in our app Docker file. And also we want to run it in the non-root mode.
In the base image we have set of groups with different permissions. For example:
- g1 - group which has access to /var/log
- g2 - group which has access to /var/run/docker.sock
These groups are defined in the base image.
I want to have ability to run docker container using non-root user and assign my user to these two groups inside my app container which is based on the base image.
I found that USER directive we can specify user and group which will be used for running container (for example USER app:g1).
Dockerfile looks like this:
# some code before FROM base:1.0 USER app:g1 CMD ["./app"] But I want to assign my user to two or more groups inside the Dockerfile of the app image.
Notes I can't modify base image, only the app image. And I have these groups on the host machine.
Notes 2 --group-add flag for docker run is not an option for me, because container is started by some internal software, which I don't manage.
Can I do this? If yes - how?
I tried to specify group names in the USER directive via comma, but it doesn't work (I believe it shouldn't work because it's not documented, I just tried :)).
I want to have ability to run docker container using non-root userI do not follow, inside the container you want to run docker containers, so docker-in-docker?/var/run/docker.sockis needed to get info about other docker containers which are running on the host. How COPY directive will help me in this case?get info about other docker containers which are running on the hostSo you do not want to run docker-in-docker, but access the docker outside the container. So are teh groupsg1andg2inside or outside the container? Do you know them by name or do you know them by gid?How COPY directive will help me in this case?You canCOPYbusybox and run shell inside. You canCOPYprepared /etc/shadow and /etc/groups and change groups inside the container. Do you can useRUNfrom dockerfile? How is this policy enforced, do you use automated tools?g1andg2and userappare created on the host OS and inside the base image with same ids (group id and user id). I can't use RUN directive because there is no shell for my app Docker container. I tried usingCOPYdirective to override /etc/shadow and /etc/groups and it works for me like a charm, thanks a lot. Could be some issue with this approach (I mean overriding files) inside the Docker container? One disadvantage which I can see that somebody can easily corrupt /etc/group or /etc/passwd and it would be hard to find the issue.RUN directive because there is no shell for my app DockerYes, so you canCOPY busybox /binand thenRUN busybox any_commandand install busybox and have all the commands. Also, not having shell is not really an issue, anyway you can doRUN ["executable", "args"]and executegroupaddorusermod.Could be some issueThe issue is the lack of synchronization with base image. I do not understand the sentence with corrupting, it's as easy to corrupt as it was. Copy it withCOPY --chown=root:rootso it's owned by root.