bashdockershellazure-devopsdocker-repository

push all Dockerfiles to ACR using bash


I'm trying to tag and push all my docker files into azure container repository by using bash script:

 path="./base-images"
dfname="test.azurecr.io"
for file in $path; do
 docker build . -t $dfname -f $file
 docker push $dfname/baseimage/
done

but I got an error:

failed to solve with frontend dockerfile.v0: failed to read dockerfile: read /var/lib/docker/tmp/buildkit-mount173864107/base-images: is a directory
invalid reference format

Why does it write a different path? All my docker files are inside another folder (base-images).


Solution

  • Your for file in $path is not giving you the individual files in that directory, which is what it looks like you're hoping for. It is just going to assign the one value bash sees (./base-images) to $file and give you one iteration based on that. That's why docker complains about base-images: is a directory: it is expecting a Dockerfile file, not a directory.

    To get bash to loop through all of the files in that directory you need to follow this answer from a few years ago and start with

    for file in "$path"/*; do
    

    I would also suggest calling your $path variable $dir or $directory so you're not confusing other maintainers of this code with the $PATH builtin variable.

    Shellcheck would also tell you to put your variables in quotes such as:

    docker build . -t "$dfname" -f "$file"