问题
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
回答1:
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"
and now it works fine.
Thanks David.
回答2:
Rocket have different standard configuration, please try staging
or prod
to be able to do what you want, source.
ROCKET_ENV=staging cargo run
See also:
- Why can I not access this Rust simple server from the Internet?
来源:https://stackoverflow.com/questions/54119006/why-is-are-my-published-ports-not-working