pi-hole / docker-pi-hole

Pi-hole in a docker container
https://pi-hole.net
Other
8.54k stars 1.13k forks source link

Need document on how to access pihole from a different container #438

Open yegle opened 5 years ago

yegle commented 5 years ago

This is a...

Description

I'm puzzled by how to provide the PiHole DNS service to a different docker container on the same server. Apparently using HOST_IP:53 doesn't work for some reason.

Expected Behavior

HOST_IP:53 should work

Actual Behavior

Timeout

Possible Fix

Steps to Reproduce and debugging done

e.g. your docker run command, pages to visit, CLI commands you ran 1.

Debug steps I have tried

Context and extra information

Your Environment

bassopenguin commented 5 years ago

I think you can solve this using docker network

Steps

docker network create pihole # create a new network for your containers to talk over
# when starting your pihole add this option: --network
docker run \ # your options go here...
  --network pihole \ # this tells your pihole to be on the "pihole" docker network
  pihole/pihole:latest

# determine your pihole's IP address on the "pihole" docker network
docker network inspect pihole # find the Containers field with the name "pihole"
PIHOLE_IP=0.0.0.0 # replace 0.0.0.0 with the value from the "IPv4Address" field

# start your other container
docker run \ # other container options
  --network pihole \ # tell it to use the same "pihole" network
  --link pihole \ # tell it to link to the pihole container on the "pihole" docker network
  --dns ${PIHOLE_IP} \
  alpine:latest \ # or your container name
  /bin/sh # your container command

Then 🤞 it should work for you....

Tests I Used

Give it a try and let me know how it goes!

yegle commented 5 years ago

@bassopenguin

Yes at the end I figured out how to do this. Your solution is actually not complete, as there's no guarantee the $PIHOLE_IP will not change.

In order to guarantee you get a static IP address, you have to specify the ipv4_address in the networks section. You'll realize you can't assign IP address using the default network.

You can create a network in the docker-compose.yml file, but for each of the containers in the same YAML file, you have to specify the network repeatedly, which is not good.

In the end I figure this is the simplest you can do:

# Create a network outside of this YAML file, then reference the network here.
 networks:                                                                       
    default:                                                                    
        external:                                                               
            name: pihole_network
# Then define your pihole container:
services:
  pihole:
    ...
    network:
      default:
        ipv4_address: $IP # An IP address in the network you just created
  # Then the other containers that needs to use this DNS server:
  my_container:
    ...
    dns: $IP

Unfortunately you still need to specify dns repeatedly for each of the container. I guess this is some unexpected complexity of running a DNS server in container.

kevindd992002 commented 4 years ago

Any more efficient way to do this? I'm having the same problem.

shamoon commented 4 years ago

Cheers 🍻 @yegle I had been banging my head about this one for a while. Still feels like a bit of a work-around but as this issue states, would be helpful to include in documentation if it is in fact the correct method.

yegle commented 4 years ago

In case it might help someone else, here's the docker-compose.yaml file I'm using: https://github.com/yegle/your-dns/blob/master/docker-compose.yaml

RomainTT commented 3 years ago

@yegle 2 years later it seems it still is the right method. Thanks. One thing I don't know about is what happens to name resolution of the docker network itself (you can reach another container by using its name). I don't know if the use of PiHole inhibits that.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Please comment or update this issue or it will be closed in 5 days.

RomainTT commented 2 years ago

This still needs documentation and should not be closed.

PromoFaux commented 2 years ago

Seems like there are some solutions in this issue thread. PRs are welcome - we don't bite too hard

tkrafael commented 10 months ago

When running container on same network, you can just refer to it as its domain name. docker-compose will help on this:

# pihole deployment
services:
  pihole:
    networks:
      - default
      - pihole
networks:
  pihole:
  default:
services:
  myawesomeservice:
    env:
      # assuming that service name in above docker-compose is pihole
      - pihole_addr=http://pihole 
    networks:
      - default
      - pihole
networks:
  default:
  pihole:
    external: true

It will just work, even if you are using docker swarm and deploying containers in different machines using docker stack for example