Skip to content

Commit 6b55800

Browse files
committed
utils/aosp: Update script and Dockerfile to work with any host user uid/gid
This commit introduces the "docker_entrypoint" script that will create a user with uid/gid matching given `USER_ID` and `GROUP_ID` (or default to `1000` if not provided). Fixes #9 This approach works around missing docker feature discussed in moby/moby#7198 and allow to have executable in the docker container manipulating files in the shared volume owned by the `USER_ID:GROUP_ID` The utility script `aosp` has also been updated to automatically set `USER_ID` and `GROUP_ID` to the value matching the current user by invoking "docker run" with ``` -e USER_ID=$(id -u) -e GROUP_ID=$(id -g) ``` Finally, the output has also been updated to be more verbose. For example: ``` $ AOSP_VOL=/home/jcfr/Projects/aosp-root/ aosp id aosp: Checking if /home/jcfr/Projects/aosp-root/aosp exists aosp: Checking if /home/jcfr/Projects/aosp-root/aosp exists - ok aosp: Checking if /home/jcfr/Projects/aosp-root/ccache exists aosp: Checking if /home/jcfr/Projects/aosp-root/ccache exists - ok docker_entrypoint: Creating user UID/GID [1000/1000] docker_entrypoint: Creating user UID/GID [1000/1000] - done docker_entrypoint: Copying .gitconfig and .ssh/config to new user home docker_entrypoint: Copying .gitconfig and .ssh/config to new user home - done docker_entrypoint: Creating /tmp/ccache and /asop directory docker_entrypoint: Creating /tmp/ccache and /asop directory - done uid=1000(aosp) gid=1000(aosp) groups=1000(aosp) ```
1 parent 2ee12e4 commit 6b55800

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

Dockerfile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ RUN curl -O http://mirrors.kernel.org/ubuntu/pool/universe/o/openjdk-8/openjdk-8
4343
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
4444

4545
# All builds will be done by user aosp
46-
RUN groupadd -r aosp && useradd --create-home -g aosp aosp
47-
COPY gitconfig /home/aosp/.gitconfig
48-
COPY ssh_config /home/aosp/.ssh/config
49-
RUN chown aosp:aosp /home/aosp/.gitconfig && \
50-
chown aosp:aosp -R /home/aosp/.ssh
51-
52-
RUN mkdir -p /tmp/ccache /aosp && \
53-
chown aosp:aosp /tmp/ccache /aosp
46+
COPY gitconfig /root/.gitconfig
47+
COPY ssh_config /root/.ssh/config
5448

5549
# The persistent data will be in these two directories, everything else is
5650
# considered to be ephemeral
@@ -61,5 +55,7 @@ ENV USE_CCACHE 1
6155
ENV CCACHE_DIR /tmp/ccache
6256

6357
# Work in the build directory, repo is expected to be init'd here
64-
USER aosp
6558
WORKDIR /aosp
59+
60+
COPY utils/docker_entrypoint.sh /root/docker_entrypoint.sh
61+
ENTRYPOINT ["/root/docker_entrypoint.sh"]

utils/aosp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ AOSP_VOL_CCACHE=${AOSP_VOL_CCACHE%/} # Trim trailing slash if needed
2222
# Convenience function
2323
function aosp_create_dir_if_needed {
2424
directory=$1
25-
msg="Checking if $directory exists"
25+
msg="aosp: Checking if $directory exists"
2626
echo "$msg"
2727
if [ ! -d "$directory" ]; then
2828
echo "$msg - unexistent"
@@ -37,12 +37,15 @@ function aosp_create_dir_if_needed {
3737
aosp_create_dir_if_needed $AOSP_VOL_AOSP
3838
aosp_create_dir_if_needed $AOSP_VOL_CCACHE
3939

40+
# Set uid and gid to match host current user
41+
AOSP_HOST_ID_ARGS="-e USER_ID=$(id -u) -e GROUP_ID=$(id -g)"
42+
4043
if [ -n "$SSH_AUTH_SOCK" ]; then
4144
SSH_AUTH_ARGS="-v $SSH_AUTH_SOCK:/tmp/ssh_auth -e SSH_AUTH_SOCK=/tmp/ssh_auth"
4245
fi
4346

4447
echo ""
4548

46-
docker run $AOSP_ARGS $SSH_AUTH_ARGS $AOSP_EXTRA_ARGS \
49+
docker run $AOSP_ARGS $AOSP_HOST_ID_ARGS $SSH_AUTH_ARGS $AOSP_EXTRA_ARGS \
4750
-v "$AOSP_VOL_AOSP:/aosp" -v "$AOSP_VOL_CCACHE:/tmp/ccache" \
4851
$AOSP_IMAGE $@

utils/docker_entrypoint.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# This script designed to be used a docker ENTRYPOINT "workaround" missing docker
5+
# feature discussed in docker/docker#7198, allow to have executable in the docker
6+
# container manipulating files in the shared volume owned by the USER_ID:GROUP_ID.
7+
#
8+
# It creates a user named `aosp` with selected USER_ID and GROUP_ID (or
9+
# 1000 if not specified).
10+
11+
# Example:
12+
#
13+
# docker run -ti -e USER_ID=$(id -u) -e GROUP_ID=$(id -g) imagename bash
14+
#
15+
16+
# Reasonable defaults if no USER_ID/GROUP_ID environment variables are set.
17+
if [ -z ${USER_ID+x} ]; then USER_ID=1000; fi
18+
if [ -z ${GROUP_ID+x} ]; then GROUP_ID=1000; fi
19+
20+
msg="docker_entrypoint: Creating user UID/GID [$USER_ID/$GROUP_ID]" && echo $msg
21+
groupadd -g $GROUP_ID -r aosp && \
22+
useradd -u $USER_ID --create-home -r -g aosp aosp
23+
echo "$msg - done"
24+
25+
msg="docker_entrypoint: Copying .gitconfig and .ssh/config to new user home" && echo $msg
26+
cp /root/.gitconfig /home/aosp/.gitconfig && \
27+
chown aosp:aosp /home/aosp/.gitconfig && \
28+
mkdir -p /home/aosp/.ssh && \
29+
cp /root/.ssh/config /home/aosp/.ssh/config && \
30+
chown aosp:aosp -R /home/aosp/.ssh &&
31+
echo "$msg - done"
32+
33+
msg="docker_entrypoint: Creating /tmp/ccache and /asop directory" && echo $msg
34+
mkdir -p /tmp/ccache /aosp
35+
chown aosp:aosp /tmp/ccache /aosp
36+
echo "$msg - done"
37+
38+
echo ""
39+
40+
41+
# Execute command as `aosp` user
42+
export HOME=/home/aosp
43+
exec sudo -u aosp "$@"
44+
exec "$@"

0 commit comments

Comments
 (0)