7

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
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
rvabdn
  • 947
  • 6
  • 20
  • 6
    That sounds like the symptom of the server process binding to 127.0.0.1 and being unreachable from outside the container, with the answer being to bind to 0.0.0.0, but I can't tell you how to address it for this particular server stack. – David Maze Jan 09 '19 at 23:07
  • I bet you are right. I'll try setting it to 0.0.0.0 – rvabdn Jan 09 '19 at 23:24
  • That was the issue. If you make this an answer I will accept it. – rvabdn Jan 10 '19 at 00:19

2 Answers2

9

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.

rvabdn
  • 947
  • 6
  • 20
3

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:

Stargateur
  • 24,473
  • 8
  • 65
  • 91