Home > Software design >  cmd: Open a browser when a string is output using docker
cmd: Open a browser when a string is output using docker

Time:12-01

I'm running a docker command from inside cmd (a .bat script). The command looks like docker run --shm-size=1gb -it --privileged --name test p 8080:8080 -h test thom/test:latest I want to make a loop that waits for a certain string to appear after I invoke this command, or it waits for the port (8080) to be open. The string is "Daemon started!", and when it is output, I want to open a browser:

explorer "http://localhost:8080"

I'm struggling to make a while loop in batch though without restarting the docker command. This is what I have so far.

:loop
timeout /t 5
(docker run --shm-size=1gb -it --privileged --name test p 8080:8080 -h test thom/test:latest | find "proxy Daemon started!")  > nul 2>&1
if find "proxy Daemon started!" goto loop
echo I can go!
explorer "http://localhost:8080

 

CodePudding user response:

Use findstr and the resulting errorlevel to perform the actions accodringly, you can also use conditional operators && and || as seen in the second example:

@echo off
:loop
timeout /t 5
(docker run --shm-size=1gb -it --privileged --name test p 8080:8080 -h test thom/test:latest | findstr /R "proxy.*Daemon.*started")>nul 2>&1
if errorlevel 1 goto :loop
echo I can go!
explorer "http://localhost:8080"

or Confitional operator usage:

:loop
timeout /t 5
(docker run --shm-size=1gb -it --privileged --name test p 8080:8080 -h test thom/test:latest | findstr /R "proxy.*Daemon.*started")>nul 2>&1 && explorer "http://localhost:8080" || goto :loop
  • Related