5

I am trying to run jenkins container. I used "docker run --restart always --name myjenkins -p 8080:80 jenkins" but cannot access jenkins at http://localhost:8080 on browser. If I use docker run --restart always --name myjenkins -p 8080:8080 jenkins, I can access the jenkins url.

Thanks in advance

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
YashRao
  • 69
  • 1
  • 3
  • 7

4 Answers4

17

Without Docker

imagenro1

  • Each application must use a different port.

  • You can access to your application using directly its ports (if are available of course):

With Docker

enter image description here

  • Applications could use any port because each one "is a different world"

  • You can not access to your docker applications using its internal ports:

Because for instance, 8080 of APP_B is only visible inside APP_B container. No body can access to this applications.

In order to access to your docker applications, You must explicitly establish a relationship between:

Linux host ports <-> inside containers ports.

enter image description here

To do that you could use -p parameter

  • docker run -d -p 8080:8080 APP_A ...
  • docker run -d -p 8081:8080 APP_B ...
  • docker run -d -p 8082:8080 APP_C ...

After this you could access to your docker applications using its new ports :

Also a common error when docker-compose & docker network are used is use localhost instead ip when a docker app needs to connect to another docker app. As you can see you need to use ip or domain + external port instead localhost:8080


what is the difference between publishing 8080:80 and 8080:8080 in a docker run?

  • With 8080:80 you expect that your application uses or start with the 80 internal port inside container.
  • With 8080:8080 you expect that your application uses or start with the 8080 internal port inside container.

You just need to research what is the internal container port used by your jenkins and put it in docker run -p ...

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • 2
    Shouldn't it be `docker run -d -p 8080:8080 APP_A ... // docker run -d -p 8081:8080 APP_B ... // docker run -d -p 8082:8080 APP_C ...` ? – benmaq May 25 '20 at 10:06
10

8080:80 refers that in the container you are using port 80 and you are forwarding that port to host machine's 8080 port. So you are running Jenkins on port 80 inside your container wherever in scenario 2 you are running Jenkins on port 8080 inside the container and exposing it over the same port on host machine. For example if I am running mysql in container I may use 8080:3306 so mysql would be running on port 3306 but exposed on 8080 of host machine but if choose it to be 8080:80 for mysql it may not work because as per the code of mysql it binds itself on port 3306 not port 80. Same is the scenario in your case of Jenkins too.

Vikas Kumar
  • 151
  • 2
  • @YashRao, this command shows you ports and process running in your linux machine : **netstat -tulpn** – JRichardsz Sep 05 '18 at 06:00
  • @JRichardsz what is the difference between docker run -p 8080:80 and docker run -p 8080:8080? – YashRao Sep 05 '18 at 14:42
  • @YashRao it means you are using port forwarding. Docker has its own network namespace hence it has its own ports from 1 to 65535 Now in case 1 (8080:8080) As jenkins run by default on port 8080 and is being forwarded on host machine's port 8080 Now in case 2 (8080:80) jenkins is being ran on port 80 on container and is being forwarded on host machine's port 8080 So in case2 you may need to check in your container whether jenkins is running on port 80 or not which seems highly unlikely in this scenario. – Vikas Kumar Oct 18 '18 at 06:02
3

When you say 8080:80, it means any request coming on port 8080 will be forwarded to service running on port 80 inside your docker container. Similarly 8080:8080 means any request coming for port 8080 will be forwarded to service running on port 8080 inside your container

You can also think of it as -

 Port for Outside World: Actual Port of service in container

Hope this helps

Vatsal
  • 2,068
  • 4
  • 21
  • 24
0

The syntax looks like below. More details about -p flag.

docker run -p [ip-on-host:]port-on-host:port-in-container image-name

In your case, -p 8080:80 means leading all traffic to port 80 in container. If you check port status on host by netstat -lntp|grep 8080, there is a process managed by docker-proxy who is listening on port 8080 on host machine. It would manage all traffic routing between port 8080 on host and port 80 in container.

Light.G
  • 5,548
  • 1
  • 14
  • 25
  • port 80 exists in container. I created a GitLab container by publishing 80 port in container with 8081 port on host – YashRao Sep 07 '18 at 14:31
  • @YashRao gitlab in jenkins image? Gitlab is not mentioned in your post. Maybe you could update your post? – Light.G Sep 07 '18 at 16:42