I am trying to use a simple bash while loop to build some podman images. The while loop reads lines from a file to get the image names and uses them to create image tag names and build context paths.
File containing container image names one per line in cwd of script ./containers
e.g:
cont1
cont2
cont3
The loop works fine if I just read the file and echo out the created path names in my loop:
#!/bin/bash
containers_basedir=/path/to/container_dirs/
while read container; do
echo "${containers_basedir}${container}/context"
done < containers
output:
/path/to/container_dirs/cont1/context
/path/to/container_dirs/cont2/context
/path/to/container_dirs/cont3/context
As soon as I add the actual podman build command into my loop things get strange. When I run the script, it will build the first container and exit. I run it again, it will run the build command on the first container (quick since its already built) then do the second container and exit. Run script again and it will do first, second, third. Run it again and it will do first, second, third, fourth etc.
Here is my script with the podman build line added
#!/bin/bash
containers_basedir=/path/to/container_dirs/
while read container; do
echo "${containers_basedir}${container}/context"
podman build -q -t ${container} ${containers_basedir}${container}/context
done < containers
What is going on here?
EDIT: Here is what the output looks like when I add set -x just above the while loop as suggested by comment from @berndbausch
No podman comannd, just echo:
+ read container
+ echo /path/to/container_dirs/cont1/context
/path/to/container_images/cont1/context
+ read container
+ echo /path/to/container_dirs/cont2/context
/path/to/container_images/cont2/context
+ read container
+ echo /path/to/container_dirs/cont3/context
/path/to/container_images/cont3/context
+ read container
Podman added, first run:
+ read container
+ echo /path/to/container_dirs/cont1/context
/path/to/container_dirs/cont1/context
+ podman build -q -t cont1 /path/to/container_dirs/cont1/context
<---build output omitted--->
+ read container
And here is the second run output with podman added.....
+ read container
+ echo /path/to/container_dirs/cont1/context
/path/to/container_dirs/cont1/context
+ podman build -q -t cont1 /path/to/container_dirs/cont1/context
<---build output omitted--->
+ read container
+ echo /path/to/container_dirs/cont2/context
/path/to/container_dirs/cont2/context
+ podman build -q -t cont2 /path/to/container_dirs/cont2/context
<---build output omitted--->
+ read container
set -x
before the while loop. This enables debug mode so that each command is printed when it is executed. – berndbausch Jan 27 '21 at 00:43podman
likely consuming stdin since the redirection is applied to the loop as a whole. – muru Jan 27 '21 at 02:22