Docker端口未映射 [英] Docker Port Not Mapping

查看:263
本文介绍了Docker端口未映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,事实证明这很明显,但是我很难从主机上的容器外部访问dockerized服务器.这是我在做什么:

I'm sorry if this turns out to be obvious, but I'm having a hard time accessing a dockerized server from outside the container on the host. Here's what I'm doing:

我正在使用以下命令运行容器:

I'm running the container with the following command:

docker run -it --rm --name aurelia_dev -p 8888:8080 -v /Users/terskine/git/marvel/legendary:/app aurelia /bin/bash

我正在容器中运行应用程序:

Within the container I'm running the app:

cd app/
au run

在另一个终端中,我可以看到容器正在运行:

In a separate terminal, I can see the container is running:

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4b0a14bc6e88        aurelia             "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        0.0.0.0:8888->8080/tcp   aurelia_dev

但是,我无法到达服务器:

However, I can't reach the server:

$ curl http://localhost:8888
curl: (52) Empty reply from server

但是在容器中,我可以到达服务器:

But within the container, I can reach the server:

$ docker exec -it aurelia_dev /bin/bash
root@4b0a14bc6e88:/# curl http://localhost:8080
<!DOCTYPE html> ...
<html>
... [A whole bunch of HTML] ...
</html>
root@4b0a14bc6e88:/# 

为什么我不能从主机端口8888的容器外部访问服务器?

Why can't I access the server from outside the container on the host's port 8888?

我确定我缺少明显的步骤.请帮帮我.

I'm sure I'm missing an obvious step. Please help me out.

谢谢!

我认为以下内容不是必需的,但如果有帮助,我将运行一个简单的aurelia容器,该容器是使用以下Dockerfile创建的:

Edit 1: I don't think the following is necessary, but If it's helpful, I'm running a simple aurelia container which I created with the following Dockerfile:

FROM node

RUN npm install -g aurelia-cli

CMD /bin/bash

我正在运行的aurelia项目是官方教程项目,可以在这里找到:

The aurelia project that I'm running is the official tutorial project which can be found here: https://aurelia.io/docs/tutorials/creating-a-contact-manager

这是我的主机文件的内容.

This is the content of my hosts file.

root@d7d1cc03a5c0:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      d7d1cc03a5c0

似乎我没有在听172.17.0.2:8080

root@d7d1cc03a5c0:/# curl 172.17.0.2:8080
curl: (7) Failed to connect to 172.17.0.2 port 8080: Connection refused

推荐答案

事实证明,这与我的Docker配置无关.相反,这是Aurelia使用的webpack的问题. Webpack无法在Docker容器中与localhost一起使用,请签出

This turned out not to be a problem with my Docker configuration. Rather it was a problem with webpack, which is used by Aurelia. Webpack won't work with localhost in a docker container, please checkout here.

要解决此问题,必须将0.0.0.0指定为主机,而不要使用默认值localhost.

To fix the problem, you must specify 0.0.0.0 as the host rather than using the default, which is localhost.

au run --host 0.0.0.0

注意:我在这里提出了一个有关实际问题的问题,并回答了它,因此更容易找到其他人将来也会遇到相同的问题,但是我要离开这个问题,因为可能有像我这样的人认为这是docker配置问题.

Note: I asked a question about the real problem here and answered it, so it would be easier to find for others who have the same problem in the future, but I'm leaving this question because there may be people like me who think this is a docker config problem.

这篇关于Docker端口未映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆