简体   繁体   English

在Ubuntu VM中访问Docker容器端口

[英]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? 给定一个在内部运行172.0.18.2映像(IP: 172.0.18.2 )的Ubuntu VMWare计算机(IP: 192.168.10.35 ),并给出此docker-compose.yml ,我如何从本地计算机访问Docker映像?

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. 我尝试访问192.168.10.35:4620但连接失败。 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? docker-compose missing中是否有选项,还是我需要将端口从VM内部转发到docker映像?

PS: If I start the image in docker-for-windows on my local machine I can access it via http://localhost:4620 . PS:如果我在本地计算机上的docker-for-windows启动映像,则可以通过http://localhost:4620

You can't, because you've explicitly declared that the container (not the image) is only reachable from the VM itself. 不能,因为您已明确声明只能从VM本身访问容器 (而不是映像)。 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 ). 将主机上端口4620上的入站连接转发到容器中的端口80,但仅在绑定到127.0.0.1的接口上,该接口是专用的回送接口(通常称为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. 当您尝试从主机联系它时,它到达了VM的外部IP 192.168.10.35,但是那里没有监听。

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. 如果删除显式端口绑定,则Docker将侦听通常需要的所有接口,然后您应该能够通过VM的外部IP地址访问容器。

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.) (术语:映像是一组静态文件系统内容;您从映像启动容器,并与正在运行的容器建立网络连接。您无法直接看到映像中的内容,映像没有任何正在运行的进程,并且您将无法单独连接到图像。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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