I have the following script that i am hoping will build and publish to my azure container registry.
echo "env override file:${1}"
cd ../.devcontainer
docker compose --env-file .env --env-file $1 build
#parse image name and tag from above output here into $docker_image and $tag
docker image tag $docker_image:$tag mycontainerreg.azurecr.io/$docker_image:latest
docker push mycontainerreg.azurecr.io/$docker_image:latest
After the compose comand, the last output shown is :
=> => naming to docker.io/library/my_app:v1.0
I would like to be able to extract the image name and tag from that output to use in the subsequent docker commands. How can i do that?
Also what is best practice for tagging? i tag with a version number. but should i also re-tag as the latest version as im doing – as it would simplify downstream deploys?
for completeness my docker-compose relevant context:
services:
my_app:
container_name: my_app_${ENV}
image: "my_app:${VERSION:-local}"
env files are .env, env_dev, etc:
VERSION=v1.0
ENV=local
USER_ID=1000
GROUP_ID=1000
USER_NAME=user1
SVC_PORT=5000
DB_PORT=5434
2
Answers
Store the output in a variable, let it be output
Regarding tagging:
v1.1).
avoid using it for production.
when appropriate, but make sure to test thoroughly before deploying
to production.
I’d just copy the
image:
from the Compose file, and not try to parse Compose’s output. Compose’s environment-variable substitution is a subset of POSIX shell syntax and you should be able to use the exact same string format in your script.When you set both
build:
andimage:
in the Compose file, theimage:
sets the name of the built image, so a better approach here might be to just set the Composeimage:
to what you want. Then you candocker-compose push
the built image without having to manually extract its name.I’d suggest that tagging the image with a date stamp, source-control commit ID, or version string is a best practice. Fixed tags like
latest
can cause some problems, particularly if you’re using Kubernetes, since it becomes unclear which "latest" image you’re using – if the local system already has a "latest" image then the container system usually won’t check if the upstream registry has a newer one.