-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·92 lines (75 loc) · 2.77 KB
/
entrypoint.sh
File metadata and controls
executable file
·92 lines (75 loc) · 2.77 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash -e
#==============================================#
# Script to support the initialization process #
# in a docker container. #
#==============================================#
#set -xv
# Executable to which cl arguments should fit
_SHELL='/bin/bash'
EXECAPP="${EXECAPP:-$_SHELL}"
# Save the input cl arguments
DARGS="$*"
# Save the current path
CWD="$PWD"
function config_user()
{
#================================================#
# Config a User #
# ------------- #
# It is meant to run during container's init #
# process. Such process is necessary to better #
# exchange files/bus between host/container. #
# Variables DOCKER_USER, DOCKER_UID, DOCKER_GID #
# are read from the environment; #
# if NOUSER is set, do nothing. #
#================================================#
DEFAULT_USER="user"
DEFAULT_UID="1000"
DEFAULT_GID="100"
[[ -n "$NOUSER" ]] && \
return 0 # nothing to be done here
DOCKER_USER="${DOCKER_USER:-$DEFAULT_USER}"
DOCKER_UID="${DOCKER_UID:-$DEFAULT_UID}"
DOCKER_GID="${DOCKER_GID:-$DEFAULT_GID}"
id $DOCKER_USER &> /dev/null || useradd -u "$DOCKER_UID" \
-g "$DOCKER_GID" \
-d "/home/$DOCKER_USER" -m \
-s /bin/bash \
"$DOCKER_USER"
echo "$DOCKER_USER"
return 0
}
# Add a user here
USERNAME=$(config_user)
# If no user created, use the current one (root by default)
id "$USERNAME" 2> /dev/null || USERNAME="$USER"
# Garantee the user will run on a proper place.
# WORKDIR is the dir where the user will run from.
[[ -z "$WORKDIR" ]] && export WORKDIR='/work'
# Verify WORKDIR existence
[[ ! -d "$WORKDIR" ]] && mkdir -p $WORKDIR
# And grant permissions
# To simplify the permissions now, I'll give ownership.
#TODO: give 'w/r/x' permissions instead of changing ownership;
# this is important 'cause WORKDIR could already exist.
chown ${USERNAME}: $WORKDIR && chmod -R u+wrx $WORKDIR
USERID=$(id -u $USERNAME)
GROUPID=$(id -g $USERNAME)
echo ""
echo "#====================================================#"
echo " This container is running: $EXECAPP"
echo " with arguments: $DARGS"
echo ""
echo " by user: '${USERNAME} (uid:$USERID,gid:$GROUPID)'."
echo "#====================================================#"
echo ""
if [ "$EXECAPP" != "$_SHELL" ]; then
su -l $USERNAME -c "cd $WORKDIR && $EXECAPP $DARGS"
else
if [ -z "$DARGS" ]; then
#cd $WORKDIR && su $USERNAME
su -l $USERNAME
else
su -l $USERNAME -c "cd $WORKDIR && $DARGS"
fi
fi