10

I want to build services using Dockerfiles in remote projects found on github. This is for an end-to-end testing framework, so I need to be able to specify specific commits I want it to build. I am having a lot of difficulty trying to figure out how to pull a specific commit.

I have a docker compose file which looks something like this:

version: '3'
services:
  web:
    build: https://${GITHUB_ACCESS}:@github.com/mycompany/web.git#${COMMIT_SHA}

The above works fine if I omit #${COMMIT_SHA}. Unfortunately, if I include the sha for the specific commit I care about, I get the following error:

ERROR: error fetching: error: no such remote ref <commit sha>
: exit status 128

I am certain the sha exists, and I have tried it with a few others just to be sure. Am I getting the syntax wrong here, or does docker-compose not support referring to a specific commit?

tgogos
  • 23,218
  • 20
  • 96
  • 128
melchoir55
  • 6,842
  • 7
  • 60
  • 106
  • 3
    The docs only mention using branches and tags for builds using github repos as their build context. Maybe worth a shot tagging the specific commit in git and using it as the reference instead of the commit SHA. Have a look here for more info: https://docs.docker.com/engine/reference/commandline/build/#git-repositories and here: https://github.com/docker/compose/issues/3038 – Pitt Jan 25 '18 at 04:46
  • Does the URL work in a browser? – Thorbjørn Ravn Andersen Dec 27 '18 at 06:45

1 Answers1

2

As you get err ERROR: error fetching: error: no such remote ref <commit sha>: exit status 128 then most certainly you have syntax issue. In your case you have few possible causes.

  • GITHUB_ACCESS || GIT_COMMIT_SHA environment variable is not set
  • GIT_COMMIT_SHA is set to invalid sha value.

    I am certain the sha exists

    Just double check

You can configure Git URLs in their fragment section separated by a colon :. The first part represents the reference that Git will check out, this can be either a branch, a tag, or a commit SHA. So yes docker-compose does support referring to a specific commit. The second part represents a sub directory inside the repository that will be used as a build context. This is only needed when your docker file is not in repository root. Valid Git URL schemas can be anything what Git considers natively as valid. See this file found in its core for reference.

  • If your Dockerfile is not in repository root you would get ERROR: Cannot locate specified Dockerfile: Dockerfile

I'm using nginxinc/docker-nginx repository as example. There are multiple docker files in this repository, so let's say we want to build image based on Dockerfile found in repository subdirectory stable/alpine

Example 1

In this example lets build docker image from master branch

version: '3'
services:
  web:
    image: web:latest
    build: https://github.com/nginxinc/docker-nginx.git#master:stable/alpine

Example 1 - Do build it based on specific commit

I add .env file while most likely your variables are set in your Continuous Integration environment.

GIT_COMMIT_SHA=d377983a14b214fcae4b8e34357761282aca788f

and change our docker-compose.yaml to

version: '3'
services:
  web:
    image: web:${GIT_COMMIT_SHA}
    build: https://github.com/nginxinc/docker-nginx.git#${GIT_COMMIT_SHA}:stable/alpine

here resulting build: url should be https://github.com/nginxinc/docker-nginx.git#d377983a14b214fcae4b8e34357761282aca788f:stable/alpine

mkungla
  • 3,390
  • 1
  • 26
  • 37