Skip to content

Commit 2dc12d9

Browse files
committed
[SPARK-43370] Switch spark user only when run driver and executor
### What changes were proposed in this pull request? Switch spark user only when run driver and executor ### Why are the changes needed? Address doi comments: question 7 [1] [1] docker-library/official-images#13089 (comment) [2] docker-library/official-images#13089 (comment) ### Does this PR introduce _any_ user-facing change? Yes ### How was this patch tested? 1. test mannuly ``` cd ~/spark-docker/3.4.0/scala2.12-java11-ubuntu $ docker build . -t spark-test $ docker run -ti spark-test bash sparkafa78af05cf8:/opt/spark/work-dir$ $ docker run --user root -ti spark-test bash root095e0d7651fd:/opt/spark/work-dir# ``` 2. ci passed Closes: #44 Closes #43 from Yikun/SPARK-43370. Authored-by: Yikun Jiang <[email protected]> Signed-off-by: Yikun Jiang <[email protected]>
1 parent 9d4c98c commit 2dc12d9

File tree

8 files changed

+44
-22
lines changed

8 files changed

+44
-22
lines changed

3.4.0/scala2.12-java11-python3-r-ubuntu/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#
1717
FROM spark:3.4.0-scala2.12-java11-ubuntu
1818

19+
USER root
20+
1921
RUN set -ex; \
2022
apt-get update; \
2123
apt install -y python3 python3-pip; \
@@ -24,3 +26,5 @@ RUN set -ex; \
2426
rm -rf /var/lib/apt/lists/*
2527

2628
ENV R_HOME /usr/lib/R
29+
30+
USER spark

3.4.0/scala2.12-java11-python3-ubuntu/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
#
1717
FROM spark:3.4.0-scala2.12-java11-ubuntu
1818

19+
USER root
20+
1921
RUN set -ex; \
2022
apt-get update; \
2123
apt install -y python3 python3-pip; \
2224
rm -rf /var/cache/apt/*; \
2325
rm -rf /var/lib/apt/lists/*
26+
27+
USER spark

3.4.0/scala2.12-java11-r-ubuntu/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
#
1717
FROM spark:3.4.0-scala2.12-java11-ubuntu
1818

19+
USER root
20+
1921
RUN set -ex; \
2022
apt-get update; \
2123
apt install -y r-base r-base-dev; \
2224
rm -rf /var/cache/apt/*; \
2325
rm -rf /var/lib/apt/lists/*
2426

2527
ENV R_HOME /usr/lib/R
28+
29+
USER spark

3.4.0/scala2.12-java11-ubuntu/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ ENV SPARK_HOME /opt/spark
7777

7878
WORKDIR /opt/spark/work-dir
7979

80+
USER spark
81+
8082
ENTRYPOINT [ "/opt/entrypoint.sh" ]

3.4.0/scala2.12-java11-ubuntu/entrypoint.sh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ elif ! [ -z ${SPARK_HOME+x} ]; then
6969
SPARK_CLASSPATH="$SPARK_HOME/conf:$SPARK_CLASSPATH";
7070
fi
7171

72+
# Switch to spark if no USER specified (root by default) otherwise use USER directly
73+
switch_spark_if_root() {
74+
if [ $(id -u) -eq 0 ]; then
75+
echo gosu spark
76+
fi
77+
}
78+
7279
case "$1" in
7380
driver)
7481
shift 1
@@ -78,6 +85,8 @@ case "$1" in
7885
--deploy-mode client
7986
"$@"
8087
)
88+
# Execute the container CMD under tini for better hygiene
89+
exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}"
8190
;;
8291
executor)
8392
shift 1
@@ -96,20 +105,12 @@ case "$1" in
96105
--resourceProfileId $SPARK_RESOURCE_PROFILE_ID
97106
--podName $SPARK_EXECUTOR_POD_NAME
98107
)
108+
# Execute the container CMD under tini for better hygiene
109+
exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}"
99110
;;
100111

101112
*)
102113
# Non-spark-on-k8s command provided, proceeding in pass-through mode...
103-
CMD=("$@")
114+
exec "$@"
104115
;;
105116
esac
106-
107-
# Switch to spark if no USER specified (root by default) otherwise use USER directly
108-
switch_spark_if_root() {
109-
if [ $(id -u) -eq 0 ]; then
110-
echo gosu spark
111-
fi
112-
}
113-
114-
# Execute the container CMD under tini for better hygiene
115-
exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}"

Dockerfile.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ ENV SPARK_HOME /opt/spark
7777

7878
WORKDIR /opt/spark/work-dir
7979

80+
USER spark
81+
8082
ENTRYPOINT [ "/opt/entrypoint.sh" ]

entrypoint.sh.template

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ elif ! [ -z ${SPARK_HOME+x} ]; then
6969
SPARK_CLASSPATH="$SPARK_HOME/conf:$SPARK_CLASSPATH";
7070
fi
7171

72+
# Switch to spark if no USER specified (root by default) otherwise use USER directly
73+
switch_spark_if_root() {
74+
if [ $(id -u) -eq 0 ]; then
75+
echo gosu spark
76+
fi
77+
}
78+
7279
case "$1" in
7380
driver)
7481
shift 1
@@ -78,6 +85,8 @@ case "$1" in
7885
--deploy-mode client
7986
"$@"
8087
)
88+
# Execute the container CMD under tini for better hygiene
89+
exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}"
8190
;;
8291
executor)
8392
shift 1
@@ -96,20 +105,12 @@ case "$1" in
96105
--resourceProfileId $SPARK_RESOURCE_PROFILE_ID
97106
--podName $SPARK_EXECUTOR_POD_NAME
98107
)
108+
# Execute the container CMD under tini for better hygiene
109+
exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}"
99110
;;
100111

101112
*)
102113
# Non-spark-on-k8s command provided, proceeding in pass-through mode...
103-
CMD=("$@")
114+
exec "$@"
104115
;;
105116
esac
106-
107-
# Switch to spark if no USER specified (root by default) otherwise use USER directly
108-
switch_spark_if_root() {
109-
if [ $(id -u) -eq 0 ]; then
110-
echo gosu spark
111-
fi
112-
}
113-
114-
# Execute the container CMD under tini for better hygiene
115-
exec $(switch_spark_if_root) /usr/bin/tini -s -- "${CMD[@]}"

r-python.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#
1717
FROM spark:{{ SPARK_VERSION }}-scala{{ SCALA_VERSION }}-java{{ JAVA_VERSION }}-ubuntu
1818

19+
USER root
20+
1921
RUN set -ex; \
2022
apt-get update; \
2123
{%- if HAVE_PY %}
@@ -30,3 +32,5 @@ RUN set -ex; \
3032

3133
ENV R_HOME /usr/lib/R
3234
{%- endif %}
35+
36+
USER spark

0 commit comments

Comments
 (0)