Simplifying IP Lookup for Windows Containers

Simplifying IP Lookup for Windows Containers

When doing work with Windows Docker containers, you'll quickly realize that you can't use localhost to access the container like you can with Linux containers. This puts you in a situation where you need to know the IP address of your running containers.

Image Credit: Quinn Dombrowski

Finding the IP of your containers is easy enough:

# To get the list of container ids
docker ps

# To get the IP address of a container
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

But that's a bit of work and I found myself landing on this Stackoverflow answer multiple times per day.

To solve the problem, I've created a Powershell function that lists the IP address and name of all running containers. So that when I type dip (docker ip) in a Powershell window, I get:

PS C:\Users\jgraham> dip
172.22.89.191 - registry.name/db:5.2
172.22.82.234 - registry.name/api:5.2
172.22.94.33 - registry.name/client:5.2

To add the function to your Powershell profile for easy use run:

notepad $profile

A notepad window will pop up. Add the following function to that file and save.

Function global:dip {,@(docker ps --quiet) | %{&docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} - {{ .Config.Image }}' $_}}

Open a fresh Powershell window and you should see some results when you type dip (given you have running containers).