Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to a docker container using a domain name

So I have some docker web application, when it loads using docker-compose the dhcp service chooses some ip address lets say 192.168.96.3, the webapp is located at port 6000, so connecting to the webapp I use http://192.168.96.3:6000. Is there any way, in the docker-compose.yml to assign the domain name foo.local so that when I connect to the webapp I type in foo.local:6000?

In my docker-compose.yml, can I add a domain name that my host machine can map to the dynamic ip of the container?

Note: The container uses its own network, so attaching it to the host network will conflict with its purpose.

like image 271
Tsangares Avatar asked Oct 31 '25 08:10

Tsangares


1 Answers

Forwarding container port

For me you can easily accessing from the host by exposing the port of the container. So from that host you should be able to access it as localhost:6000 by exposing the port. From other machines in your network that can access the host, use the IP of the host or its name/DNS name.

For example in docker-compose.yml

services:
    myservice:
        image: myImage
        ports:
            - "published_port:container_port"

So if you put "6000:6000" its mean that on the host port 6000 will forward to the service on port 6000.

DNS

So I would say for overall access, ensure that your company DNS match foo.local to your docker host and expose the port from the container in docker to the docker host.

If you want to be able to do that only from a given machine yoythe host you can add an entry to /etc/hosts (assuming linux)

127.0.0.1       localhost
127.0.0.1       foo.local

Here this is assuming we are on the same machine, but you can use the right IP. And if you have a different OS, check the documentation on how to do that for your os.

like image 173
Nicolas Bousquet Avatar answered Nov 02 '25 22:11

Nicolas Bousquet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!