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)
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.
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.
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.
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 .
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 whichdocker-compose
is run.
Any hope of running a command directly in the docker-compose.yml
file was ruled out with docker/compose
issue 4081.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With