Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose: setting hostname equal to its hostname of where the container is running

Right now, I have two-node in swarm cluster

$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
yey1njv9uz8adf33m7oz0h80f *   redis2              Ready               Active              Leader
lbo91v2l15h24isfd5jegoxu1     redis3              Ready               Active   

this is docker-compose.yml file

version: "3"
services:
  daggr:
    # replace username/repo:tag with your name and image details
    image: daggr
    hostname: examplehostname
    deploy:
      replicas: 1
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
        - "8080:8080"
    volumes:
        - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
        placement:
            constraints: [node.role == manager]
    networks:
        - webnet
  redis:
    image: redis
    networks:
        - webnet
networks:
  webnet:

as you see, i am explictly setting hostname for daggr service

daggr container basically runs a python tornado web server

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('Hello from daggr running on %s\n' % socket.gethostname())

I've tested out as below

$ curl redis2:4000
Hello from daggr running on examplehostname

Now, instead of statically setting the hostname, i want it to be dynamic matching to hostname of where the container is running. ie. if daggr container is running on redis2 it should say redis2 and on redis3, it should say redis3.

How can i specify that from docker-compose.yml file?

like image 722
ealeon Avatar asked Apr 05 '18 06:04

ealeon


People also ask

How do I change the hostname of a running docker container?

Running the command "sudo nsenter --target 1 --uts hostname <my new hostname>" from inside the container did the trick.

What is the use of hostname in docker compose?

The general goal of the hostname is that computers on the network know each other and thus communicate between themselves. Similarly, the main goal here is to ensure that containers can communicate with each other successfully within a Docker network.

What is the hostname of a docker container?

In the same way, a container's hostname defaults to be the container's ID in Docker. You can override the hostname using --hostname . When connecting to an existing network using docker network connect , you can use the --alias flag to specify an additional network alias for the container on that network.

What does docker compose Depends_on do?

Docker Compose depends_on Docker will pull the images and run the containers based on the given dependencies. So, in this case, the Postgres container is the first in the queue to run. However, there are limitations because depends_on doesn't explicitly wait for dependencies to be ready.


2 Answers

I tried Constantin Galbenu's solution only work on a swarm setup and brandonsimpkins's lacks the HOSTNAME definition.

A workaround is to set an environment variable to your hostname:

export HOSTNAME="$(cat /etc/hostname)"
my-container:
  hostname: ${HOSTNAME}

If like me, your hostname is the same as your username, skip step 1 and only do

my-container:
  hostname: ${USER}
like image 137
David Bensoussan Avatar answered Sep 16 '22 11:09

David Bensoussan


The selected answer above did not work for me (presumably since I am not running docker/docker-compose in swarm mode).

I was able to set the container hostname for my reverse proxy to match the docker host FQDN by doing the following:

version: '3'

reverse-proxy:
  hostname: $HOSTNAME

This allowed me to easily configure Nginx to pick the correct server cert / key pair prior to startup.

like image 41
brandonsimpkins Avatar answered Sep 17 '22 11:09

brandonsimpkins