Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the (short) git commit hash to tag image versions in Docker Compose file

From the official Docker Compose build doc:

If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image:

Example:

build: 
   ...
image: myapp:tag

but I would like to replace tag with the output of git rev-parse --short HEAD.

Ideally I need something like:

image: myapp:$(git rev-parse --short HEAD)
like image 640
Francesco Borzi Avatar asked Jan 14 '19 13:01

Francesco Borzi


People also ask

What is git Short commit hash?

A short git commit hash is an abbreviation of the hash to the first 7 characters, it is almost certainly unique within a repository and git will increase the number of characters used if it is not.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.

Can Docker compose build a docker image?

Define and run multi-container applications with Docker. docker-compose build : This command builds images in the docker-compose. yml file. The job of the build command is to get the images ready to create containers, so if a service is using the prebuilt image, it will skip this service.

Which docker command is used to map a custom tag for the image?

Now, if you want to build the Image with a custom tag called my-ubuntu, you can use the following command. sudo docker build -t tag-demo:my-ubuntu .


1 Answers

The build ARGS section illustrates jonrsharpe's comment

You need to set an environment variable first, and declare the ARGS in your docker-compose.yml

ARG commit
...
image: "myapp:${commit}"

See "variable substitution" and also The “env_file” configuration option

Your configuration options can contain environment variables.
Compose uses the variable values from the shell environment in which docker-compose is run.

Any hope of running a command directly in the docker-compose.yml file was ruled out with docker/compose issue 4081.

like image 69
VonC Avatar answered Nov 10 '22 04:11

VonC