嗨,我的LXC容器有问题。如果我运行.sh脚本一个,它不会在容器中创建文件,但是路径存在。有时,当我多次执行脚本时,它的运行正常,我不知道为什么。
结果: mkstemp:没有这样的文件或目录 touch:无法触摸“ /root/.ssh/config”:没有此类文件或目录 / bin / sh:13:无法创建/root/.ssh/config:目录不存在
我的脚本:
# Lets define some variables.
export CONTAINER="cv-app-backend-5"
export SSH_SRC_PATH="./ssh"
export SSH_DST_PATH="/root/"
export SSH_CONFIG_PATH="/root/.ssh/config"
export GIT_HOST="host"
export GIT_URI="git@git.git"
export APP_PATH="/root/app"
export SSH_PATH="/root/ssh/id_rsa"
export DB_USER="postgres"
export DB_PASSWORD="postgres"
#
# Nothing below this point should need to be modified.
#
# Create a default Debian container.
lxc launch 'images:debian/9' ${CONTAINER}
lxc config device add ${CONTAINER} http proxy \
listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80
lxc config device add ${CONTAINER} https proxy \
listen=tcp:0.0.0.0:443 connect=tcp:127.0.0.1:443
lxc config device add ${CONTAINER} http proxy \
listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:8080
lxc file push -r ${SSH_SRC_PATH} ${CONTAINER}${SSH_DST_PATH}
# Run a script in the container
cat <<EOF | lxc exec ${CONTAINER} -- /bin/sh
echo "In container, phase 1"
apt-get update
apt-get -y install sudo git
# Clone project repository
pwd
ls -la
rm -rf ${APP_PATH}
ssh-keygen -R ${GIT_HOST}
touch ${SSH_CONFIG_PATH}
echo "Host ${GIT_HOST}\n\tStrictHostKeyChecking no\n" >> ${SSH_CONFIG_PATH}
ssh-agent /bin/sh -c "ssh-add ${SSH_PATH}; git clone ${GIT_URI} ${APP_PATH}"
cd ${APP_PATH}
#In repository
git checkout develop
ssh-agent /bin/sh -c "ssh-add ${SSH_PATH}; git pull"
#git reset --hard
EOF
答案 0 :(得分:0)
失败的原因是因为当您创建debian / 9容器时,/ root / .ssh不存在。因此,您无法将文件复制到其中。我看不到您发布的脚本中的任何内容会对创建它产生副作用。但是,例如,如果有其他事情只是做一个“ ssh-keygen”,那就可以了。
最简单的解决方法是
lxc-exec -n ${CONTAINER} -- mkdir -p /root/.ssh
lxc-exec -n ${CONTAINER} -- chmod 700 /root/.ssh
在lxc文件推送SSH_SRC_PATH之前。