-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-entrypoint.sh
executable file
·66 lines (51 loc) · 1.87 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
set +e
ROBOT_USER_NAME="${ROBOT_USER_NAME:-robot}"
ROBOT_USER_ID="${ROBOT_USER_ID:-1000}"
ROBOT_GROUP_NAME="${ROBOT_GROUP_NAME:-robot}"
ROBOT_GROUP_ID="${ROBOT_GROUP_ID:-1000}"
DOCKER_GROUP_NAME="${DOCKER_GROUP_NAME:-docker}"
DOCKER_HOST="${DOCKER_HOST:-/var/run/docker.sock}"
setup_robot_user() {
if [[ "${ROBOT_GROUP_ID}" -ne 0 ]]; then
getent group "${ROBOT_GROUP_NAME}" &>/dev/null
if [[ $? -ne 0 ]]; then
echo "Creating robot group: ${ROBOT_GROUP_NAME}"
groupadd --gid "${ROBOT_GROUP_ID}" "${ROBOT_GROUP_NAME}" \
|| { echo "Failed to create robot user: ${ROBOT_USER_NAME}"; exit 1; }
else
echo "Robot group already exists: ${ROBOT_GROUP_NAME}"
fi
else
ROBOT_GROUP_NAME="root"
fi
if [[ "${ROBOT_USER_ID}" -ne 0 ]]; then
id -u "${ROBOT_USER_NAME}" &>/dev/null
if [[ $? -ne 0 ]]; then
echo "Creating robot user: ${ROBOT_USER_NAME}"
useradd --create-home --shell /bin/bash \
--uid "${ROBOT_USER_ID}" \
--gid "${ROBOT_GROUP_NAME}" "${ROBOT_USER_NAME}" \
|| { echo "Failed to create robot user: ${ROBOT_USER_NAME}"; exit 1; }
fi
else
ROBOT_USER_NAME="root"
fi
groups "${ROBOT_USER_NAME}" | grep "\\b${DOCKER_GROUP_NAME}\\b" &>/dev/null
if [[ $? -ne 0 ]]; then
echo "Adding robot user to group: ${DOCKER_GROUP_NAME}"
usermod -a -G "${DOCKER_GROUP_NAME}" "${ROBOT_USER_NAME}"
fi
}
setup_docker() {
if [[ ! -S "${DOCKER_HOST}" ]]; then
echo "Docker socket file does not exist or is not a socket: ${DOCKER_HOST}"
ls -la "${DOCKER_HOST}"
exit 1
fi
STAT=( $(stat -Lc "%g" "${DOCKER_HOST}") )
groupmod -g "${STAT[0]}" "${DOCKER_GROUP_NAME}"
}
setup_robot_user
setup_docker
su "${ROBOT_USER_NAME}" -c "$*"