Exit Code and cleaning after failed pod creation
I am running podman-compose from a script and try to catch an error if compose up failes. I want to revert all changes done before the erronous command and write it to a log-file.
podman-compose -f compose.yml --env-file user.env -p test up -d --force-recreate >> $LOG_PATH 2>&1
if [ ! $? -eq 0 ]
then
stop
echo "$(date): Error while starting TEST" >> $LOG_PATH
exit 1
fi
But unfortuneatly the command only outputs an error and exits with 0.
podman-compose version: 1.0.4
['podman', '--version', '']
using podman version: 4.0.3-dev
** excluding: set()
['podman', 'ps', '--filter', 'label=io.podman.compose.project=test', '-a', '--format', '{{ index .Labels "io.podman.compose.config-hash"}}']
podman pod create --name=pod_test --infra=false --share=
b7f12c401a9826a678aa5faa8f91cf7e895b12bca515171646f1c9a1bc5c6aa6
exit code: 0
['podman', 'network', 'exists', 'test_default']
['podman', 'network', 'create', '--label', 'io.podman.compose.project=test', '--label', 'com.docker.compose.project=test', 'test_default']
['podman', 'network', 'exists', 'test_default']
podman run --name=hello-world_test -d --pod=pod_test --label io.podman.compose.config-hash=945b27045c070046422e24f930b835dfaf8c59cce35ce8237a2dd150387caba1 --label io.podman.compose.project=test --label io.podman.compose.version=1.0.4 --label com.docker.compose.project=test --label com.docker.compose.project.working_dir=/test --label com.docker.compose.project.config_files=/app-compose.yml --label com.docker.compose.container-number=1 --label com.docker.compose.service=hello-world -v /www:/www --net test_default --network-alias hello-world -p 2100:8000 -u 1002:1002 62c6de28ae4c835b0175ab9eb4e0f60b61256f44f1acdcf49b311c72fff2fca5
Error: 62c6de28ae4c835b0175ab9eb4e0f60b61256f44f1acdcf49b311c72fff2fca5: image not known
exit code: 125
podman start hello-world_test
Error: no container with name or ID "hello-world_test" found: no such container
exit code: 125
Is there any other way to catch or trap the error myself and call my "stop" to revert all changes?
Expected behavior podman-compose returns an exit code other than 0 if an error happens. \
Aditionally and optionally (would avoid lots of issues), podman-compose deletes pods, network and other things created/done before the failed command.
Actual behavior podman-compose always returns an exit code 0.
Output
$ podman-compose version
podman-compose version: 1.0.4
['podman', '--version', '']
using podman version: 4.0.3-dev
podman-composer version 1.0.4
podman --version
podman version 4.0.3-dev
Environment:
- OS: Linux
- podman version: 4.0.3-dev
- podman compose version: fbff315e18fb4c98d1d7a868fbcac25060b06694
this is the line you care about
podman run --name=hello-world_test .... 62c6de28ae4c835b0175ab9eb4e0f60b61256f44f1acdcf49b311c72fff2fca5
Error: 62c6de28ae4c835b0175ab9eb4e0f60b61256f44f1acdcf49b311c72fff2fca5: image not known
yes, but I expect an exit value from the whole compose. This command is just one line of one service.
And just to make sure, my issue is not the missing image, I did this on purpose for this example.
I have added the following to the end of the run function
if exit_code != 0:
sys.exit(exit_code)
It now returns the exit code, additionally to the printed line. This works as expected, but it also leaves the process without reverting anything that was done/created before the error and calling down also doesn't work for that purpose anymore. It also exits directly when trying to remove the non existent container.
So I am not a python developer, maybe someone with more knowledge can fix it the intended way.
this is not the proper solution, because non-zero exit status is expected when we assert things that already exists.
I know and I now get the idea why it's more complicated than I initially thought. The errors of podman are ignored on purpose (due to already existing asserts) and I have no Idea how to distinguishes between the good and the bad errors.
As a workaround in my script, I am checking for any output of the following command:
podman ps -a --format {{.ID}} --filter label=io.podman.compose.project=${PROJECT_NAME} | grep -q .
But if any of the container in the pod are working and an other isn't, this does fail as well.