linux - 从这个 bash 脚本启动 docker 并保持运行?
问题描述
我试图了解如何防止下面的 bash 脚本在开始运行 docker 并执行一些初始命令后退出。之后我希望能够在终端进行交互。相反,它只是退出。我认为我要弄清楚的部分是在脚本的末尾,但为了清楚起见,我添加了整个脚本。
我对 shell 脚本和 docker 的了解有限。我的理解是这里有两个函数可以帮助将适当的文件夹从我自己的机器目录映射到 docker 目录,并附加一些我需要的 ENV 变量,以便导入一些文件夹和 python 模块。
到目前为止我已经尝试过:
- 在终端运行脚本时添加了 -d -it。
- 在此文件末尾的 docker run 命令中添加了 -d -it 作为 OPTIONS。
- 删除脚本末尾的退出
我希望能解释一下为什么方法 2 不起作用。
#!/bin/bash
#################################################
# Shell script that simulates JModelica using
# a docker image of JModelica.
#
# The main purpose of this script is to export
# MODELICAPATH and PYTHONPATH with their values
# updated for the docker, and to mount the
# required directories.
#################################################
set -e
IMG_NAME=ubuntu-1804_jmodelica_trunk
DOCKER_USERNAME=michaelwetter
# Function declarations
function create_mount_command()
{
local pat="$1"
# Each entry in pat will be a mounted read-only volume
local mnt_cmd=""
for ele in ${pat//:/ }; do
mnt_cmd="${mnt_cmd} -v ${ele}:/mnt${ele}:ro"
done
# On Darwin, the exported temporary folder needs to be /private/var/folders, not /var/folders
# see https://askubuntu.com/questions/600018/how-to-display-the-paths-in-path-separately
if [ `uname` == "Darwin" ]; then
mnt_cmd=`echo ${mnt_cmd} | sed -e 's| /var/folders/| /private/var/folders/|g'`
fi
echo "${mnt_cmd}"
}
function update_path_variable()
{
# Prepend /mnt/ in front of each entry of a PATH variable in which the arguments are
# separated by a colon ":"
# This allows for example to create the new MODELICAPATH
local pat="$1"
local new_pat=`(set -f; IFS=:; printf "/mnt%s:" ${pat})`
# Cut the trailing ':'
new_pat=${new_pat%?}
echo "${new_pat}"
}
# Export the MODELICAPATH
if [ -z ${MODELICAPATH+x} ]; then
MODELICAPATH=`pwd`
else
# Add the current directory to the front of the Modelica path.
# This will export the directory to the docker, and also set
# it in the MODELICAPATH so that JModelica finds it.
MODELICAPATH=`pwd`:${MODELICAPATH}
fi
# Create the command to mount all directories in read-only mode
# a) for MODELICAPATH
MOD_MOUNT=`create_mount_command ${MODELICAPATH}`
# b) for PYTHONPATH
PYT_MOUNT=`create_mount_command ${PYTHONPATH}`
# Prepend /mnt/ in front of each entry, which will then be used as the MODELICAPATH
DOCKER_MODELICAPATH=`update_path_variable ${MODELICAPATH}`
DOCKER_PYTHONPATH=`update_path_variable ${PYTHONPATH}`
# If the current directory is part of the argument list,
# replace it with . as the docker may have a different file structure
cur_dir=`pwd`
bas_nam=`basename ${cur_dir}`
arg_lis=`echo $@ | sed -e "s|${cur_dir}|.|g"`
# Set variable for shared directory
sha_dir=`dirname ${cur_dir}`
# Check if the python script should be run interactively (if -i is specified)
while [ $# -ne 0 ]
do
arg="$1"
case "$arg" in
-i)
interactive=true
DOCKER_INTERACTIVE=-t
;;
esac
shift
done
# --user=${UID} \
docker run \
--user=${UID} \
-i \
$DOCKER_INTERACTIVE \
--detach=false \
${MOD_MOUNT} \
${PYT_MOUNT} \
-v ${sha_dir}:/mnt/shared \
-e DISPLAY=${DISPLAY} \
-v /tmp/.X11-unix:/tmp/.X11-unix \
--rm \
${DOCKER_USERNAME}/${IMG_NAME} /bin/bash -c \
"export MODELICAPATH=${DOCKER_MODELICAPATH}:/usr/local/JModelica/ThirdParty/MSL && \
export PYTHONPATH=${DOCKER_PYTHONPATH} && \
cd /mnt/shared/${bas_nam}"
exit $?
解决方案
推荐阅读
- python - 将字节写入二进制文件和将解码的字符串写入 unicode 文件之间的区别?
- sqlite - SQLite JDBC 3.30.1(最新)不支持 java.time
- laravel - 将 Typescript 与 Laravel 和 Vue.js 一起使用,导入不使其成为 Vue 组件?
- pandas - 如何使用 .loc 为 pandas 数据框按索引设置值?
- python - Python中的算术移位
- python - 使用 OpenCV 从桌面游戏卡图像中提取艺术品
- python - 在 Property 类创建代码中 self 指的是什么?
- python - bash:django-admin:找不到命令
- python - 在 python 中读取 CSV 文件时保留引号
- c++ - 使用单个 if 条件表示真值表