Docker: Custom Networks - Domain Name Resolution with Gateway

I may be showing my lack of knowledge with this question, but can you use Docker Custom Network Container names in URLs within a Gateway? Example would be using the Container Name in a URL for MQTT Transmission.

My experiments so far indicate that only the static IP address can be used to another container and not the Custom Network name even though within the containers Bash session the Custom Network name does resolve with a 'ping' command using the name (i.e. Container resolves name, but Gateway does not).

You definitely can do this. Keep in mind that the default bridge network on a standard Docker Engine installation does NOT provide DNS records for other attached containers. But on other custom networks you'll be able to resolve by container name (or in the case of Docker Compose, "service" name).

forum-99636

ref: Differences between user-defined bridges and the default bridge

I realize that I showed an example with a ping command, but it can also be used in other places where a hostname can be supplied, such as in the URL for MQTT Engine/Transmission, e.g.:

Thanks Kevin for the response. I see your example, but when I try to use this with a Custom Network it oddly does not resolve in the MQTT URL all though it does resolve in the container's BASH session (see: GIF)

Snippet of Docker-compose that creates the custom network:
networks:
iX_Sandbox:
driver: bridge
ipam:
config:
- subnet: 192.168.1.0/24

CustomNetworksAndMQTT

iX_Sandbox

I bet that this is the problem. Underscore characters _ are not recommended in URLs. I've had issues in the past here...

EDIT: Revised reasoning is that underscore characters are invalid in hostnames, thus not recommended in URLs.

1 Like

Confirmed. When I setup two containers ignition_1 and ignition_2, I get this error from MQTT Engine trying to use ignition_1 in the URL:

ME-d681d05b-f2fb-4dca: Error while attempting connect (with autoReconnect=false) to tcp://ignition_1:1883: Unable to make field private transient java.lang.String java.net.URI.userInfo accessible: module java.base does not "opens java.net" to unnamed module @299ee532

Following this...

# Disconnect `ignition_1` container from `testing` bridge network
docker network disconnect testing ignition_1
# Reconnect `ignition_1` container to `testing` network with alias `ignition-1`
docker network connect --alias ignition-1 testing ignition_1

... and adjusting the URL to use ignition-1 got me a valid connection:

So you can either adjust your service name, or make sure to add the proper alias to the service definition. ref: Compose service aliases

2 Likes

Thanks Kevin for the "CSI" work on that. You are right and I should have known that with URLs as I have experienced that before.

Thanks again.

2 Likes