I recently faced one non-trivial (for me) case. Maybe you know any way to solve it.

I have a shell script that runs Docker command to start the Ubuntu container, which ends up with running guest Bash that starts some system services, including a web server. After guest Bash is initialised I want to open web browser on my host machine. Currently it looks like that:

docker run -it -p $PORT:80 -v $ROOT:/usr/share/nginx $PROJECT
python -m webbrowser http://localhost:$PORT

But that obviously doesn't work well, as script waits until docker exits to start python. Swapping them won't work as well, as website will be loaded before the guest Bash (and web server) is started. At the same time I need to run Docker with -it, so docker ... & python ... won't work as well.

Is there any way to make things working?

2 Answers 2


You could run the webbrowser after an initial delay in the background:

(sleep 2 && python -m webbrowser http://localhost:$PORT) &
docker run -it -p $PORT:80 -v $ROOT:/usr/share/nginx $PROJECT

I do this for a docker wrapper for grip to view README's served by a container in the host and it works fine for me. It depends on how long your container needs to start up though.

  • Thanks! This may work, however it's hard to determine how much time container needs to start on different machines.
    – VisioN
    Jul 25, 2016 at 14:25
  • 2
    You could continuously exec "curl localhost" in the container to see when the server is up in a while loop and then break and open the web browser.
    – kba
    Jul 25, 2016 at 14:47
  • it doesn't seem the ( ) & translates to windows cmd, is there any other way to specify it should start running this process (wait for 2 seconds then do something) while also running a different command (docker run) for windows? my current solution is a hack, as I have to start a different bat file: start cmd /k call run_delayed.bat argument
    – MetaStack
    Apr 2, 2019 at 21:17
  • Thanks @kbastandswithMonica, I have added the curl solution as a separate answer.
    – mzuther
    Mar 7, 2021 at 13:01

Here is a version using curl that starts the browser when the server in the container is up:

(curl --silent --retry 30 --retry-delay 1 --retry-connrefused \
       http://localhost:$PORT && \
     python -m webbrowser http://localhost:$PORT) &

docker run -it -p $PORT:80 -v $ROOT:/usr/share/nginx $PROJECT

curl tries to connect every second for thirty seconds and then just gives up. When that happens, the browser will not be started at all. In case you want the browser to start unconditionally, exchange the && for a ;.

  • Actually, I do recommend using ; instead of &&. It has resolved some weird problems I could neither debug nor fix in any other way. Also, if the browser occasionally opens up with a blank page, you at least know that the script worked like it should and can focus your efforts on the real problem.
    – mzuther
    Mar 17, 2021 at 9:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .