在Docker构建期间使用git子模块 [英] Using git submodule during Docker build

查看:356
本文介绍了在Docker构建期间使用git子模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有多项不同服务的应用程序。我正在使用docker和docker-compose来构建所有服务。但是,我需要在服务之间共享一些代码,尤其是ORM代码。我的问题是,当我尝试调用 git子模块更新--init 时,出现以下错误:

I am building an app that has a couple different services. I am using docker and docker-compose to build all of the services. However there is some code I need to share between the services, specifically the ORM code. My issue is that when I try to call git submodule update --init I get the following error:

Step 6/8 : RUN git submodule update --init
 ---> Running in 88e2dcfa5b36
fatal: Not a git repository: ../.git/modules/my-repo
ERROR: Service 'my-repo' failed to build: The command '/bin/sh -c git submodule update --init' returned a non-zero code: 128

我不确定为什么它在 ../ 中查找 .git 目录。我敢肯定我正确设置了 WORKDIR 。这是我的整个 Dockerfile

I'm not sure why it's looking in ../ for the .git directory. I'm pretty sure I set the WORKDIR correctly. Here is my whole Dockerfile

FROM node
WORKDIR /usr/src/my-repo
COPY package*.json ./
RUN npm install
COPY . .
RUN git submodule update --init
RUN git submodule foreach npm install
CMD ["npm", "start"]

此外,该子模块是一个私有存储库,我该如何使用SSH克隆它?我是否必须使用ssh密钥或其他内容设置docker?谢谢。

Also as a side note - the submodule is a private repository how would I clone it using SSH? Do I have to set up docker with my ssh key or something? Thanks.

编辑:同样,每个服务都是父项目的子模块。我认为这可能就是git在 ../ git / modules / my-repo 中寻找原因的原因,但我不确定如何解决这个问题。我的目录结构是这样的:

Also each of the services is a submodule of the parent project. I think that may be why git is looking in ../git/modules/my-repo but I'm not really sure how to get around this. My directory structure is like so:

parent-repo
--service-1
----orm
--service-2
----orm


推荐答案

总结如下:


  • 您有一个处理业务流程的目录

  • 在该目录中,您有多个项目,其中包含一个节点应用程序

  • 每个节点应用程序对orm库具有相同的依赖关系

我有一个类似的设置,没有使用子模块。这只是一个建议,但对于拥有约20个开发人员的团队来说,效果很好。您可以通过以下步骤修改当前设置:

I had a similar setup that did not use submodules. This is just a suggestion, but it worked very well for a team with about twenty developers. You can modify your current setup with these steps:


  • 保留业务流程目录

  • 删除所有子模块从该目录中

  • 现在将业务流程目录中的每个服务克隆为普通的git repo。将每个目录添加到父项目的.gitignore文件中。

  • 在每个子项目中创建一个Dockerfile来为该服务构建一个容器。

  • 在父目录中,为docker-compose文件中的每个项目创建一个条目

  • Keep the orchestration directory
  • Remove all submodules from that directory
  • Now clone each service inside the orchestration dir as a normal git repo. Add each directory to the .gitignore file of the parent project.
  • Create a Dockerfile in each sub project to build a container for just that service.
  • In the parent dir, create an entry for each project in the docker-compose file

现在您具有这样的结构

- orchestration/
  - .git/
  - .gitignore
  - docker-compose.yaml
  - service1/
    - .git/
    - Dockerfile
    - package.json
    - ...
  - service2/
    - .git/
    - Dockerfile
    - package.json
    - ...
  - service3/
    - .git/
    - Dockerfile
    - package.json
    - ...

您的 docker-compose .yaml 看起来像这样:

version: '3'

services:
  service1:
    build: ./service1
  service2:
    build: ./service2
  service3:
    build: ./service3

为每个服务提供orm代码:

To provide the orm code to each service:

当您现在调用 docker-compose up docker compose应该构建每个项目并启动它。如果任何服务需要访问其他服务,则该服务的URL只是docker-compose文件中提供的名称。例如。 service1要访问service2,则URL为 http:// serive1 / path

When you now call docker-compose up docker compose should build each project and start it up. If any service needs to access another service, the url to that service is just the name provided in the docker-compose file. E.g. service1 wants to access service2 then the url is http://serive1/path

您不必启动所有服务,可以根据需要启动它们,例如 docker-compose up service1 service3

You do not have to start all services, you can start them as you like, e.g. docker-compose up service1 service3.

要重建服务(例如,代码更改时),请调用:docker-撰写重启服务1

To rebuild a service (e.g. when the code changed) call: docker-compose restart service1

这篇关于在Docker构建期间使用git子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆