获取docker容器(container)ip地址 使用命令方式
1 sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID>
1 2 3 sudo docker inspect <container id> sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
进入容器内 命令:
1 2 sudo docker exec -it mynginx /bin/bash cat /etc/hosts
1 2 3 4 5 6 7 8 9 ubuntu@ip-172 -31 -16 -20 :~/dotnet$ sudo docker exec -it mynginx /bin/bash root@70 e571d3bcab:/ 127.0 .0.1 localhost::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17 .0.4 70 e571d3bcab
在 ~/.bashrc 中写一个 bash 函数
添加如下内容:
1 2 3 function docker_ip() { sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1 }
然后执行:
最后调用函数:
所有容器名称及其IP地址 1 sudo docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
如果使用docker-compose命令将是:
1 docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
1 sudo docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
可能遇到的问题:
1 2 3 4 5 6 7 8 ubuntu@ip-172 -31 -16 -20 :~/dotnet$ sudo docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq) Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run /docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.37/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied "docker inspect" requires at least 1 argument.See 'docker inspect --help' . Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...] [flags] Return low-level information on Docker objects
执行以下命令,将当前用户添加 docker 组:
1 2 3 sudo usermod -a -G docker $USER 或 sudo usermod -aG docker $USER
注销,重新登录:
注意:注销并重新登录是必需的,因为除非您的会话关闭,否则组更改不会生效。
参考:https://blog.csdn.net/sannerlittle/article/details/77063800