简体   繁体   English

无法从主机连接到Docker容器

[英]Unable to connect to Docker container from host

Just using Docker for the first time so I'm probably making a rookie mistake, but here goes. 刚刚第一次使用Docker,所以我可能会犯一个新手的错误,但是这里有。 I am trying to use the reactioncommerce/reaction image, and it appears to run correctly. 我正在尝试使用reactioncommerce/reaction图像,它似乎正确运行。 However, I cannot seem to connect to the server from the host. 但是,我似乎无法从主机连接到服务器。

I am running docker run -p :8080 -it reaction as suggested on the Docker Hub page , then trying to access it by going to http://localhost:8080 on a browser on the host, but no connection can be made. 我正在运行docker run -p :8080 -it reactionDocker Hub页面上建议的docker run -p :8080 -it reaction ,然后尝试通过在主机上的浏览器上访问http://localhost:8080来访问它,但是无法建立连接。 Where am I going wrong? 我哪里错了?

I'm running on a Linux Mint host. 我在Linux Mint主机上运行。

I think your problem will be your -p ( publish ) flag. 我认为你的问题将是你的-p发布 )标志。 Assuming your container is actually listening on port 8080 - try -p 8080:8080 which will map localhost:8080 to your container. 假设您的容器实际上正在侦听端口8080 - 请尝试-p 8080:8080 ,它将localhost:8080映射到您的容器。 (Well, technically it'll map 0.0.0.0:8080 which is all addresses - including external) (嗯,从技术上讲,它将映射0.0.0.0:8080 ,这是所有地址 - 包括外部)

But I think if you're not specifying something on the left hand side, you're getting a random port number mapped - you should be able to see this in docker ps or using the docker port command. 但是我想如果你没有在左侧指定一些内容,那么你会得到一个随机的端口号映射 - 你应该可以在docker ps或者使用docker port命令看到它。

When you run docker run -it you start it interactively - and it should start 'whatever is defined in the docker file' unless you specify otherwise. 当您运行docker run -it您将以交互方式启动它 - 它应该启动'docker文件中定义的任何内容',除非您另行指定。 I assume this will be a service you want, but I don't know that app. 我认为这将是你想要的服务,但我不知道那个应用程序。 You can also use the -d flag, that runs the container in the background. 您还可以使用-d标志,它在后台运行容器。

I believe that this command will work: 我相信这个命令会起作用:

docker run -p 127.0.0.1:8080:8080 -it reaction

I tried to do the same thing with a local webserver, but wasn't able to connect to my container using localhost until I added the address 127.0.0.1 to my -p command. 我试图用本地网络服务器做同样的事情,但是在我将地址127.0.0.1添加到我的-p命令之前,无法使用localhost连接到我的容器。

Your /etc/hosts file should have the following entry: 您的/etc/hosts文件应具有以下条目:

127.0.0.1    localhost

This will resolve the domain localhost allowing you to use it to access your server. 这将解析域localhost允许您使用它来访问您的服务器。

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

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