【问题标题】:Cannot create or touch file inside LXC container无法在 LXC 容器内创建或触摸文件
【发布时间】:2019-05-28 07:50:53
【问题描述】:

您好,我的 LXC 容器有一些问题。如果我运行 .sh 脚本之一,它不会在容器中创建文件,但路径存在。有时,当我多次执行脚本时,它的工作,我不知道为什么。

结果: mkstemp: 没有这样的文件或目录 触摸:无法触摸'/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

【问题讨论】:

    标签: bash lxc


    【解决方案1】:

    失败的原因是因为在创建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 之前。

    【讨论】:

      猜你喜欢
      • 2022-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2018-06-24
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      相关资源
      最近更新 更多