skip to Main Content

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


  1. 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.
    Login or Signup to reply.
  2. 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.

    docker tag my_app:${VERSION:-local} mycontainerreg.azurecr.io/$docker_image:latest
    

    When you set both build: and image: in the Compose file, the image: sets the name of the built image, so a better approach here might be to just set the Compose image: to what you want. Then you can docker-compose push the built image without having to manually extract its name.

    services:
      my_app:
        build: .
        image: mycontainerreg.azurecr.io/myapp:${VERSION:-latest}
        # should not usually need container_name:
    
    export VERSION=20230905
    docker-compose build
    docker-compose push
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search