Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose ip/hostname for the host machine

I have a following docker-compose file:

nginx:
    image: nginx
    hostname: myservice.local
    volumes:
        - nginx.local.conf:/etc/nginx/conf.d/myservice.conf
    container_name: goout-nginx

Is there a way for docker-compose to set the myservice.local for the host machine as well? At the moment calling this on the host machine doesn't do anything:

$ ping myservice.local
ping: cannot resolve myservice.local: Unknown host

I don't want to manually update local /etc/hosts for such thing.

EDIT:

My problem seems to be more complex then I previously thought. I am trying to create a development setup for my colleagues which they could run locally on their machines.

This should be an easily-runnable application running on 80/443 ports in the browser. This cannot run on the localhost:80/443 as these ports are already taken and used by apache on their machines. To do that I want the running machine to have assigned IP address so they could just interact with the application just by opening myservice.local. This doesn't have to be accessible to the outside world, only the host machine.

The setup should be super simple - I was hoping all this could be achieved just by running docker-compose up – I was hoping to avoid any complicated setup of networking, IP tables or /etc/hosts.

like image 802
Vojtěch Avatar asked May 26 '18 05:05

Vojtěch


People also ask

How do I assign a hostname to a docker container?

Basic idea is to use docker inspect to obtain the pid of the container, then enter the uts namespace of the container via nsenter . Running hostname inside that namespace will change the hostname for the docker instance that shares that namespace.

What is docker localhost IP?

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.

Is a docker container IP same as host?

On Docker for Linux, the IP address of the gateway between the Docker host and the bridge network is 172.17. 0.1 if you are using default networking. Do you see the problem already? They are different, so you cannot simply run docker-compose up -d and all operating systems behave the same.


1 Answers

This will be a little convoluted given your requirements, but it should work.

The objective here is to modify the /etc/hosts on the host machine without needing manual intervention from the running users. The non-functional requirement is that it shouldn't impact your application when it's running non-locally (I.e., no changes to the nginx or backing containers running your application). The changes should be limited to the docker-compose file and utility scripts.

To that end, I'd recommend one of two things:

  1. A bash script that inserts an entry into their /etc/hosts if it doesn't already exist grep -q -F '<static_ip> myservice.local' /etc/hosts || echo '<static_ip> myservice.local' >> /etc/hosts then starts up docker compose using docker-compose up -d. The script is idempotent, so it should not add the line a second time if grep finds the line in /etc/hosts. This requires you to assign a static IP to the nginx container, but given you can do that within docker-compose, and it won't conflict with your host, that should meet your requirements. Name the file "startMyApp.sh" so it's clear what it does, and your non-tech savvy folks should gravitate to using that instead of the docker-compose syntax anyway.

OR

  1. Create a new docker container which maps the /etc/hosts file of the host into /tmp/hosts in the container. Then you can use network discovery within the new docker container to obtain the IP of your Nginx container and use the same method as above to add it to the hosts file. Only benefit here is that you don't need a static IP. I think option 1 is much easier.

Note that in both of these cases I'm assuming port 80/443 is exposed from your webserver container. You'll need to do that unless you're running a proxy to remap them.

Sample running:

version: '2'

services:

  tomcat:
    image: tomcat
    ports: 
      - "1234:8080"
    hostname: myservice.local
    networks:
      my_network:
        ipv4_address: 10.5.0.5

networks:
  my_network:
    driver: bridge
    ipam:
      config:
        - subnet: 10.5.0.0/16
          gateway: 10.5.0.1 

grep -q -F "myservice.local 10.5.0.5" || sudo echo "myservice.local 10.5.0.5" >> /etc/hosts docker-compose up -d

open a web browser and browse to "myservice.local:8080" and you can see apache tomcat's base page there.

like image 160
Patrick Avatar answered Sep 20 '22 11:09

Patrick