1

So, I have a docker container ready for building, but, when I build it, I need to go manualy and start mongod from within docker container. What am I doing wrong? I start the mongod from the Dockerfile, but it looks like that something is killing the process or that the process is never even being executed?

FROM microsoft/iis:10.0.14393.206  
SHELL ["powershell"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET ; \  
    Install-WindowsFeature Web-Asp-Net45

COPY  Pub Pub

RUN mkdir data\db

COPY mongodb_installer.msi mongodb_installer.msi

RUN Start-Process -FilePath 'mongodb_installer.msi' -ArgumentList '/quiet', '/NoRestart' -Wait ; \
    Remove-Item .\mongodb_installer.msi 

RUN 'C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe'

RUN Remove-WebSite -Name 'Default Web Site'  
RUN New-Website -Name 'Pub' -Port 80 \  
    -PhysicalPath 'C:\Pub' -ApplicationPool '.NET v4.5'


EXPOSE 80

CMD ["ping", "-t", "localhost"]  

When I start mongod from withing container, my web api application is working perfectly, need to know how to set mongod running from start?

Miljan Vulovic
  • 1,586
  • 3
  • 20
  • 48
  • 1
    It looks like you're trying to run multiple processes in a single container. That's not the way docker is designed; it will work better if you run each process (web service, mongodb, etc)in a separate container. – Vince Bowdren Jun 15 '17 at 15:22

2 Answers2

2

So, going in the right direction with the answer provided by @Peri461, this made it work at the end:

FROM microsoft/iis:10.0.14393.206  
SHELL ["powershell"]

RUN Install-WindowsFeature NET-Framework-45-ASPNET ; \  
    Install-WindowsFeature Web-Asp-Net45

COPY Pub Pub

RUN mkdir data\db

COPY mongodb_installer.msi mongodb_installer.msi

RUN Start-Process -FilePath 'mongodb_installer.msi' -ArgumentList '/quiet', '/NoRestart' -Wait ; \
    Remove-Item .\mongodb_installer.msi     

RUN Remove-WebSite -Name 'Default Web Site'  
RUN New-Website -Name 'Pub' -Port 80 \  
    -PhysicalPath 'C:\Pub' -ApplicationPool '.NET v4.5'

ADD init.bat init.bat

ENTRYPOINT C:\init.bat

EXPOSE 80

CMD ["ping", "-t", "localhost"]  

Entrypoint should point to the batch that should be executed when container starts. After that, just start the container like:

docker run --name pub -d -p 80:80 pub
Miljan Vulovic
  • 1,586
  • 3
  • 20
  • 48
1

I tried this too myself at one point. What I found is that you can't actually start processes from the Dockerfile. It seems natural to make a Docker image by giving it all of the install commands for a certain program, but it's not yet a running instance when you're building the image.

The solution if I remember correctly, is to use an ENTRYPOINT statement in your Dockerfile, so that it'll execute these commands at runtime and not build-time.

This might make an interesting follow-up to read.

And here is the documentation Docker has for the ENTRYPOINT statement.

  • Im still not doing something right... I added the entrypoint like this: ENTRYPOINT ["C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"] and then using the same run like this: docker run --name pub -d -p 80:80 pub What am I missing? – Miljan Vulovic Jun 15 '17 at 12:48
  • I wish I knew what to say, but having done it once before doesn't necessarily mean that I understood what I was doing. I'll defer to any other answers that arise, I apologize. Things to consider might include commands in your Dockerfile that are dependent upon `mongod` to be running, but are executed before the process started, and any other processes you tried starting in your Dockerfile. `mongodb_installer.msi` might be one. –  Jun 15 '17 at 13:02
  • You pointed me in the right direction, thank you for that, will post if I find the way, again, thank you! – Miljan Vulovic Jun 15 '17 at 13:03
  • Ok, got everything up, but, the problem now is that my batch file is not executing on start. CMD C:\init.bat && cmd do you happen to know maybe anything about this? – Miljan Vulovic Jun 15 '17 at 14:14
  • From the question I linked above: "The `ENTRYPOINT` specifies a command that will always be executed when the container starts. The `CMD` specifies arguments that will be fed to the `ENTRYPOINT`." Try adding your `init.bat` to an executable script for your `ENTRYPOINT`. I think it's a conflict of `ENTRYPOINT` and `CMD` statements in your Dockerfile. –  Jun 15 '17 at 17:35
  • Worked, I added my own answer to the question, thank you! – Miljan Vulovic Jun 16 '17 at 09:30