Docker deploys java project (Kafka consumer) and reports an error, error message:
Failed to construct kafka consumer
and
No resolvable bootstrap urls given in bootstrap.servers
, there will be a warning message in the middle:
Removing server cdh1.tw9.com:9092 from bootstrap.servers as DNSresolution failed for cdh1.tw9.com
Because the idea startup or the jar package startup project uses the local /etc/hosts
configuration analysis (if this is not enough for you to play chicken) there will be no error message; but when using docker deployment, there is no configuration
inside the container , so dns resolution /etc/hosts
Domain name address will report an error
Solution: Modify the /etc/hosts configuration in the container or share the host /etc/hosts data
solution:
One: Directly enter the container to modify
/etc/hosts
. After restarting the container, the added content will be lost.
Two: Modify directly when making the image. This method requires you to be the root user, and
/etc/hosts
the file does not have permissions for ordinary users. So you have to install sudo in the container, which increases the image size and is not recommended.
Three: When using docker run to run a new container, add the domain name and IP information to the container’s /etc/hosts file through the parameter –add-host.
docker run --add-host=cdh1.tw9.com:192.168.9.1 --name server -d service-image
Four: Use docker-compose and configure
docker-compose.yml
parameters in the fileextra_hosts
. For example:extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"
Enter the container and view /etc/hosts to see the corresponding configuration;
Five: Shared hosting
/etc/hosts
:
The container "shares" the hosts file of the host (the ultimate solution)
Reference:
How to modify the hosts file in the docker container
Docker Compose configuration file details
the container's "shared" host's hosts file (ultimate solution)