docker-pi-hole icon indicating copy to clipboard operation
docker-pi-hole copied to clipboard

Need document on how to access pihole from a different container

Open yegle opened this issue 5 years ago • 10 comments

This is a...

  • [x] Request for a new or modified feature
  • [ ] Issue trying to run the docker image
  • [ ] Issue trying to build / test / develop the docker image

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. 2. 3. 4.

Debug steps I have tried

  • [x] I have tried destroying my container instance, pulling the newest image version, and re-creating a new container
  • [ ] I have tried running the nearly stock docker run example in the readme (removing any customizations I added)
  • [ ] I have tried running without my volume data mounts to eliminate volumes as the cause
  • [ ] I have searched this repository for existing issues and pull requests that look similar

Context and extra information

Your Environment

  • Docker Host Operating System and OS Version: Debian 9
  • Docker Version: Docker version 18.09.3, build 774a1f4
  • Hardware architecture: x86

yegle avatar Apr 03 '19 02:04 yegle

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

  • Scenario: Expect request to succeed when not connected to pihole
    • Given running alpine:latest container with curl added
    • And not connected to pihole
    • And URL http://b.scorecardresearch.com/p2?c1=2
    • When curl -v http://b.scorecardresearch.com/p2?c1=2
    • Then expect HTTP/1.1 200 OK response
  • Scenario: Expect request to fail when connected to pihole
    • Given running alpine:latest container with curl added
    • And connected to pihole (using --network pihole, --link pihole, and --dns ${PIHOLE_IP})
    • And URL http://b.scorecardresearch.com/p2?c1=2
    • When curl -v http://b.scorecardresearch.com/p2?c1=2
    • Then expect connect to 0.0.0.0 port 80 failed: Connection refused (because pihole is blocking it)

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

bassopenguin avatar Apr 12 '19 00:04 bassopenguin

@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.

yegle avatar Apr 22 '19 06:04 yegle

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

kevindd992002 avatar Nov 21 '19 16:11 kevindd992002

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.

shamoon avatar May 25 '20 23:05 shamoon

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

yegle avatar May 26 '20 00:05 yegle

@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.

RomainTT avatar Jun 15 '21 07:06 RomainTT

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.

github-actions[bot] avatar Jan 11 '22 18:01 github-actions[bot]

This still needs documentation and should not be closed.

RomainTT avatar Jan 13 '22 09:01 RomainTT

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

PromoFaux avatar Jan 13 '22 10:01 PromoFaux

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

tkrafael avatar Jan 12 '24 12:01 tkrafael