Skip to content

Commit 394ca3b

Browse files
snjezal0rd
authored andcommitted
CHE-3264 - Starting the che server as an user
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
1 parent a7d4bb7 commit 394ca3b

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

dockerfiles/che/entrypoint.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,27 @@ init() {
226226
export CHE_DATA="/data"
227227
CHE_DATA_HOST=$(get_che_data_from_host)
228228

229+
CHE_USER=${CHE_USER:-root}
230+
export CHE_USER=$CHE_USER
231+
if [ "$CHE_USER" != "root" ]; then
232+
if [ ! $(getent group docker) ]; then
233+
echo "!!!"
234+
echo "!!! Error: The docker group doesn't exist."
235+
echo "!!!"
236+
exit 1
237+
fi
238+
export CHE_USER_ID=`id -u ${CHE_USER}`:`getent group docker | cut -d: -f3`
239+
sudo chown -R ${CHE_USER}:docker ${CHE_DATA}
240+
sudo chown -R ${CHE_USER}:docker ${CHE_HOME}
241+
fi
229242
### Are we going to use the embedded che.properties or one provided by user?`
230243
### CHE_LOCAL_CONF_DIR is internal Che variable that sets where to load
231244
if [ -f "/conf/che.properties" ]; then
232245
echo "Found custom che.properties..."
233246
export CHE_LOCAL_CONF_DIR="/conf"
247+
if [ "$CHE_USER" != "root" ]; then
248+
sudo chown -R ${CHE_USER}:docker ${CHE_LOCAL_CONF_DIR}
249+
fi
234250
else
235251
echo "Using embedded che.properties... Copying template to ${CHE_DATA_HOST}/conf."
236252
mkdir -p /data/conf

dockerfiles/launcher/launcher_funcs.sh

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,44 @@ docker_run() {
110110
-v "$CHE_DATA_LOCATION" \
111111
-p "${CHE_PORT}":"${CHE_PORT}" \
112112
--restart="${CHE_RESTART_POLICY}" \
113-
--user="${CHE_USER}" \
114113
-e "CHE_LOG_LEVEL=${CHE_LOG_LEVEL}" \
115114
-e "CHE_IP=$CHE_HOST_IP" \
116115
--env-file=$ENV_FILE \
117116
"$@"
118-
119117
rm -rf $ENV_FILE > /dev/null
120118
}
121119

120+
get_user_id() {
121+
CHE_USER_UID=$(docker run -t \
122+
-v /etc/passwd:/etc/passwd:ro,Z \
123+
-v /etc/group:/etc/group:ro,Z \
124+
alpine id -u ${CHE_USER})
125+
CHE_USER_GID=$(docker run -t \
126+
-v /etc/passwd:/etc/passwd:ro,Z \
127+
-v /etc/group:/etc/group:ro,Z \
128+
alpine getent group docker | cut -d: -f3)
129+
echo -n "${CHE_USER_UID}" | tr '\r' ':'; echo -n ${CHE_USER_GID}
130+
}
131+
132+
docker_run_with_che_user() {
133+
if [ "${CHE_USER}" != "root" ]; then
134+
docker_run -e CHE_USER=${CHE_USER} \
135+
-v /etc/group:/etc/group:ro,Z \
136+
-v /etc/passwd:/etc/passwd:ro,Z \
137+
--user=$(get_user_id) \
138+
"$@"
139+
else
140+
docker_run --user="${CHE_USER}" "$@"
141+
fi
142+
}
143+
122144
docker_run_if_in_vm() {
123145
# If the container will run inside of a VM, additional parameters must be set.
124146
# Setting CHE_IN_VM=true will have the che-server container set the values.
125147
if is_docker_for_mac || is_docker_for_windows || is_boot2docker; then
126-
docker_run -e "CHE_IN_VM=true" "$@"
148+
docker_run_with_che_user -e "CHE_IN_VM=true" "$@"
127149
else
128-
docker_run "$@"
150+
docker_run_with_che_user "$@"
129151
fi
130152
}
131153

0 commit comments

Comments
 (0)