git - 无法 ssh 到另一个 Docker 容器 docker-compose

标签 git docker docker-compose dockerfile

我有一个 docker 镜像,我将其用作我的 git 服务器,其中包含我的所有数据。然后我有一个数据库容器,我需要从我的 git docker 服务器克隆数据。

我的 docker-compose.yml 文件是:

version: '3'

services:
  git-server:
    image: git:latest
    build:
      context: git
    ports:
      - "22:22"

  my-db:
    image: my-db:latest
    build:
      context: db
    ports:
      - "5433:5432"
    links:
      - git-server

对于 git docker 我只创建 git repos。
FROM kinogmt/centos-ssh:latest

......

RUN mkdir /home/git/dashboard.git
WORKDIR /home/git/dashboard.git
RUN git --bare init && git --bare fetch git@bitbucket.org:****/******.git
.......
EXPOSE 22

在我的数据库 dockerfile 中,我正在克隆那个 repo。
From centos:centos7

.......
RUN git clone git@git-server:dashboard.git
.......

当我运行 docker-compose build

它说

ssh: Could not resolve hostname git-server: Name or service not known fatal: Could not read from remote repository.

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

最佳答案

在构建时,容器不连接到桥接网络(也不连接到卷)。如果您需要容器到容器的网络,您需要将该工作流移到您在容器启动时运行的入口点或命令中。

使用 RUN 执行的命令在 Dockerfile 中是在构建时执行的。当 docker 构建你的镜像时,它不会与其他容器联网,没有安装卷,也没有对其他容器的任何依赖。它只是从前一行中获取先前的状态,创建一个运行 RUN 的临时容器。命令,并捕获生成的图层。因此尝试使用容器到容器网络将失败,因为其他容器在构建时不存在。如果可能,构建应该是可重现的并且独立于外部工件,因此需要重新设计。

作为重新设计,我会亲自从您的镜像步骤中删除 git clone,并将代码包含在您的 Dockerfile 所在的存储库中。在开始构建之前进行任何检查。这具有保留任何 secret 以登录到您的图像之外的私有(private)存储库的优势。然后在您的构建中,您只需执行 COPY将文件放入您的图像而不是 git fetch。

如果您的进程需要容器到容器的通信,另一个选择是在您的 CMD 中运行脚本。或 ENTRYPOINT运行容器时与构建镜像时所需的任何步骤。

关于git - 无法 ssh 到另一个 Docker 容器 docker-compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44494089/

相关文章:

git - 将 ssh key 添加到 Git 帐户

git - Bitbucket Pipeline - 使用 SSH key pull/推代码,无需拒绝权限

docker - magento中的 Grunt 不编译文件

ubuntu - 如何使用 docker 创建运行 docker 的最小实时 ISO?

node.js - 在两个Docker容器之间(Vue.js到Node.js)与HTTP通信

scala - 在 docker compose 文件中将环境设置为列表

wordpress - 使用Docker运行现有的Wordpress网站

linux - 创建显示较少删除行的补丁

python - 如何为 flask 和 celery 应用程序创建通用的基本docker镜像

git - TortoiseGit 从远程源 merge