skip to Main Content

How can I check if my extra_hosts configuration is working?

version: '3.5'
services:
  nginx:
    image: nginx:stable
    extra_hosts:
      - "host.docker.internal:host-gateway"

I tried docker exec nginx /bin/sh -c 'ping host.docker.internal'

but got /bin/sh: 1: ping: not found

Is there some kind of ping alternative available in the nginx docker image?

Testing on Ubuntu 20.04.3 LTS host, with docker version 20.10.11 and docker-compose version 1.29.2.

2

Answers


  1. nginx image does not come with ping command, you can add a busybox to test in and out:

    cat << EOF > docker-compose.yaml
    version: '3.5'
    services:
      nginx:
        image: nginx:stable
        extra_hosts:
        - "host.docker.internal:host-gateway"
        ports:
          - 8080:80
      busybox:
        image: busybox
        extra_hosts:
        - "host.docker.internal:host-gateway"
        command: ash -c 'sleep 3600'
    EOF
    
    docker-compose up -d
    docker-compose exec busybox ping host.docker.internal
    docker-compose exec busybox wget -qO- nginx
    docker-compose exec busybox wget -qO- host.docker.internal:8080
    docker-compose down
    
    Login or Signup to reply.
  2. Always use sidecar container & do not overwhelm the main image:

    k8s community provides a good image for network debugging which includes almost all famous CLIs : dig, nslookup, ping,.etc

     k8s.gcr.io/e2e-test-images/jessie-dnsutils:1.3
    

    Use it the same way the busybox way explained by gohm’c

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search