2

I would like to run a custom script which sets up the docker network and starts a docker container (after setting up some directories). This script is slow so I would like it to run when my computer is starting up, but only AFTER the docker daemon is startup.

Following the instructions here Run Batch File On Start-up I can easily create a batch file and have it run on starutp, however I am currently getting the error:

docker network create --driver nat MY-net error during connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/networks/create: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.

I am quite sure it is not related to privileges since running the script itself works.

Questions: Is it possible on windows batch file start to somehow run my batchfile last (after all other startup have been run, services/daemons are already started)?

Alternatively, is there some hook , which would let me run some custom script once the docker Daemon is up and running.

I am running Docker 19.0 on Windows 10. My docker is configured to be run on startup, and the daemon runs smoothly as I use docker regaulraly, so the issue seems to be that the script is beinf run before the docker daemon is fully started.

shelbypereira
  • 2,097
  • 3
  • 27
  • 50
  • ...But it is telling you __exactly__ what the problem is `:)` – Gerhard Oct 18 '19 at 06:55
  • I clarified my question , I am 100% certain that the docker is daemon is configured to run on startup, so my issue is that I think my batch file is being run BEFORE the docker daemon has started. – shelbypereira Oct 18 '19 at 07:10
  • Well I am not sure how you added the batch file to startup. But you would probably need to amend it to check if the service is running and only continue when the docker daemon is running. – Gerhard Oct 18 '19 at 07:14
  • batch file was just added to the startup path. so how would I check if "docker daemon is running and only continue when it is running?" – shelbypereira Oct 18 '19 at 07:33
  • 1
    run from `cmd` the command `tasklist /?` to see how you can find running processes. Use `findstr` to find a specific string and you can then use `if errorlevel` to determine if the batch file should continue or wait for the process. – Gerhard Oct 18 '19 at 07:35
  • I will try to program this and post back the answer if it works, thanks for suggestion! – shelbypereira Oct 18 '19 at 08:55

2 Answers2

2

Solution proposed by @gerhard works, basic solution is similar to this: https://superuser.com/questions/618210/how-to-make-a-batch-file-wait-for-a-process-to-begin-then-continue:

:search
REM CHECKING IF PROCESS IS RUNNING
tasklist|find "MyEXE"
IF %ERRORLEVEL% = 1 THEN (GOTO NOTfound)
TIMEOUT /T 1
GOTO search

:NOTfound
REM DO WORK
shelbypereira
  • 2,097
  • 3
  • 27
  • 50
1

The answer from shelbypereira is a good lead. Thanks! However, it is only a lead. Allow me to add an actual script for doing the detection and reaction that has been asked for:

:search
TIMEOUT /T 1
call docker container ls

IF /I "%ERRORLEVEL%" NEQ "0" GOTO search

echo Found docker to be running due to exit code being 0.
call docker run awesome_autostart_stuff_or_something
Markus-Hermann
  • 789
  • 11
  • 24