简体   繁体   English

无法使用 IP 地址访问本地 docker 容器

[英]Unable to access local docker container with IP address

I am running a docker container locally on my Mac system.我在我的 Mac 系统上本地运行 docker 容器。 I have pasted the Dockerfile contents at the bottom of this post.我在这篇文章的底部粘贴了 Dockerfile 的内容。 I have exposed port 8888 in the image and I would like to access the python program from my host browser using container-ip-address:8888.我在图像中暴露了端口 8888,我想使用 container-ip-address:8888 从我的主机浏览器访问 python 程序。 But, it doesn't connect.但是,它没有连接。 Mapping it to a port on localhost works, but I don't want to do that.将其映射到本地主机上的端口是可行的,但我不想这样做。 How can I solve this?我该如何解决这个问题? (Already tried creating a docker network and running the container as part of the new network - no dice). (已经尝试创建 docker 网络并将容器作为新网络的一部分运行 - 没有骰子)。 Any help would be much appreciated.任何帮助将非常感激。 Dockerfile contents - Dockerfile 内容 -

FROM python:latest

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY . /usr/src/app

EXPOSE 8888
RUN pip install tornado
CMD ["python", "./api.py" ]

Expose just tells docker that this container going to work with this port. Expose 只是告诉 docker 这个容器将与这个端口一起工作。 it doesn't map it anywhere.它不会 map 它在任何地方。 https://docs.docker.com/engine/reference/builder/#expose https://docs.docker.com/engine/reference/builder/#expose

Mapping is the task of code which is running your container.映射是运行容器的代码的任务。 In case of local machine its a docker.如果是本地机器,它是 docker。 If you don't map port in docker run then container will not be accessible from outside of docker network.如果您不docker run docker 中的 map 端口,则无法从 docker 网络外部访问容器。 At the same time your host machine is not in docker network.同时您的主机不在 docker 网络中。 Same in case if you create docker network manually - your machine just doesn't have access to inner parts of it.如果您手动创建 docker 网络,情况相同 - 您的机器无法访问它的内部部分。

In other words you have to choose your way:换句话说,你必须选择你的方式:

  • use -p.... when you do docker run (local development)使用-p....当您执行 docker 运行(本地开发)
  • or define it in docker compose file if you use docker composer (mostly local development).或者如果您使用 docker 作曲家(主要是本地开发),则在 docker 撰写文件中定义它。 this approach is very useful when you are running multiple communicating systems in docker.当您在 docker 中运行多个通信系统时,这种方法非常有用。
  • or do it through kubernetes yaml definition file on production if you go into kubernetes with your solution (production with orchestrator/ autoscaler and etc.) or do it through kubernetes yaml definition file on production if you go into kubernetes with your solution (production with orchestrator/ autoscaler and etc.)

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

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