Tsangares
Tsangares

Reputation: 825

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.

Upvotes: 4

Views: 8650

Answers (1)

Nicolas Bousquet
Nicolas Bousquet

Reputation: 4000

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.

Upvotes: 4

Related Questions