How to get IP address of a running docker-compose container
This will get the IP address of a running docker-compose
container for the mongo
service.
docker inspect $(docker-compose ps --format json | jq -r 'map(select(.Service=="mongo"))[0].ID') --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
When you use this in shell scripts, it’s often convenient to store the IP address in a variable:
export MONGO_IP=$(docker inspect $(docker-compose ps --format json | jq -r 'map(select(.Service=="mongo"))[0].ID') --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}')
which you can then use as $MONGO_IP
.
For more details on how this works, see the following posts:
- How to list just container names & IP address(es) of all Docker conatiners
- How to get container name of docker-compose container