如何在 Dockerfile 中为 Docker Cloud 正确初始化 git 子模块

Ste*_*ter 5 git docker dockerhub dockerfile docker-cloud

我们正在 Docker Cloud 上构建一个 Docker 容器。构建过程需要 git 子模块。

为了初始化本地构建的子模块,我们在 Dockerfile 中添加了以下行:

RUN git submodule update --init --recursive
Run Code Online (Sandbox Code Playgroud)

参见:https : //github.com/open62541/open62541/blob/master/Dockerfile#L9

对应commit:https : //github.com/open62541/open62541/commit/ee9c18a6a05722edfe7c0d8d8e140d802fa2e5f2 和Pull Request:https :
//github.com/open62541/open62541/pull/3191

注意: 与类似问题相比,所有子模块都是 github 上的公共存储库,无需身份验证。

没有子模块初始化行的情况:

RUN git submodule update --init --recursive
Run Code Online (Sandbox Code Playgroud)

错误:

CMake Error at CMakeLists.txt:830 (message):
  File /opt/open62541/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml not found.
  You probably need to initialize the git submodule for deps/ua-nodeset.
Run Code Online (Sandbox Code Playgroud)

Dockerfile 中子模块 init 的情况:

Step 7/18 : RUN git submodule update --init --recursive
---> Running in b358c21c4d53
fatal: not a git repository: /src/b6tohshrfzzntavvhek3zna/.git/modules/deps/mdnsd
Unable to find current revision in submodule path 'deps/mdnsd'
Run Code Online (Sandbox Code Playgroud)
  • 在本地构建:成功
git clone https://github.com/open62541/open62541.git
cd open62541
# Parent commit without git submodule update
git checkout e97abd591a159ce894488d93796b858d9f0d00b9
# This will fail because the submodules are obviously not initialized
docker build .
Run Code Online (Sandbox Code Playgroud)

如何正确初始化 dockerfiles 中的子模块,以便它可以在 Docker 云上运行,同时可以只拉主 repo 并构建 docker 容器?


相关问题:

小智 3

请参阅我在这里给出的答案:https ://stackoverflow.com/a/59640438/1021344

为了简单起见,在此转载:您需要使用钩子:https://docs.docker.com/docker-hub/builds/advanced/#custom-build-phase-hooks

TL;DR:将其放入hooks/post_checkout

#!/bin/bash
# Docker hub does a recursive clone, then checks the branch out,
# so when a PR adds a submodule (or updates it), it fails.
git submodule update --init
Run Code Online (Sandbox Code Playgroud)