When working with Docker containers, there may be times when you need to know the IP address of a running container, particularly for setting up network communication between containers or integrating with external services. On Ubuntu, this can be easily achieved using the following command:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <ContainerName>
This command retrieves the IP address of a specified Docker container. It utilizes Docker’s inspect functionality, which provides detailed information about Docker objects like containers, images, volumes, and networks. The -f option allows the output to be formatted using a Go template. In this example, the template iterates over all the networks associated with the container and extracts the IPAddress field from each network object.
To use this command, replace
This method is particularly useful in scenarios where containers need to communicate with each other or when services running outside of Docker require the container’s IP address for integration. By programmatically accessing the container’s IP address, you can streamline the process of configuring and managing Docker-based networks.