Is there a way to permanently set a hostname and IP to a container in docker? I want to create a stack of machines (containers) in one VM ideally talking to one another with hostname.
Asked
Active
Viewed 5.1k times
26
-
What are you trying to accomplish? I would suggest you're thinking about containers the wrong way if this is what you're trying to do. – Sobrique Feb 17 '16 at 20:35
-
I want to set up a stack of machines for running a webapp. Say 2 mariadb machines, nginx, one with jvm, kakfa, zookeeper, one for logparser etc. currently the setup is running as 12-14 VMs in vmware. Is it possible to run these VMs as containers ? – san1512 Feb 18 '16 at 19:00
-
Yes. But you would be better off port mapping - maybe using haproxy - than trying to static configure your container. – Sobrique Feb 18 '16 at 20:39
-
Yes it was a wrong assumption. – san1512 Feb 12 '20 at 16:58
-
I didn't have much idea what I was doing back then when I asked this question. – san1512 Oct 08 '20 at 22:54
-
Assigning a fixed IP may be required in situations when you install a distributed system like a Zookeeper cluster. Every node in the cluster has a configuration that refers to the IP addresses of the other nodes in the cluster. – Binita Bharati Jul 17 '21 at 13:51
2 Answers
28
You can use the new networking feature available after Docker version 1.10.0
That allows you to connect to containers by their name, assign Ip addrees and host names.
When you create a new network, any container connected to that network can reach other containers by their name, ip or host-names.
i.e:
1) Create network
$ docker network create --subnet=172.18.0.0/16 mynet123
2) Create container inside the network
$ docker run --net mynet123 -h myhostname --ip 172.18.0.22 -it ubuntu bash
Flags:
--net
connect a container to a network--ip
to specify IPv4 address-h, --hostname
to specify a hostname--add-host
to add more entries to /etc/hosts

Hemerson Varela
- 24,034
- 16
- 68
- 69
9
You can use docker-compose
tool to create a stack of containers with specific hostnames and addresses.
Here is the example docker-compose.yml
with specific network config:
version: "2"
services:
host1:
networks:
mynet:
ipv4_address: 172.25.0.101
networks:
mynet:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
Source: Docker Compose static IP address in docker-compose.yml
.
And here is the example of docker-compose.yml
file with containers pinging each other:
version: '3'
services:
ubuntu01:
image: bash
hostname: ubuntu01
command: ping -c1 ubuntu02
ubuntu02:
image: bash
hostname: ubuntu02
command: ping -c1 ubuntu01
Run with docker-compose up
.

kenorb
- 155,785
- 88
- 678
- 743
-
This doesn't automatically add hosts to your other instance. Eg. `ubuntu02` wont have the `ubuntu01` host, and vice versa. – Chris Qiang Oct 10 '19 at 06:26
-
See extra_hosts https://docs.docker.com/compose/compose-file/#extra_hosts – Gelldur Oct 04 '20 at 21:08
-
this answer is for adding hostname, not adding hosts (as in /etc/hosts). so it worked for me, as I was looking to change the hostname. hosts were added using [extra_hosts](https://stackoverflow.com/questions/29076194/using-add-host-or-extra-hosts-with-docker-compose) – Tuhin Jun 11 '21 at 14:38