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:32

If you forgot container ID or don't want to manipulate with shell commands, it's better to use UI like Portainer.

https://portainer.io/

$ docker volume create portainer_data
$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

There you can find all information about container also IP.

查看更多
冷夜・残月
3楼-- · 2019-01-01 14:33

To get the IP address and host port of a container:

docker inspect conatinerId | awk '/IPAddress/ || /HostPort/'

Output:

    "HostPort": "4200"
                    "HostPort": "4200"
        "SecondaryIPAddresses": null,
        "IPAddress": "172.17.0.2",
                "IPAddress": "172.17.0.2",
查看更多
弹指情弦暗扣
4楼-- · 2019-01-01 14:34

To get all container names and their IP addresses in just one single command.

docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)

If you are using docker-compose the command will be this:

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

The output will be:

/containerA - 172.17.0.4
/containerB - 172.17.0.3
/containerC - 172.17.0.2
查看更多
泪湿衣
5楼-- · 2019-01-01 14:34

For windows 10:

docker inspect --format "{{ .NetworkSettings.IPAddress }}"  containerId
查看更多
像晚风撩人
6楼-- · 2019-01-01 14:35

Use:

docker inspect $CID | grep IPAddress | grep -v null| cut -d '"' -f 4 | head -1
查看更多
栀子花@的思念
7楼-- · 2019-01-01 14:37

Execute:

docker ps -a

This will display active docker images:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3b733ae18c1c        parzee/database     "/usr/lib/postgresql/"   6 minutes ago       Up 6 minutes                 5432/tcp            serene_babbage

Use the CONTAINER ID value:

docker inspect <CONTAINER ID> | grep -w "IPAddress" | awk '{ print $2 }' | head -n 1 | cut -d "," -f1

"172.17.0.2"

查看更多
登录 后发表回答