1

I'm using Windows server 2016 to spin up windowsservercore docker containers and am noticing what I think is incorrect behavior where the container exits very quickly even though it should be sleeping for over 15 minutes. I have the following Dockerfile:

FROM microsoft/windowsservercore
RUN powershell Start-Sleep -s 1000

I build the container with docker build -t mybuild . when in the same directory as the Dockerfile. I then run the container with docker run mybuild and it exits very quickly.

Looking at this answer it seems that a sleep should keep the container alive. That answer was showing Linux so not sure if that matters but I feel like the sleep process is running in either case and that's what determines if the container should exit or not on default

If I use interactive mode and/or (I tried all 3 combinations) a tty (docker run -it mybuild) it stays up until I exit the container's shell

Looking at the docker docs run executes the container in the foreground (like -it) although I don't understand why that would matter since the process should still be running regardless of the container being detached or not. I also tried running it in detached mode with docker run -d and it exits very quickly in that case as well.

I also tried running another command after the sleep but that still didn't work. The docker file then looked like:

FROM microsoft/windowsservercore
RUN powershell Start-Sleep -s 1000
RUN echo "hello" > C:\hello.txt

I looked at the dockerfile reference for RUN and it says that RUN in shell form executes the command using cmd /S /C on Windows. So I tried running this from my normal shell on my host Windows machine exactly like the Dockerfile specifies (cmd /S /C powershell Start-Sleep -s 1000) and verified that it works as expected.

What am I not understanding here? I'm new to docker and trying to learn but I can't figure out what's going on searching the internet and reading docs

ddrake12
  • 831
  • 11
  • 22
  • Possible duplicate of [What's the difference between RUN and CMD in a docker file and when should I use one or the other?](https://stackoverflow.com/questions/37461868/whats-the-difference-between-run-and-cmd-in-a-docker-file-and-when-should-i-use) – David Maze Jul 20 '18 at 19:27

1 Answers1

7

I think that there is a confusion about the RUN command in dockerfile: its not saying what is going to run when the container will start, it just a command to run when building the image (like run this installation command..). I think you are looking for one of the two options:

The CMD line in the dockerfile (doc):

FROM microsoft/windowsservercore
CMD ["powershell", "Start-Sleep", "-s", "1000"]

or, you can run it from command-line:

docker run -d mybuild <your command>
ItayB
  • 10,377
  • 9
  • 50
  • 77