New to docker..
Is it possible to support multiple containers in different ports but to map to the same docker host port 80 ?
I have two apps, one running in apache, the other on glassfish server.
I want to run these two containers in the same linux instance in aws.
Is it possible to have something like the following:
Already tried, obviously getting:
Bind for 0.0.0.0:80 failed: port is already allocated
But what I want' to know if is it possible, how to achieve it, and also is it recommended or not and why ?
Thanks
According to nginx-proxy
Usage To run it:
$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy Then start any containers you want proxied with an env var VIRTUAL_HOST=subdomain.youdomain.com
$ docker run -e VIRTUAL_HOST=foo.bar.com ... The containers being proxied must expose the port to be proxied, either by using the EXPOSE directive in their Dockerfile or by using the --expose flag to docker run or docker create.
Provided your DNS is setup to forward foo.bar.com to the host running nginx-proxy, the request will be routed to a container with the VIRTUAL_HOST env var set.
I performed the following:
docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy
Then ran one of my images to test, but no luck, what am I doing wrong:
docker run -e docker run -e VIRTUAL_HOST=my.host my/image
Where my.host
is in /etc/hosts
:
127.0.0.1 localhost
127.0.0.1 my.host
Also, my/image
is exposed at 8080
This doesn't work, test: my.host/app-context-root throws:
502 Bad Gateway nginx/1.13.12
Is it possible to have something like the following:
container 1 apache EXPOSE 9090
container 2 glassf EXPOSE 8080
docker run -p 80:9090 container1
docker run -p 80:8080 container2
Answer: It is not possible.
When you run various docker containers the Docker host will redirect the request to the correct container based on the port mapping as all container share the same host IP and the request to all containers will come through the host ip and only the port will vary.
What you are trying is exposing two different ports of the containers but mapping them to same port.
So, your first container will start but the second container won't and on checking the logs you will see the error port already in use
.
container 1 apache EXPOSE 9090
container 2 glassf EXPOSE 8080 docker run -p 80:9090 container1 docker run -p 80:8080 container2
From the above I see that port 80 of your host machine is already mapped to container1, but again you are trying to map it to container 2 and this will not work.
Try the port mapping as below
docker run -p 9090:80 container1 docker run -p 8080:80 container2
Your containers 1 and 2 will have their port 80 mapped to different ports of your host machine.
As you'll start to see by the current answers, it is not possible or recommended to achieve what you are doing.
The ports you use in an interface are there to delegate traffic to its correct destination. Think of it as if your street had two houses, with the same ZIP code, and the same house number - where would your delivery go?
The big question is, why do you want it this way? the obvious solution is to have them on separate ports - and any requests for apache
would go to the apache
port, and vice versa for glassf
.
If they must be given the same port number on the same box - you could have a second instance of docker running pointed to a second interface (or secondary IP on the same interface). The trade-off here is that your docker containers would no longer be on the same network.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.