Issue
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
Solution
Store the output in a variable, let it be output
output="=> => naming to docker.io/library/my_app:v1.0"
image_and_tag=$(echo "$output" | awk -F '/' '{print $NF}')
image_name=$(echo "$image_and_tag" | awk -F ':' '{print $1}')
image_tag=$(echo "$image_and_tag" | awk -F ':' '{print $2}')
echo "Image Name: $image_name"
echo "Image Tag: $image_tag"
Regarding tagging:
- Use specific version tags for production deployments (e.g., v1.0, v1.1).
- Use a 'latest' tag for testing, development, and internal use, but avoid using it for production.
- Periodically update the 'latest' tag to the latest stable version when appropriate, but make sure to test thoroughly before deploying to production.
Answered By - Abd allah Khateeb Answer Checked By - Willingham (WPSolving Volunteer)