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
Terminal Ouput
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?
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With