docker - Ansible Docker 容器 - 克隆私有(private)仓库

标签 docker ansible

我正在尝试从 Docker 容器(使用 Ansible)中的 BitBucket 克隆一个私有(private)存储库。我只是想尝试让它工作,所以我将我的公钥和私钥复制到容器中。然后我运行以下命令(FWICT,这是 Ansible 命令的简化版本):

docker exec -i web git clone git@bitbucket.org:user/repo.git

我明白了:
Cloning into 'repo'...
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

作为旁注,如果我运行:
docker exec -i -t web git clone git@bitbucket.org:user/repo.git

我收到一个 TTY,并提示我输入 SSH 私钥密码(似乎无法使用 Ansible 执行此操作),并且 repo 被克隆。

所以问题是,如何在没有 -t 的情况下克隆 Docker 容器中的私有(private)存储库? ?或者有谁知道如何使用 Ansible 在容器中克隆私有(private)仓库?

最佳答案

我设法通过在我的任务中使用 SSH 代理转发 ( http://dchua.com/2016/01/15/ssh-agent-forwarding-with-your-docker-container ) 找到解决方法,如下所示:

- set_fact:
    ssh_auth_sock: "{{ lookup('env','SSH_AUTH_SOCK') }}"

- name: Create container
  docker_container:
    name: "my_container"
    image: "my_image"
    ports: 
      - 80
    volumes:
      - "{{ playbook_dir }}/www:/var/www"
      - "{{ ssh_auth_sock }}:/ssh-agent"
    env:
      SSH_AUTH_SOCK: /ssh-agent

- name: Add container to inventory
  add_host:
    name: "web"
    ansible_connection: docker

- name: Clone Repository
  git:
    repo: "git@example.com:user/repo.git"
    dest: "/var/www/html"
    accept_hostkey: true
  delegate_to: "web"

关于docker - Ansible Docker 容器 - 克隆私有(private)仓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38748439/

相关文章:

ansible - 如何在 Ubuntu 18.04 上完全删除 Ansible 2.8.3

ansible - Saltstack 中的 register(Ansible) 相当于什么?

string - 如何将配置文件(行)转换为 Ansible 中的字典列表

python - 如何辨别我的 Python 模块是否被 Ansible 调用?

docker-compose 获取 docker 的 ID

node.js - 从 docker 容器和 Node 使用 websocket 时出现问题

python - 无法从 Docker 容器访问本地主机

git - 将 ssh 代理套接字转发到 docker build

docker - 如何在 docker-compose 中创建基础镜像

python - 如何从 Ansible python playbook api 获取远程标准输出