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

Viewed 6092

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)
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.

Related