When building a Maven project in Docker, all target files are rooted

Viewed 219

I have a Maven project which I want to build in a Docker container, so that I don't have to install Java, Maven, ... on my Jenkins system.

I build the Maven project as following:

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock:ro
                    -v $(pwd):/opt/maven
                    -w /opt/maven
                    maven
                    mvn clean install

This works great, the build is correctly made. However, when doing this as my regular user, all files in the target/ directory are owned by root and no by jester, which is my current user.

Is there an easy way to fix this?

1 Answers

You chan check out the option -u, as in this gist, which wraps docker run in a script:

#!/bin/sh

set -e
test ":$DEBUG" != :true || set -x

# set image
set -- debian:jessie "$@"

# use current user and its groups at host
for v in /etc/group /etc/passwd; do
  [ ! -r "$v" ] || set -- -v $v:$v:ro "$@"
done
set -- --user "`id -u`:`id -g`" "$@"
for g in `id -G`; do
  set -- --group-add "$g" "$@"
done
set -- -v "$HOME":"$HOME" "$@"

exec docker run --rm -it "$@"

The goal is to make sure the container mounts the local user definition, and uses the same uid/gid.

Related