Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running rust on Docker: Empty reply from server

Tags:

docker

rust

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:

  1. docker build -t oror/rust-test
  2. docker run -it -p 8080:8080 --rm -v $(pwd):/source -w /source oror/rust-test cargo run
  3. docker ps

Terminal Ouput

  1. ifconfig to get my machine's IP address: 192.168.0.6
  2. curl 192.168.0.6:8080 to connect to my rust web app

curl: (52) Empty reply from server

I've tried localhost:8080 and I still get the same output. What am I missing?

like image 611
OReallyOReily Avatar asked May 21 '17 14:05

OReallyOReily


People also ask

What is a dockerfile in rust?

Dockerfile: A file named Dockerfile that contains the commands that Docker will run to build the image. In a Rust project, it lies alongside the manifest, that is, the Cargo.toml file; Image: When we run a command build we create an image that contains everything we specified in the Dockerfile. Running an image result in a container.

How do I shut down rust-server?

Once that's done, you should see the server starting up. Now, to shutdown the server, you can either press CTRL-C, which will initiate the shutdown procedure for rust-server, or you can open another terminal window and type in the following command to stop the server:

How do I set up a rust server in a container?

Give the container a name you prefer. Add mdarkness1988/rust-serverin Repository. Add the variables you want from the list below. Add ports to container, (8080/TCP, 28015/TCP&UDP, 28016/TCP). Save Template and RUN :) NOTE: This image will install/update on startup. The path /steamcmd/rustcan be mounted on the host for data persistence.

What happens when we run Docker build?

When we run docker build, only the modified layers are updated, the rest is retrieved from the local cache. To put it in practical terms, as long as we don't change the manifest, the dependencies will not have to be rebuilt.


1 Answers

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.

like image 126
toffan Avatar answered Oct 23 '22 13:10

toffan