Home > Software design >  Hostname not accessible from outside the container in Docker
Hostname not accessible from outside the container in Docker

Time:05-19

I have created the following docker compose file:

version: '2'

services:
  testApp:
    image: nginx
    hostname: myHost
    ports:
      - "8080:80"
    networks:
      - test

networks:
  test:
    driver: bridge

From outside the container, I can open the web page with localhost:8080. But if I try to open the web page via the defined hostname, it doesn't work.

Can anyone tell me how I can fix this issue?

Is there also a way to connect to the containers ip from the outside?

Thanks in advance.

CodePudding user response:


Other containers on the test network would be able to reference it by that hostname, but not your host machine. You are binding port 8080 on your machine to port 80 on the container, so any external system that you would want to access the website would need to connect to your host machine on 8080 (as you did with localhost:8080).

How you do that depends on your networking, but for example if you know the ip or hostname of your local machine you can (probably) connect from another device on the same home network (your phone? Another computer?) using http://{ip-of-your-host}:8080. Exposing that to the internet from within a home network typically requires port forwarding on your router, and optionally a domain name.

The main point though is that the hostname in your compose is only relevant to other containers connecting to the same docker network (test). Outside of that, systems would need to make a connection to 8080 on your host machine.

  • Related