Identifying Docker Container IP Addresses

by 25th October 2018Development, Technological Thoughts

I have been starting to use Docker to implement a number of applications (Jira, Confluence, Jenkins, GitLab etc), but one issue I have encountered is, that unless you create specific networks for each container, on starting the container, it is given a random IP address (typically the next available one in the subnet).

Since I use reverse proxies to navigate to the containers, I need to be able to redirect from the external URL to the internal IP address of each container, but there is no simple way of identifying those IP addresses.

Enter a quick piece of code that can be used to retrieve the IP addresses for each container:

  1. Log into your Linux server that is running Docker as a user that can execute Docker commands.
  2. At the shell prompt, type ‘vi’.
  3. In vi, press I and paste in the code on the tab.
  4. In vi, press [ESC] and then type ‘wq’ and press [ENTER].
  5. At the shell prompt, type ‘chmod 744’ to make the script executable.
  6. You can now run the script specifying each container you want the IP address for as follows:
    ./ [containername1 | containername2 | containernameN]
  7. If you want to display IP addresses for all Containers, simply type:
  8. If you want to make life even more simple, you can create an alias for the script.
  9. At the shell prompt, type ‘vi ~/.bashrc’ and press [ENTER].
  10. In vi, type ‘dockerip=’~/’.
  11. In vi, press [ESC] and then type ‘wq’ and press [ENTER].
  12. At the shell prompt, type ‘. ~/.bashrc’ to reload the file.

To run the script you can now use: dockerip in place of ./

bash$: dockerip Jira Confluence

bash$: dockerip

# Displays the IP addresses for each Docker Container specified:
# ./ [containername1 | containername2 | containernameN]
# or all Docker Containers if no parameters are passed:
# ./

if [ ! "$@" ]
	containers="$(docker ps -f '{{.Names}}')"
for container in $containers
	echo -n  "$container: "; docker inspect -f 
	'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container

Matthew Cunliffe

Matthew Cunliffe


Matthew is an IT specialist with more than 24 years experience in software development and project management. He has a wide range of interests, including international political theory; playing guitar; music; hiking, kayaking, and bouldering; and data privacy and ethics in IT.

1 Comment

  1. Matthew

    I’ve made a change to the dockerip script to use a more elegant way of obtaining the Docker Container names. Previously I was passing the docker ps values to sed and retreiving the names based on column positioning: I have now changed it to use a simple Go template which Docker supports natively and is inherently more reliable in retrieving the container names.

Share this post