How to get a Docker container's IP address, located within a bridge network, from a windows host?

15,302

Solution 1

You can get directly the ip by using docker inspect and a proper format:

docker inspect containerId --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

More info in the Docker documentation.

Solution 2

You can get directly the IP address by using docker inspect. Docker inspect provides detailed information on constructs controlled by Docker.

Modern Docker client syntax is:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

Old Docker client syntax is:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

These commands will return the Docker container's IP address.

If you are on Windows, use double quotes " instead of single quotes ' around the curly braces.

For more information Docker inspect documentation.

Solution 3

Here is the command to get container information along with container IP.

docker inspect 'ContainerID'

Solution 4

Since you ran

docker run -p 3306:3306 ...

you should access the service via port 3306 using your host's DNS name or IP address.

In the very specific case where you're at a shell prompt or browser URL entry on the machine where you ran docker run, assuming you're not using Docker Machine or Docker Toolbox, in this case and in this case only, you can use localhost:3306 to access the container. Never ever use localhost for anything else around Docker unless you're totally clear what it means. (If you ever start writing a question that uses the words "...the localhost of..." then localhost isn't what you want.)

If you are trying to reach this container from another container, since you ran

docker run --name=blogdb --network=blognetwork ...

you can use the name blogdb as a host name, and Docker provides an internal DNS service that can resolve it to something that will reach the container.

You never want the Docker-internal IP address, and should never run docker inspect to try to find it. The two biggest problems with it are that it's unreachable from off-host and that it changes when the container is deleted and recreated.

Share:
15,302
navarq
Author by

navarq

Updated on June 12, 2022

Comments

  • navarq
    navarq almost 2 years

    A bridge network has been created in docker using the following:

    docker network create blognetwork
    

    And a container has been created in that network with the following:

    docker run --name=blogdb --network=blognetwork -p 3306:3306 -e --bind-address=0.0.0.0 -e=MYSQL_ROOT_PASSWORD=mypassword -detach mysql
    

    How do I access the ip address of the new container, within the "blognetwork", from a windows host?

    How do I get the ip address of the form X.X.X.X, on its own? I know I can use the following to get a large json output with this data in it:

    docker network inspect blognetwork