Reputation: 133
I'd like to run a rust web app in a docker container. I'm new to both technologies so I've started out simple.
Here is main.rs
:
extern crate iron;
use iron::prelude::*;
use iron::status;
fn main() {
fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Hello World!")))
}
Iron::new(hello_world).http("127.0.0.1:8080").unwrap();
}
Cargo.toml
[package]
name = "docker"
version = "0.1.0"
[dependencies]
iron = "*"
Dockerfile
(adapted from this tutorial)
FROM jimmycuadra/rust
EXPOSE 8080
COPY Cargo.toml /source
COPY src/main.rs /source/src/
CMD cargo run
These are the commands I ran:
docker build -t oror/rust-test
docker run -it -p 8080:8080 --rm -v $(pwd):/source -w /source oror/rust-test cargo run
docker ps
ifconfig
to get my machine's IP address: 192.168.0.6
curl 192.168.0.6:8080
to connect to my rust web appcurl: (52) Empty reply from server
I've tried localhost:8080
and I still get the same output.
What am I missing?
Upvotes: 12
Views: 12979
Reputation: 386
The problem is your web server is listening to requests from 127.0.0.1 (local interface) but from inside your container. From the container point of view, your host is outside so you need to listen to requests from 0.0.0.0, then it should works.
Iron::new(hello_world).http("0.0.0.0:8080").unwrap();
If you need to filter where your requests come from, I suggest you to do it from outside your container with a firewall or something like that.
Upvotes: 37