Docker port binding not working as expected

Viewed 12544

Running a Jenkins image in my container which is bound to the host port 9090

sudo docker run -itd -p 9090:8080 -p 50000:50000 --name=myjenkins -t jenkins-custom /bin/bash

The output of running $docker port myjenkins

50000/tcp -> 0.0.0.0:50000
8080/tcp -> 0.0.0.0:9090

I can also see the binding from the host perspective ps -Af | grep proxy

root     15314 15194  0 17:52 ?        00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 50000 -container-ip 172.17.0.2 -container-port 50000
root     15325 15194  0 17:52 ?        00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9090 -container-ip 172.17.0.2 -container-port 8080

After starting my jenkins server i try connect to the container using the host ip and the forwarded port (9090).

I'm new to Docker so may have missed something however would appreciate suggestions

Update: including dockerfile

From local-artifiactory/jenkinsci/jenkins:2.9
ENV java_opts="-Xmx8192m"
3 Answers

This is not an answer to this specific question. It is a possible answer to "port mapping doesn't work"

I've been caught by this twice.

The image name must come last when creating a container from the command line

This syntax:

docker run --name MyContainer MyImage -p 8080:80

will create container MyContainer from MyImage without issue

But the -p 8080:80 part will be silently ignored and your port mapping won't work

This syntax will work - you'll see exactly the same outcome except that port mapping will actually work.

docker run --name MyContainer -p 8080:80 MyImage

Same for this:

docker run MyImage --name MyContainer

This will create a container from MyImage but it won't give it the explicit name, it'll assign a random name

I hope this saves someone some time.

Port binding/publishing in docker is actually publishing container's port to docker-machine's, instead of to localhost's. For example, with
docker run -p 9090:8080 jenkins
you will be able to access the service by curl <your-docker-machine>:9090, NOT curl localhost:9090 or curl 127.0.0.1:9090
To get your docker-machine's IP, do: docker-machine ls and check the URL

Related