How to get a Docker container's IP address from the host

ghz 8months ago ⋅ 125 views

Is there a command I can run to get the container's IP address right from the host after a new container is created?

Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts.

Answers

Yes, you can use the docker inspect command to retrieve information about a container, including its IP address. Here's how you can do it:

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

Replace containername with the name or ID of your container.

This command will output the IP address of the specified container. You can use this IP address in your scripts for tasks such as code deployment or container configuration.

For example, if you want to retrieve the IP address of a container named wordpress, you would run:

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

The output will be the IP address of the wordpress container, which you can then use in your scripts.