Why is are my published ports not working?

时间:2019-01-09 22:00:25

标签: docker curl networking rust rocket

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();
}

I have compiled this and called it server

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"]                           

I build the docker image with $ docker build -t port_test and run it with $ docker run -p 8000:8000 port_test

At this point it all looks good

$ 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

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

2 个答案:

答案 0 :(得分:0)

火箭有不同的标准配置,请尝试stagingprod以执行您想要的source

ROCKET_ENV=staging cargo run

另请参阅:

答案 1 :(得分:0)

大卫迷宫是正确的。问题在于该进程正在绑定到容器中的本地主机。我添加了带有以下条目的Rocket.toml文件

[global]
address = "0.0.0.0"

[development]
address = "0.0.0.0"

现在工作正常。

谢谢大卫。

相关问题