为什么我发布的端口不起作用? [英] Why is are my published ports not working?

查看:99
本文介绍了为什么我发布的端口不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含rust应用程序的docker映像,该应用程序响应端口8000上的请求。该应用程序本身是使用火箭库的基本示例( https://rocket.rs/ )看起来像这样

I've created a docker image containing a rust application that responds to get requests on port 8000. The application itself is a basic example using the rocket library (https://rocket.rs/) it looks like this

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}

我已对此进行了编译并将其称为 server

I have compiled this and called it server

然后我创建了一个Docker文件来托管它

I then created a Docker file to host it

FROM ubuntu:16.04

RUN apt-get update; apt-get install -y curl

COPY server /root/

EXPOSE 8000

CMD ["/root/server"]                           

我用
$ docker build -t port_test构建docker镜像并使用 $ docker run -p 8000:8000 port_test

运行它一切看起来不错

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED                     STATUS              PORTS                    NAMES
3befe0c272f7        port_test           "/root/server"      7 minutes ago       Up 7 minutes        0.0.0.0:8000->8000/tcp   festive_wilson

如果我在容器中运行curl,就可以正常工作

If I run curl within the container it works fine

$ docker exec -it 3befe0c272f7 curl -s localhost:8000
Hello, world!

但是我不能从主持人那里做同样的事情

However I can't do the same from the host

$ curl localhost:8000
curl: (56) Recv failure: Connection reset by peer


推荐答案

David Maze是正确的。问题在于该进程正在绑定到容器中的本地主机。我添加了一个Rocket.toml文件,其中包含以下条目

David Maze was correct. The problem was that the process was binding to localhost in the container. I added a Rocket.toml file with the following entries

[global]
address = "0.0.0.0"

[development]
address = "0.0.0.0"

现在可以正常工作了。

谢谢大卫。

这篇关于为什么我发布的端口不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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