Docker volume on host machine – write access and shared permissions

Using docker for running all kinds of reports (reporting applications, such as performance tests or penetration tests etc.) in the CI is a common practice. However, sometimes you’d have to cleanup the report directory in order to create a new one.

When running applications inside a docker, usually they will run as root and files created will have the root privileges.

There are many ideas on how to solve this, for example here is one https://vsupalov.com/docker-shared-permissions/.

I found out that using chown is the simplest thing to do. I am running everything from a bash script and then, at the end of whatever reporting I have to do, I simply change the file permissions to current host machine user. Here is the code example:

function changePermissions {
USERID=$(id -u)
USERGRP=$(id -g)
echo "Current user id: $USERID, current user group id: $USERGRP"
docker exec ${CONTAINER_NAME} chown -R $USERID:$USERGRP /opt/somereport/results/
}