3

I'm running a redis tunnel on my local machine. I want my container to connect to my local machine to then connect to the redis tunnel so that my redis credentials are kept locally.

Here is my docker-compose.yml

version: '3.8'

services:

      front:
    container_name: front_fr
    build:
      context: docker
    volumes:
      - ".:/var/www/front:rw,cached"
    environment:
      - APPLICATION_ENV=dev
    ports:
      - "443:443"
      - "6379:6379"
    extra_hosts:                                                                                                                                                                            
      - "redis.XXXX.amazonaws.com:172.23.0.1"                                                                            
      

This works fine. But I had to install iproute 2 on the container and run ip route to get the route (172.23.0.1) and add it to the extra_hosts:

   docker exec e09f640bfd0d apt-get install -y iproute2                                                                                             
   docker exec e09f640bfd0d ip route   

Is there a way to add the gateway automatically without having to manually do these steps ?

Thank you.

Alain Zelink
  • 889
  • 2
  • 15
  • 33

2 Answers2

2

If you own the docker file for this service, you can add these steps to a separate script and set it up as the ENTRYPOINT (ref. https://docs.docker.com/engine/reference/builder/#entrypoint).

If you're comfortable overriding the existing entry-point for this image, this can be done in docker-compose.yml as well (ref. https://docs.docker.com/compose/compose-file/#entrypoint)

Ankur
  • 106
  • 9
  • Sounds good. But how can I change dynamically the IP of the gateway in my docker-compose. yml file ? How can the value 172.23.0.1 be replaced dynamically by the value that "ip route" is going to return ? – Alain Zelink Aug 31 '20 at 14:45
  • 1
    extra_hosts is just a convenient way of adding entries to the /etc/hosts file of the container. Since you are fetching the IP address after the container is run; there's no point updating docker-compose. yml . You can, instead, use the ENTRYPOINT script to add the IP entry to /etc/hosts file of the container directly. – Ankur Sep 01 '20 at 04:25
  • Thank you. I found some info here https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build/40721996 about adding my extra conf to /etc/hosts. I ended up adding it in docker-compose.yml : command: bash -c 'echo `ip route show default | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"` " example.com" >> /tmp/hosts_extra && cat /tmp/hosts_extra >> /etc/hosts' – Alain Zelink Sep 01 '20 at 13:48
0

just create a shell script with all of docker-compose commands

docker-compose build
docker-compose run front apt-get install -y iproute2  
docker-compose run front ip route 
docker-compose up
Sergio Tanaka
  • 1,325
  • 1
  • 6
  • 18
  • Same comment as for the first answer. Sounds good. But how can I change dynamically the IP of the gateway in my docker-compose. yml file ? How can the value 172.23.0.1 be replaced dynamically by the value that "ip route" is going to return ? – Alain Zelink Aug 31 '20 at 14:46