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

后端 未结 30 2645
小鲜肉
小鲜肉 2020-11-22 08:45

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

30条回答
  •  萌比男神i
    2020-11-22 09:40

    Add this shell script in your ~/.bashrc or relevant file:

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

    Then, to get an IP address of a container, simply do this:

    docker-ip YOUR_CONTAINER_ID
    

    For the new version of the Docker, please use the following:

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

提交回复
热议问题