Mathias Conradt
Mathias Conradt

Reputation: 28665

Docker: how to execute a batch file when container starts and keep the user in cmd / session

I'm using Docker on Windows 2016 Server TP4 with a Windows container.

When the container gets started, I want to execute a certain initialization script (init.bat) but also want to keep the user logged into the container session (in cmd).

With this dockerfile:

FROM windowsservercore
ADD sources /init
ENTRYPOINT C:/init/init.bat

and this init.bat (which is supposed to run inside the container on startup):

mkdir C:\myfolder
echo init end

and this startup call for the container:

docker run -it test/test cmd

the init.bat batch file gets executed inside the container, but the user does not stay logged in the container, but the container exits (with exit code 0).

I don't quite understand why it exits. From how I understand the docker documentation:

If the image also specifies an ENTRYPOINT then the CMD or COMMAND get appended as arguments to the ENTRYPOINT.

the cmd command should get appended to the entrypoint, which is my init script, but it doesn't.

I also tried this syntax, but it does not make a difference.

ENTRYPOINT ["C:/init/init.bat"]

If I remove the ENTRYPOINT from the dockerfile and start the container with the cmd command, I stay in the session and I can of course run the init.bat script manually and it works, but I want it to run automatically.

When I work with Ubuntu containers, I usually use supervisord to execute any initialization scripts, and bin/bash (which equivalents to cmd on Windows) as the command. I am not sure how to do the same on a Windows container though.

Upvotes: 7

Views: 41579

Answers (2)

Mihir
Mihir

Reputation: 11

I've a similar case. Here's my dockerfile

FROM microsoft/dotnet-framework-build:4.7.1 

run mkdir c:\WorkSpace
copy ./CreateFolder.bat /WorkSpace

CMD c:\\WorkSpace\\CreateFolder.bat

ENTRYPOINT POWERSHELL Write-Host Folder created ; \
while ($true) { Start-Sleep -Seconds 3600 }

This isn't working. The container stays up but the folder isn't created.

Whereas if i do the opposite :

FROM microsoft/dotnet-framework-build:4.7.1 

run mkdir c:\WorkSpace
copy ./CreateFolder.bat /WorkSpace

ENTRYPOINT c:\\WorkSpace\\CreateFolder.bat

CMD POWERSHELL Write-Host Folder created ; \
while ($true) { Start-Sleep -Seconds 3600 }

---- this is working. The container stays up and the folder is created.

Upvotes: 0

hillel
hillel

Reputation: 2373

instead of the ENTRYPOINT you can try putting something like this in your Dockerfile:

CMD C:\init\init.bat && cmd

Upvotes: 9

Related Questions