Home > database >  How to test extra_hosts configuration?
How to test extra_hosts configuration?

Time:12-08

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.

CodePudding user response:

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
  • Related