I have docker compose file with following content. Somehow the ports are working only I can access on port 80 for all the images
If I try to access using the IP:9000 and so on it does not work. I fetch the IP from “docker network inspect network1” and so on.
Why do you expect it to work differently? httpd is listening on port 80 by default and the compose file will not change it. You need to forrward all your ports to port 80 inside the container, not port 8080, 8081 or 8082.
Regarding IP:9000 the left side of the port mapping is always the host port. When you are using the container IP you need to use the internal port which is port 80 in this case.
I hope I could help.
PS.: Please use code blocks next time. It is the </> icon right above the message text area next to the quote button. If you don’t use it, well… I suppose you saw how your message looked like before I fixed it
Sure ! Next time I will follow the same. I am new to docker so somewhere I would not be able to understand. Now question is if I keep all the container to have 80 port, is it possible. ?
If not what can I do to make it work ? Since all ports were same that’s the reason I forcefully used different ports.
Every container has its own ip address and processes are listening on that. So yes, it is possible and not just possible, this is exactly what you do now since you haven’t changed the httpd config.
If you use the container ip, of course it will only work with the container port. Why would you expect it to work with the container ip and the port you published it on the host?
Though, in almost no scenario using the container ip is the desired approach. You do publish container ports to host ports… why don’t you use them?
On the local machine either use localhost:{host-port} or {host-ip}:{host-port}will work, the later will work if you want to access it from another host in your lan as well. User defined networks like your network1 and network2 support dns-based service discovery, which allow accessing another container by its service name.
Note: even in container to container communication, calling another container by its ip is undesired.
I feel you still don’t understand what that ports section is for. I agree with every words of @meyay, but in addition to that, I think we need to clear this misconception.
- 9000:80 doesn’t mean that you magically change on which port the service inside the container is listening. That has nothing to do with the ports you define in a compose file. That is only port forwarding. Defining that the host should accept the request on port 9000 and forward it to the container IP on port 80.
Although I got the feeling you believed that the left side of the port mapping (port 9000) was teling the container to somehow replace the listening port on the right side, your fist post was based on a completely different assumption, so if you still don’t understand what the ports section is exactly and what I tried to explain, please, describe as clearly as you can what you think it is so we can understand you.
Thanks for increasing my knowledge
Thanks a lot to make me understand. I was truly in impression that whenever I start a container I have to use its IP by inspecting it network, but now I am very much clear! Since I am learning I think I misunderstood the concept! Thanks again!
It works with localhost:9000 and so on