Francesco Borzi
Francesco Borzi

Reputation: 61874

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)

Upvotes: 11

Views: 10733

Answers (1)

VonC
VonC

Reputation: 1324957

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.

Upvotes: 6

Related Questions