Skip to content

Instantly share code, notes, and snippets.

@xuanyuanaosheng
Created March 16, 2019 11:08
Show Gist options
  • Save xuanyuanaosheng/d25b181ace74e4a0d83e2ec563a931c4 to your computer and use it in GitHub Desktop.
Save xuanyuanaosheng/d25b181ace74e4a0d83e2ec563a931c4 to your computer and use it in GitHub Desktop.
容器启动脚本 在容器中挂载外部的应用并在容器启动的时候启动,参数传递使用环境变量 #bash #docker
nvidia-docker run -d --rm \
-e TZ=Asia/Shanghai \
-e AILAB_EXEC_SCRIPT="/etc/ailab-init/startup-vnc.sh#" \
-e USER_NAME=test \
-e LANG=zh_CN.UTF-8 \
-e RESOLUTION=1024x768x16 \
-v `pwd`/ailab-init:/etc/ailab-init \
--entrypoint bash \
-p 46080:8282 -p 45901:5900 \
-e VNC_PASSWORD=111111 \
test:v0.4 /etc/ailab-init/startup-env.sh
#!/bin/bash
debug()
{
if [ -n "$AILAB_DEBUG" ];then
echo "$@" >>/tmp/${PROG}.log
fi
}
add_user()
{
local opts=""
[ -n "$USER_HOME" ] && opts="$opts -md $USER_HOME"
[ -n "$USER_ID" ] && opts="$opts -u $USER_ID"
[ -n "$USER_SHELL" ] && opts="$opts -s $USER_SHELL"
[ -n "$GROUP_NAME" ] && opts="$opts -g $GROUP_NAME"
if ! grep -q $GROUP_NAME /etc/group ;then
groupadd -g $GROUP_ID $GROUP_NAME
fi
debug "add user: $USER_NAME"
useradd $opts $USER_NAME
debug "chpasswd: $USER_NAME"
echo $USER_NAME:$USER_PASSWD|chpasswd
debug "chpasswd: root"
echo root:$ROOT_PASSWD|chpasswd
mkdir -p /etc/sudoers.d
echo "$USER_NAME ALL=(ALL) NOPASSWD: ALL" >/etc/sudoers.d/$USER_NAME
}
clean_up()
{
unset ROOT_PASSWD
unset USER_NAME
unset USER_PASSWD
unset USER_HOME
unset USER_ID
unset USER_SHELL
unset GROUP_NAME
unset GROUP_ID
unset HOME_DIR
}
pre_exec()
{
add_user
}
exec_cmd()
{
#AILAB_EXEC_SCRIPT="date#ls -l"
OLD_IFS=$IFS
IFS='#' read -r -a exec_array <<< "$AILAB_EXEC_SCRIPT"
for i in "${exec_array[@]}";do
debug "execute custom process $i"
$i &
done
IFS=$OLD_IFS
debug "clean up enviroment variable"
clean_up
/usr/sbin/sshd -D
}
main()
{
pre_exec
exec_cmd
}
export AILAB_DEBUG="1"
PROG=$(basename $0)
export ROOT_PASSWD=${ROOT_PASSWD:-"p@ssw0rd"}
export USER_NAME=${USER_NAME:-"ailab"}
export USER_PASSWD=${USER_PASSWD:-"ailab"}
export HOME_DIR=${HOME_DIR:-"/home"}
export USER_HOME=${HOME_DIR}/$USER_NAME
export USER_ID=${USER_ID:-"1000"}
export USER_SHELL=${USER_SHELL:-"/bin/bash"}
export GROUP_NAME=${GROUP_NAME:-"student"}
export GROUP_ID=${GROUP_ID:-"1000"}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment