Python docker容器中的SSH密钥

Python docker容器中的SSH密钥,python,docker,gitlab-ci,ssh-keys,Python,Docker,Gitlab Ci,Ssh Keys,我有一个连接到gitlab的脚本,如何将它正确地传输到容器中,以便它可以通过SSH连接到gitlab if not os.path.exists(os.path.join(PATH_SAVE_SCHEMA, '.git')): repo = Repo.init(PATH_SAVE_SCHEMA) origin = repo.create_remote('origin', GIT_REMOTE_URL) config_writer = repo

我有一个连接到gitlab的脚本,如何将它正确地传输到容器中,以便它可以通过SSH连接到gitlab

   if not os.path.exists(os.path.join(PATH_SAVE_SCHEMA, '.git')):
        repo = Repo.init(PATH_SAVE_SCHEMA)
        origin = repo.create_remote('origin', GIT_REMOTE_URL)
        config_writer = repo.config_writer()
        config_writer.set_value('user', 'name', GIT_USER_NAME)
        config_writer.set_value('user', 'email', GIT_USER_EMAIL)
        config_writer.set_value('http', 'sslverify', 'false')
        config_writer.release()
        origin.fetch()
        origin.pull(repo.refs[0].remote_head)
        repo.git.reset('--hard')
    else:
        repo = Repo(PATH_SAVE_SCHEMA)
        repo.git.reset('--hard')
        repo.remote('origin').pull('master')
现在我的容器正在运行,但它给出了一个错误

Cmd('git') failed due to: exit code(1)
cmdline: git pull -v origin master
stderr: 'fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'
如何在Docker文件中指定密钥的命令? 我需要在GitLab存储库中输入密钥吗

我的docker文件:

ARG SSH_PRIVATE_KEY
ARG SSH_PUBLIC_KEY
ARG SSH_KNOWN_HOSTS
RUN mkdir ~/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa
RUN echo "${SSH_PUBLIC_KEY}" > ~/.ssh/id_rsa.pub
RUN chmod 700 ~/.ssh/id_rsa
RUN chmod 700 ~/.ssh/id_rsa.pub
RUN echo "${SSH_KNOWN_HOSTS}" > ~/.ssh/known_hosts

您的帐户没有在任何地方提供密码

快速而肮脏的方法是在git url中指定密码:

:@github.com/.git

但是,您的密码将以明文形式出现在您的代码中。正确的方法是使用password.txt文件


更好的做法是使用
ssh\u密钥
并将其作为卷装载

在构建映像期间,您可以将ssh密钥复制到某个“tmp”构建阶段,从git下载代码,并在“main”构建阶段从“tmp”阶段复制该代码


最后,映像不应包含任何有关ssh密钥检查的信息。

几乎不可能在Dockerfile中安全地包含此类内容的凭据;最好从主机上运行这样的命令。作为一名开发人员,如果docker build序列尝试切换到另一个分支而不是构建我已经签出的分支,我也会感到惊讶。因为现在脚本在主机上运行,访问是在SSH密钥上进行的,但我不知道如何将此密钥放入容器中