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

26,395

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

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

Share:
26,395
Mathias Conradt
Author by

Mathias Conradt

Cybersecurity Professional (CIAM, AppSec, DevSecOps, OSINT), Software Engineer, Open Source Enthusiast, Privacy Advocate, Tactical & Stealth Gamer, Motor Biker.

Updated on April 13, 2020

Comments

  • Mathias Conradt
    Mathias Conradt about 4 years

    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.