Search code examples
dockerdocker-machine

Is there a way to add a hostname to an EXISTING docker container?


I have some containers that communicate via their IP from the network docker.

I can use the option -h or --hostname when running a new container but I want to set the hostname for existing container.

Is it possible?


Solution

  • One way is to create network and add different container in this network. When adding container in the network, you can use the --alias option of docker network. Like this:

    • Create a network:

      docker network create <my-network-name> 
      
    • Add containers in the network:

      docker network connect --alias <hostname-container-1> <my-network-name> <container-1>
      docker network connect --alias <hostname-container-2> <my-network-name> <container-2>
      docker network connect --alias <hostname-container-3> <my-network-name> <container-3>
      
    • Enjoy.

    So each container can see other container by the alias (the alias is used as hostname).