How to get a Docker container's IP address fro

2019-01-01 14:15发布

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.

标签: docker
30条回答
情到深处是孤独
2楼-- · 2019-01-01 14:20

Show all containers IP addresses:

docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
查看更多
临风纵饮
3楼-- · 2019-01-01 14:22

Quick answer (This works):

Get your container name or ID:

sudo docker container ls

Then do it:

sudo docker inspect <container_ID Or container_name> |grep 'IPAddress'

查看更多
千与千寻千般痛.
4楼-- · 2019-01-01 14:24

Reference containers by name:

docker run ... --name pg-master

Then grab the IP address address by name:

MASTER_HOST=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' pg-master)
查看更多
骚的不知所云
5楼-- · 2019-01-01 14:25

Based on some of the answers I loved, I decided to merge them to a function to get all the IP addresses and another for an specific container. They are now in my .bashrc file.

docker-ips() {
    docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
}

docker-ip() {
  docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
}

The first command gives the IP address of all the containers and the second a specific container's IP address.

docker-ips
docker-ip YOUR_CONTAINER_ID
查看更多
梦醉为红颜
6楼-- · 2019-01-01 14:25

NOTE!!! for Docker Compose Usage:

Since Docker Compose creates an isolated network for each cluster, the methods below does not work with docker-compose.


The most elegant and easy way is defining a shell function as the most-voted answer @WouterD's:

dockip() {
  docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}

Docker can write container ids to a file like Linux programs:

Running with --cidfile=filename parameter, Docker dumps the ID of the container to this file.

Docker runs PID equivalent Section

--cidfile="app.cid": Write the container ID to the file

Using PID file

  1. Running container with --cidfile parameter, app.cid file content is like below:

a29ac3b9f8aebf66a1ba5989186bd620ea66f1740e9fe6524351e7ace139b909

  1. You can use file content to inspect Docker containers:

    ➜ blog-v4 git:(develop) ✗ docker inspect `cat app.cid`

  2. Extracting Container IP inline Python script:

    $ docker inspect `cat app.cid` | python -c "import json;import sys;\ sys.stdout.write(json.load(sys.stdin)[0]['NetworkSettings']['IPAddress'])" 172.17.0.2


More Human Friendly Form

#!/usr/bin/env python
# Coding: utf-8
# Save this file like get-docker-ip.py in a folder that in $PATH
# Run it with
# $ docker inspect <CONTAINER ID> | get-docker-ip.py

import json
import sys

sys.stdout.write(json.load(sys.stdin)[0]['NetworkSettings']['IPAddress'])

http://networkstatic.net/10-examples-of-how-to-get-docker-container-ip-address/

Here there are 10 alternatives of getting the Docker container IP addresses.

查看更多
宁负流年不负卿
7楼-- · 2019-01-01 14:27

Inspect didn't work for me. Maybe as I was using -net host and some namespaces.

Anyway, I found this to work nicely:

docker exec -i -t NAME /sbin/ifconfig docker0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
查看更多
登录 后发表回答