What will be the bat script syntax to find conatiner id and stop the docker container What will be the bat script syntax to find conatiner id and stop the docker container docker docker

What will be the bat script syntax to find conatiner id and stop the docker container


Your if syntax is slightly off. The command has to be on the same (logical) line as the if. Also the else and the command after it has to be on the same (logical) line. Use parentheses to start a code-block (the start of a command block is syntactically treated as a valid command):

for /f %%i in ('docker ps -qf "name=^demo-application"') do set containerId=%%iecho %containerId%If "%containerId%" == "" (  echo "No Container running") ELSE (  docker stop %ContainerId%  docker rm -f %ContainerId%)


I would do this, without having to set a variable:

for /f %%i in ('docker ps -qf "name=^demo-application"') do If not "%%i" == "" (    docker stop %%i    docker rm -f %%i  ) else (    echo "No Container running")