20

I am using the following Dockerfiles to create a container running Jenkins in a windows container on Windows 10 desktop running Docker Desktop for Windows version 17.03

FROM microsoft/windowsservercore

RUN powershell -Command wget 'http://javadl.oracle.com/webapps/download/AutoDL?BundleId=210185' -Outfile 'C:\jreinstaller.exe' ; Start-Process -filepath C:\jreinstaller.exe -passthru -wait -argumentlist "/s,INSTALLDIR=c:\Java\jre1.8.0_91" ; del C:\jreinstaller.exe

ENV JAVA_HOME c:\\Java\\jre1.8.0_91  
RUN setx PATH %PATH%;%JAVA_HOME%\bin

CMD [ "java.exe" ]

I create the image from this docker file:

docker build -t windows-java:jre1.8.0_91 .

The second Dockerfile I am using to install Jenkins on top of this:

FROM windows-java:jre1.8.0_91

ENV HOME /jenkins  
ENV JENKINS_VERSION 2.58  
RUN mkdir \jenkins  
RUN powershell -Command "wget -Uri https://updates.jenkins-ci.org/latest/jenkins.war -UseBasicParsing -OutFile /jenkins/jenkins.war"

EXPOSE 8080  
EXPOSE 50000  

CMD java -jar C:\\jenkins\\jenkins.war


docker build -t jenkins-windows:2.0 .

Then I launch the container like this:

docker run --name jenkinsci -p 8080:8080 -p 50000:50000  jenkins-windows:2.0

I can see the container running fine and logs showing up all good

PS C:\Users\mandeep\ringba\ringba-jenkins-setup-windows\jenkins-master> docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
85ba2ef525a1        jenkins-windows:2.0   "cmd /S /C 'java -..."   8 hours ago         Up 8 hours          0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkinsci

However, I cannot access the jenkins server running on http://localhost:8080 on the host machine's web browser.

Not sure if it helps but when I was running docker in Linux container mode on the same machine, I was able to access jenkins server on http://localhost:8080 using their official docker image.

6 Answers 6

15

This is a currently a known issue on Windows. It's not possible to access a container endpoint from its own host using localhost/127.0.0.1. It is possible using Linux containers today because Docker has included a special workaround that is unique to their Moby/Linux implementation for running Linux containers on Windows.

We're working on a fix for this, but today we recommend working around this by either:

  • Accessing container endpoints from a separate host, using the IP address of the host that is running the container, and the exposed port for the container on its host
  • OR by accessing the container on the same host, using the container's internal IP address and published port (you can use docker network inspect <network name> or docker exec <container ID> ipconfig> to get the IP address of the container endpoint itself)
7
  • Thanks Kallie. This worked perfectly. Could you please correct the localhost IP address in your answer from 172.0.0.1 to 127.0.0.1? May 4, 2017 at 5:43
  • Great, I'm glad it worked for you. And absolutely--thanks for catching that :) May 5, 2017 at 18:23
  • Ahh, I've spent hours trying to find this out... Thanks!
    – White hawk
    May 12, 2017 at 17:33
  • I guess this still only works in Win10, not in Win7 :(
    – cst1992
    Jun 18, 2019 at 10:00
  • 1
    Resolved it, when using docker run I had -p parameter at the end, which didn't work. Rearranging the order did worked. docker run -p 8081:8081 <image>
    – abitcode
    Jun 23, 2020 at 8:44
6

To complete @Kallie-Microsoft post:

docs.docker.com have been updated with a section Limitations of Windows containers for localhost and published ports


Docker for Windows provides the option to switch Windows and Linux containers. If you are using Windows containers, keep in mind that there are some limitations with regard to networking due to the current implementation of Windows NAT (WinNAT). These limitations may potentially resolve as the Windows containers project evolves.

One thing you may encounter rather immediately is that published ports on Windows containers do not do loopback to the local host. Instead, container endpoints are only reachable from the host using the container’s IP and port.

So, in a scenario where you use Docker to pull an image and run a webserver with a command like this:

docker run -d -p 80:80 --name webserver nginx

Using curl http://localhost, or pointing your web browser at http://localhost will not display the nginx web page (as it would do with Linux containers).

In order to reach a Windows container from the local host, you need to specify the IP address and port for the container that is running the service.

You can get the container IP address by using docker inspect with some --format options and the ID or name of the container. For the example above, the command would look like this, using the name we gave to the container (webserver) instead of the container ID:

$ docker inspect \
  --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
  webserver
1
  • Thanks. this is useful but even after having the ip address. For example 172.17.0.2. How does one make that available to the internet? Is it a matter of configuring the router to target not just 192.168.0.whatever, but also 172.17.0.2? Nov 15, 2019 at 10:16
4

I faced the same issue, the ordering of docker run command matters.

docker run -p <host port>:<container port> <image> Works

docker run <image> -p <host port>:<container port> Doesn't Work

My setup -

Using Windows 10 , Version 2004 (OS build 19041.329) WSL 2 enabled - https://learn.microsoft.com/en-us/windows/wsl/wsl2-index Ubuntu 18.04 installed from Microsoft store and its enabled in docker.

image

2
  • This worked for my teammate, any reasoning as to why the order of the 'p' flag matters?
    – deruitda
    Jan 25 at 21:43
  • 1
    As mentioned here, docs.docker.com/engine/reference/commandline/run, it's the way docker recognizes the OPTIONS before image name and COMMAND after it. Since -p or -publish is an option it has to be before.
    – abitcode
    Jan 30 at 6:15
3

The issue here is that "localhost" does not resolve to your container IP address, since it is not running directly on Windows but rather on a Linux virtual machine through WSL. This a known issue with Docker Desktop.

What you must do is bind your local port (on Windows) to the port on WSL.

In Windows Terminal / Powershell :

Fetch the IP address of the Linux virtual machine (Docker Desktop creates a distro called "docker-desktop" in WSL2)

$wsl_ip = (wsl -d "docker-desktop" -- "ifconfig" "eth0" "|" "grep" "inet addr:").trim("").split(":").split()[2]

Bind your local port to the same port on Linux (see Netsh)

netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=8080 connectaddress=$wsl_ip
0

Using Docker Desktop for Windows 4.20.1 and WSL 2, it works like a charm for me.

The only thing you need to do is to document the ports you wish to expose from your image.

Inside your Dockerfile, write the following:

EXPOSE 8080 (or whatever ports you want to expose)

Then create your image and start it via Docker Desktop, you can then bind your host ports to your container ports in the dialog where you can also define volumes, env variables etc (Optinal Settings)

See the following picture:

enter image description here

-1

Looks this issue does not seem to be so boring as it was. Followings docs at https://docs.docker.com/engine/reference/run/#expose-incoming-ports you can specify an IP address at the host machine where you want container's port(s) to be exposed.

-p=[] : Publish a container's port or a range of ports to the host
               format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
               Both hostPort and containerPort can be specified as a
               range of ports. When specifying ranges for both, the
               number of container ports in the range must match the
               number of host ports in the range, for example:
                   -p 1234-1236:1234-1236/tcp
               When specifying a range for hostPort only, the
               containerPort must not be a range.  In this case, the container port is published somewhere within the specified hostPort range. (e.g., `-p 1234-1236:1234/tcp`)
               (use 'docker port' to see the actual mapping)

Probably that is 127.0.0.1, and it resolves an issue with access to exposed Docker container port on the Windows system. Just use -p switch with IP address when running container.

docker run --rm -it -p 127.0.0.1:3000:3000 ubuntu:latest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.