简体   繁体   中英

Access Docker Container Port in a Ubuntu VM

Given an Ubuntu VMWare Machine (IP: 192.168.10.35 ) that runs a docker image inside (IP: 172.0.18.2 ) and given this docker-compose.yml how would I access the Docker Image from my local machine?

version: '3'

services:
    sc2:
      build: .
      ports:
        - 127.0.0.1:4620:80
      restart: always
      networks:
        - default
      volumes:
        - ./sc2ai:/sc2ai
        - ./apache/000-default.conf:/etc/apache2/sites-available/000-default.conf
networks:
    default:

I tried to access 192.168.10.35:4620 but the connection failed. What am I missing? Is there an option in the docker-compose missing or do I need to forward ports from inside the VM to the docker image?

PS: If I start the image in docker-for-windows on my local machine I can access it via http://localhost:4620 .

You can't, because you've explicitly declared that the container (not the image) is only reachable from the VM itself. The declaration

ports:
  - 127.0.0.1:4620:80

forwards inbound connections on port 4620 on the host to port 80 in the container, but only on the interface bound to 127.0.0.1, which is the dedicated loopback interface (often named lo ). When you try to contact it from the host, it arrives on the VM's external IP 192.168.10.35, but nothing is listening there.

If you remove the explicit port binding, Docker will listen on all interfaces, which is usually what you want, and then you should be able to reach the container via the VM's external IP address.

ports:
  - '4620:80'

(Terminology: an image is a set of static filesystem content; you launch containers from an image and make network connections to the running containers. You can't directly see what's inside an image, an image doesn't have any running processes, and you can't connect to an image on its own.)

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