Skip to content

Commit 4fd3a9d

Browse files
committed
Merge pull request #11 from jcfr/support-current-user-with-arbitrary-uid-gid
Update `Dockerfile` and `utils/aosp` to work with arbitrary uid gid
2 parents 66505f7 + f1b7f6a commit 4fd3a9d

File tree

4 files changed

+110
-11
lines changed

4 files changed

+110
-11
lines changed

Dockerfile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,28 @@ RUN apt-get update && \
2323
ADD https://commondatastorage.googleapis.com/git-repo-downloads/repo /usr/local/bin/
2424
RUN chmod 755 /usr/local/bin/*
2525

26+
# Install latest version of JDK
27+
# See http://source.android.com/source/initializing.html#setting-up-a-linux-build-environment
28+
WORKDIR /tmp
29+
RUN curl -O http://mirrors.kernel.org/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jre-headless_8u45-b14-1_amd64.deb && \
30+
curl -O http://mirrors.kernel.org/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jre_8u45-b14-1_amd64.deb && \
31+
curl -O http://mirrors.kernel.org/ubuntu/pool/universe/o/openjdk-8/openjdk-8-jdk_8u45-b14-1_amd64.deb && \
32+
sum=`shasum ./openjdk-8-jre-headless_8u45-b14-1_amd64.deb | awk '{ print $1 }'` && \
33+
[ $sum == "e10d79f7fd1b3d011d9a4910bc3e96c3090f3306" ] || \
34+
( echo "Hash mismatch. Problem downloading openjdk-8-jre-headless" ; exit 1; ) && \
35+
sum=`shasum ./openjdk-8-jre_8u45-b14-1_amd64.deb | awk '{ print $1 }'` && \
36+
[ $sum == "1e083bb952fc97ab33cd46f68e82688d2b8acc34" ] || \
37+
( echo "Hash mismatch. Problem downloading openjdk-8-jre" ; exit 1; ) && \
38+
sum=`shasum ./openjdk-8-jdk_8u45-b14-1_amd64.deb | awk '{ print $1 }'` && \
39+
[ $sum == "772e904961a2a5c7d2d129bdbcfd5c16a0fab4bf" ] || \
40+
( echo "Hash mismatch. Problem downloading openjdk-8-jdk" ; exit 1; ) && \
41+
dpkg -i *.deb && \
42+
apt-get -f install && \
43+
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
44+
2645
# All builds will be done by user aosp
27-
RUN useradd --create-home aosp
28-
ADD gitconfig /home/aosp/.gitconfig
29-
ADD ssh_config /home/aosp/.ssh/config
30-
RUN chown aosp:aosp /home/aosp/.gitconfig
46+
COPY gitconfig /root/.gitconfig
47+
COPY ssh_config /root/.ssh/config
3148

3249
# The persistent data will be in these two directories, everything else is
3350
# considered to be ephemeral
@@ -38,5 +55,7 @@ ENV USE_CCACHE 1
3855
ENV CCACHE_DIR /tmp/ccache
3956

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

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DOCKER = docker
2+
IMAGE = kylemanna/aosp
3+
4+
aosp: Dockerfile
5+
$(DOCKER) build -t $(IMAGE) .
6+
7+
all: aosp
8+
9+
.PHONY: all

utils/aosp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,44 @@
88
set -e
99

1010
# Override from environment
11+
1112
AOSP_IMAGE=${AOSP_IMAGE:-kylemanna/aosp}
12-
AOSP_VOL=${AOSP_VOL:-/vol0}
1313
AOSP_ARGS=${AOSP_ARGS:---rm -it}
14+
15+
AOSP_VOL=${AOSP_VOL:-~/aosp-root}
16+
AOSP_VOL=${AOSP_VOL%/} # Trim trailing slash if needed
1417
AOSP_VOL_AOSP=${AOSP_VOL_AOSP:-$AOSP_VOL/aosp}
18+
AOSP_VOL_AOSP=${AOSP_VOL_AOSP%/} # Trim trailing slash if needed
1519
AOSP_VOL_CCACHE=${AOSP_VOL_CCACHE:-$AOSP_VOL/ccache}
20+
AOSP_VOL_CCACHE=${AOSP_VOL_CCACHE%/} # Trim trailing slash if needed
1621

17-
if [ ! -d "$AOSP_VOL_AOSP" -o ! -d "$AOSP_VOL_CCACHE" ]; then
18-
sudo mkdir -p $AOSP_VOL_AOSP $AOSP_VOL_CCACHE
19-
sudo chmod 777 $AOSP_VOL_AOSP $AOSP_VOL_CCACHE
20-
fi
22+
# Convenience function
23+
function aosp_create_dir_if_needed {
24+
directory=$1
25+
msg="aosp: Checking if $directory exists"
26+
echo "$msg"
27+
if [ ! -d "$directory" ]; then
28+
echo "$msg - unexistent"
29+
msg="Creating $directory"
30+
echo "$msg"
31+
mkdir -p $directory
32+
fi
33+
echo "$msg - ok"
34+
}
35+
36+
# Create AOSP_VOL_AOSP
37+
aosp_create_dir_if_needed $AOSP_VOL_AOSP
38+
aosp_create_dir_if_needed $AOSP_VOL_CCACHE
39+
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)"
2142

2243
if [ -n "$SSH_AUTH_SOCK" ]; then
2344
SSH_AUTH_ARGS="-v $SSH_AUTH_SOCK:/tmp/ssh_auth -e SSH_AUTH_SOCK=/tmp/ssh_auth"
2445
fi
2546

26-
docker run $AOSP_ARGS $SSH_AUTH_ARGS $AOSP_EXTRA_ARGS \
47+
echo ""
48+
49+
docker run $AOSP_ARGS $AOSP_HOST_ID_ARGS $SSH_AUTH_ARGS $AOSP_EXTRA_ARGS \
2750
-v "$AOSP_VOL_AOSP:/aosp" -v "$AOSP_VOL_CCACHE:/tmp/ccache" \
2851
$AOSP_IMAGE $@

utils/docker_entrypoint.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
# Default to 'bash' if no arguments are provided
41+
args="$@"
42+
if [ -z "$args" ]; then
43+
args="bash"
44+
fi
45+
46+
# Execute command as `aosp` user
47+
export HOME=/home/aosp
48+
exec sudo -u aosp $args

0 commit comments

Comments
 (0)