How to reach docker containers by name instead of IP address?

前端 未结 6 1744
醉梦人生
醉梦人生 2020-12-29 17:44

Is there a way I can reach my docker containers using names instead of ip addresses?

I\'ve heard of pipework and I\'ve seen some dns and hostname type options for do

6条回答
  •  我在风中等你
    2020-12-29 18:38

    Create a new bridge network other than docker0, run your containers inside it and you can reference the containers inside that network by their names.

    Docker daemon runs an embedded DNS server to provide automatic service discovery for containers connected to user-defined networks. Name resolution requests from the containers are handled first by the embedded DNS server.

    Try this:

    docker network create 
    docker run --net  --name test busybox nc -l 0.0.0.0:7000
    docker run --net  busybox ping test
    

    First, we create a new network. Then, we run a busybox container named test listening on port 7000 (just to keep it running). Finally, we ping the test container by its name and it should work.

提交回复
热议问题