I have docker container for experiments. So I don't know which ports I will use later when I trying new apps. Isn't it possible to access docker container application with ip/port from the host without exposing it in the docker-run command?
-
Basically, you want to expose new port in running container right? Here is the related discussion in docker forum. I have tried some of them, and find them working fine. https://forums.docker.com/t/how-to-expose-port-on-running-container/3252/14 – sayboras Nov 13 '17 at 09:50
2 Answers
update: the OP didn't provide that it was about docker-for-mac in the original question, so the rest cannot be applied if you are using this version of docker.
Original answer:
Yes, you can do that.
Let's say that you have a container named boring_pare and a web app running on port 8080. You can access it from your host machine by requesting http://[ip_of_boring_pare]:8080. If you are using the default docker network, this ip will probably be in the 172.17.0.xxx range.
To find this IP, you can inspect your container by using:
docker container inspect boring_pare
Also, you mention:
Isn't it possible to access docker container application with ip/port from the host without exposing it in the docker-run command?
The correct term here would be publishing. Further reading:
Documentation about
EXPOSE/ Dockerfile...The
EXPOSEinstruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the-pflag ondocker runto publish and map one or more ports, or the-Pflag to publish all exposed ports and map them to to high-order ports.
Update to answer to @robie2011's comment:
Below, I run 3 commands:
- run a
nginxcontainer without publishingport 80to ahostport - inspect its
ip address - use
curlfromhostto access thenginxhome page
My console output:
$ docker run --rm --name some-nginx -d nginx
0e53d3b5ef6d65a4731c4066f3523c5ecd3c118abedae44b33e89fdf8e401632
$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' some-nginx
172.17.0.3
$ curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
$
- 19,659
- 17
- 88
- 119
-
Didn't worked in my case. Here two examples: `docker run --name some-nginx -d nginx` here is the proof that this should work on port 80 `docker run -p9999:80 --name some-nginx2 -d nginx` – robie2011 Dec 15 '17 at 14:54
-
@robie2011 I added at the end of my answer the results I get when I `curl` from `host` to the `container` run by your first command. – tgogos Dec 15 '17 at 15:39
-
I followed your steps but it didn't work for me. Im using macosx and have following runtime https://pastebin.com/q866LHRc maybe it's a mac issue? – robie2011 Dec 16 '17 at 20:38
-
@robie2011 exactly `The docker (Linux) bridge network is not reachable from the macOS host.`. I think @tgogos was not using MacOS – tread Oct 17 '19 at 12:54
Docker for Mac is unable to route traffic to containers, and from containers back to the host.
https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers
- 3,188
- 4
- 20
- 20