5
votes

We are building a Docker Container on Docker Cloud. The build process requires git submodules.

To initialize the submodules for a local build we added the following line to the Dockerfile:

RUN git submodule update --init --recursive

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

Corresponding commit: https://github.com/open62541/open62541/commit/ee9c18a6a05722edfe7c0d8d8e140d802fa2e5f2 and Pull Request:
https://github.com/open62541/open62541/pull/3191

Note: In contrast to similar questions, all the submodules are public repos on github without authentication.

Situation without submodule init line:

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 .

Error:

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.

Situation with submodule init in Dockerfile:

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'
  • Build locally: Success
git clone https://github.com/open62541/open62541.git
cd open62541
# Commit which added git submodule init in Dockerfile
git checkout ee9c18a6a05722edfe7c0d8d8e140d802fa2e5f2
# This will succeed
docker build .

How are submodules in dockerfiles correctly initialized so that it works on Docker cloud, and at the same time one can just pull the main repo and build the docker container?


Related questions:

1

1 Answers

3
votes

See the answer I gave here: https://stackoverflow.com/a/59640438/1021344

Reproduced here for simplicity: You need to use hooks: https://docs.docker.com/docker-hub/builds/advanced/#custom-build-phase-hooks

TL;DR: Place this in 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