简体   繁体   中英

Access server running on docker from host machine

I have a rest api server running on a docker container. The server is running on port 8000 of the docker container and Port 8081 of host machine is bind to port 8000 of the container. By connecting to the container and executing curl localhost:8000, I can see the rest api server is running OK inside the container. And by executing docker inspect I can see the port biniding is as expected. However when i browse localhost:8081 from the host PC, i get ERR_CONNECTION_REFUSED error. Below are some of my docker settings and logs. Any ideas on how to fix this?

# Docker compose File
version: "3"
services:  
  genie:
    network_mode: host
    build:
      context: app
      dockerfile: Dockerfile
    restart: unless-stopped
    ports:
      - "8081:8000"
    env_file:
      - ./.env
    volumes:
      - ./app:/home/genie/app
    entrypoint:
      - bin/server
# Docker inpsect result on the running container
"PortBindings": {
                "8000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8081"
                    }
                ]
            },

Your application inside the container needs to be configured to listen on all interfaces, not just localhost. Docker networking is namespaced, so only processes inside the container can reach localhost, just as other machines in the same network as your laptop cannot reach an application on your laptop listening only on localhost.

This change is application specific, which we have no details of in this question. However if it's configured for 127.0.0.1, you would change that to 0.0.0.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM