I came up with a couple of work-arounds of different complexity. They all rely on the assumption that ${IMAGE_TAG}
stores the customized tag that represents e.g. a build no. and we want to tag all services' images with this tag as well as with latest
.
grep
the image names from the docker-compose.yml
file
images=$(cat docker-compose.yml | grep 'image: ' | cut -d':' -f 2 | tr -d '"')
for image in $images
do
docker tag "${image}":"${IMAGE_TAG}" "${image}":latest
done
However, this is error prone if somebody adds a comment in docker-compose.yml
which would e.g. look like # Purpose of this image: do something useful...
.
Build twice
Use ${IMAGE_TAG}
as an environment variable in your docker-compose.yml
file as described here in the first example.
Then, simply run the build process twice, each time substituting ${IMAGE_TAG}
with a different value:
IMAGE_TAG="${IMAGE_TAG}" docker-compose build
IMAGE_TAG=latest docker-compose build
The second build process should be much faster than the first one since all image layers should still be cached from the first run.
Drawback of this approach is that it will flood your log output with two subsequent build processes for each single service which might make harder to search through it for something useful.
Besides, if you have any command in your Dockerfile
which always flushes the build cache (e.g. an ADD
command fetching from a remote location with auto-updating last-modified
headers, adding files which are constantly updated by an external process etc.) then the extra build might slow things down significantly.
Parse image names from the docker-compose.yml
file with some inline Python code
Using a real yaml
parser in Python (or any other language such as Ruby
or perl
or whatever is installed on your system) is more robust than the first mentioned grep
approach since it will not get confused by comments or strange but valid ways of writing the yml
file.
In Python, this could look like this:
images=$(python3 <<-EOF # make sure below to indent with tabs, not spaces; or omit the "-" before "EOF" and use no indention at all
import yaml
content = yaml.load(open("docker-compose.build.yml"))
services = content["services"].values()
image_names = (service["image"].split(":")[0] for service in services)
print("\n".join(image_names))
EOF
)
for image in ${images}
do
docker tag ${image}:${IMAGE_TAG} ${image}:latest
done
Drawback of this approach is that the machine executing the build has to have Python3 installed, along with the PyYAML library. As already mentioned, this pattern could similarly be used with Python2 or any other programming language that is installed.
Get image names with combination of some docker
commands
The following approach using some native docker
and docker-compose
commands (using go-templates) is a bit more complex to write but also works nicely.
# this should be set to something unique in order to avoid conflicts with other running docker-compose projects
compose_project_name=myproject.tagging
# create containers for all services without starting them
docker-compose --project-name "${compose_project_name}" up --no-start
# get image names without tags for all started containers
images=$(docker-compose --project-name "${compose_project_name}" images -q | xargs docker inspect --format='{{ index .RepoTags 0}}' | cut -d':' -f1)
# iterate over images and re-tag
for image in ${images}
do
docker tag "${image}":"${IMAGE_TAG}" "${image}":latest
done
# clean-up created containers again
docker-compose --project-name "${compose_project_name}" down
While this approach does not have any external dependencies and is more safe than the grep
method, it might take a few more seconds to execute on large setups for creating and removing the containers (typically not an issues though).