1

Could I expose different docker container points to the same HTTP port on the host?

Example

docker container run --publish 80:80 -d -it --name wp wordpress
docker container run --publish 90:80 -d -it --name ci jenkins
docker container run --publish 100:80 -d -it --name gitlab gitlab/gitlab-ce

1 Answer 1

1

With that commands you are not using the same port at host. The nomenclature for -pis "hostPort:containerPort" so in that way you are mapping container's port 80 from all of them to your host at ports 80, 90 and 100. So no conflict at all.

Anyway, to answer to your question about possible conflicting. In first instance, your commands should be:

docker container run --publish 80:80 -d -it --name wp wordpress
docker container run --publish 80:80 -d -it --name ci jenkins
docker container run --publish 80:80 -d -it --name gitlab gitlab/gitlab-ce

In this way, you can do that commands but you'll probably get an error saying Bind for 0.0.0.0:80 failed: port is already allocated..

Anyway, in the hypothetical case of docker allowing that without an error... The first one you map is which is going to work because on "docker run" command there are iptables commands for openning ports from container to host, and iptables rules work in "first matching is which works" style. So you'll have 3 iptables rules in this case but the one is going to work is the first.

4
  • If I did this, this should work "docker container run --publish 80:80 -d -it --name wp wordpress / docker container run --publish 90:80 -d -it --name ci jenkins / docker container run --publish 100:80 -d -it --name gitlab gitlab/gitlab-ce" however the host would use different ports.
    – 001
    Commented May 17, 2017 at 7:39
  • But after executing that three commands, if you access to 127.0.0.1 from your host, you'll only get the wordpress site. I tested it already. Commented May 17, 2017 at 7:42
  • you would have to add the port at the end, localhost:80 (wordpress), localhost:90 (jenkins), localhost:100 (gitlab).
    – 001
    Commented May 17, 2017 at 7:52
  • But when you map a port... the nomenclature is hostPort:containerPort. All containers are using port 80 but your commands are using all different ports on host, 80, 90 and 100... so no conflict at all. I don't understand the question: Could I expose different docker container points to the same HTTP port on the host? because you are not doing that with that commands... I'll edit my answer Commented May 17, 2017 at 7:56

Your Answer

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

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