As the title says. I need to be able to retrieve the IP address the docker hosts and the portmaps from the host to the container, and doing that inside of the container.
相关问题
- Docker task in Azure devops won't accept "$(pw
- Unable to run mariadb when mount volume
- Unspecified error (0x80004005) while running a Doc
- What would prevent code running in a Docker contai
- How to reload apache in php-apache docker containe
On Docker for Mac, as of version 18.03, you can use
host.docker.internal
as the host's IP.Note, as in the documentation, "This is for development purpose[s] and will not work in a production environment outside of Docker for Mac." This is because, in Docker for Mac, "you cannot see a docker0 interface on the host. This interface is actually within the virtual machine."
This is an update from
docker.for.mac.localhost
, available since version 17.06, anddocker.for.mac.host.internal
, available since version 17.12, which may also still work.For example, I have environment variables set on my host:
In my
docker-compose.yml
file, I have this:With https://docs.docker.com/machine/install-machine/
a) $ docker-machine ip
b) Get the IP address of one or more machines.
Update: On Docker for Mac, as of version 18.03, you can use host.docker.internal as the host's IP. See allanberry's answer. For prior versions of Docker for Mac the following answer may still be useful:
On Docker for Mac the
docker0
bridge does not exist, so other answers here may not work. All outgoing traffic however, is routed through your parent host, so as long as you try to connect to an IP it recognizes as itself (and the docker container doesn't think is itself) you should be able to connect. For example if you run this from the parent machine run:This should show you the IP of your Mac on its current network and your docker container should be able to connect to this address as well. This is of course a pain if this IP address ever changes, but you can add a custom loopback IP to your Mac that the container doesn't think is itself by doing something like this on the parent machine:
You can then test the connection from within the docker container with telnet. In my case I wanted to connect to a remote xdebug server:
Now when traffic comes into your Mac addressed for 192.168.46.49 (and all the traffic leaving your container does go through your Mac) your Mac will assume that IP is itself. When you are finish using this IP, you can remove the loopback alias like this:
One thing to be careful about is that the docker container won't send traffic to the parent host if it thinks the traffic's destination is itself. So check the loopback interface inside the container if you have trouble:
In my case, this showed
inet 127.0.0.1/8
which means I couldn't use any IPs in the127.*
range. That's why I used192.168.*
in the example above. Make sure the IP you use doesn't conflict with something on your own network.If you want real
IP
address (not a bridgeIP
) onWindows
and you have docker18.03
(or more recent) do the following:Run bash on container from host where image name is
nginx
(works onAlpine Linux distribution
):Then run inside container
192.168.65.2
is the host's IP - not the bridge IP like inspinus
accepted answer.I am using here host.docker.internal:
For those running Docker in AWS, the instance meta-data for the host is still available from inside the container.
For example:
If you enabled the docker remote API (via
-H
tcp://0.0.0.0:4243
for instance) and know the host machine's hostname or IP address this can be done with a lot of bash.Within my container's user's
bashrc
:The second line grabs the container ID from your local
/proc/self/cgroup
file.Third line curls out to the host machine (assuming you're using 4243 as docker's port) then uses node to parse the returned JSON for the
DESIRED_PORT
.