Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify commit in docker-compose remote github build

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?

like image 459
melchoir55 Avatar asked Jan 24 '18 22:01

melchoir55


People also ask

Should you commit Docker compose?

You should basically never use docker commit . The standard approach is to describe how to build your images using a Dockerfile, and check that file into source control. You can push the built image to a registry like Docker Hub, and you can check out the original source code and rebuild the image.

Does Docker build automatically push?

Docker Hub can automatically build images from source code in an external repository and automatically push the built image to your Docker repositories.


1 Answers

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

like image 192
mkungla Avatar answered Sep 30 '22 05:09

mkungla