我创建了一个包含 rust 应用程序的 docker 映像,该应用程序响应获取端口 8000 上的请求。该应用程序本身是一个使用火箭库 ( https://rocket.rs/ ) 的基本示例,它看起来像这样
#![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
然后我创建了一个 Docker 文件来托管它
FROM ubuntu:16.04
RUN apt-get update; apt-get install -y curl
COPY server /root/
EXPOSE 8000
CMD ["/root/server"]
我使用构建 docker 映像
$ docker build -t port_test
并运行它$ 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 它工作正常
$ docker exec -it 3befe0c272f7 curl -s localhost:8000
Hello, world!
但是我不能从主机做同样的事情
$ curl localhost:8000
curl: (56) Recv failure: Connection reset by peer