Search code examples
dockerdocker-for-windowswindows-container

Website locally hosted by a Docker for Windows windows container is unavailable via localhost


I am attempting to set up a Docker for Windows container to build and host a simple static website using lite-server and Sphinx. I first run the container.

$ docker run -it -p 8080:8080 -v "$(pwd):C:\src" website

And then start lite-server.

$ yarn serve

The website is available from the container's IP address (e.g., http://172.26.141.28:8080) so I know lite-server is serving the content, but I cannot access the content with http://localhost:8080.

How can I expose the website via localhost:8080?

My Dockerfile is as follows

FROM microsoft/windowsservercore

ENV chocolateyUseWindowsCompression false
RUN powershell -Command \
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'));

RUN choco install nodejs -y
RUN choco install yarn -y
RUN choco install python3 -y
RUN pip install sphinx
RUN pip install sphinx_rtd_theme

# https://github.com/nodejs/node/issues/8897#issuecomment-319010735
# Workaround for error An unexpected error occurred: "ENOENT: no such file or directory, lstat 'C:\\ContainerMappedDirectories'".
RUN mkdir C:\src
RUN powershell Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices' -Name 'S:' -Value '\??\C:\src' -Type String
WORKDIR 'S:\\'

EXPOSE 8080
CMD ["powershell"]

lite-server is launched with

"scripts": {
  "serve": "light-server -s ./build/html -p 8080"
},

Software:

  • Docker version 17.06.2-ce, build cec0b72
  • Windows 10 (host)
  • Windowsservercore (container)

Solution

  • I don't know if there's been an update, but as of a year ago, this was the expected behavior:

    containers and their ports are only accessible via the NATed IP address.

    see: https://github.com/docker/for-win/issues/204#issuecomment-258638899.