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
3 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
EXPOSE
instruction 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-p
flag ondocker run
to publish and map one or more ports, or the-P
flag 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
nginx
container without publishingport 80
to ahost
port - inspect its
ip address
- use
curl
fromhost
to access thenginx
home 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>
$

- 23,218
- 20
- 96
- 128
-
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
-
Get just the ip: `docker container inspect docker_container_name | jq -r '.[0].NetworkSettings.Networks.web.IPAddress'` – Mint Sep 28 '22 at 23:18
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,678
- 4
- 21
- 20
I am not entirely sure i follow the question, but for mac you could use
- ip: 0.0.0.0
- port: the port you are exposing
For example, i have a compose file
version: '3.8'
services:
db:
image: postgres
restart: on-failure
environment:
POSTGRES_PASSWORD: changeit
ports:
- target: 5432
published: 5432
protocol: tcp
mode: ingress
volumes:
- db-data:/var/lib/postgresql/data
admin:
image: dpage/pgadmin4
restart: on-failure
environment:
PGADMIN_DEFAULT_EMAIL: admin@mail.com
PGADMIN_DEFAULT_PASSWORD: changeit
ports:
- target: 80
published: 8080
protocol: tcp
mode: host
volumes:
- admin-data:/var/lib/pgadmin
volumes:
db-data:
admin-data:
On docker compose up
a network db_default
is created (check logs)
There are 3 scenarios
- To access pg-admin you open browser and type 0.0.0.0:8080 .
- To create a database from pg-admin (Right click 'Servers' / Register / Server) under Connection tab, for Host name / address you specify "db". That is because you are inside the network created by compose
- To connect to the db from outside (say for some reason you wish to connect to db from a local script using a database connection url) you will use (for user postgres, driver psycopg2: postgresql+psycopg2://postgres:changeit@0.0.0.0:5432/your_db_name_here
A note:
Some peer recommend doing docker inspect db_default
and find the IPv4 of the db container and use that in the url. This will not work as robie2011 said

- 486
- 1
- 5
- 21