I am new in Rust and to study new language I have decided to write simple "server app" based on TcpListener. It's simple main function that looks like this:
fn main() {
println!("Start server");
let server = TcpListener::bind("127.0.0.1:7878").unwrap();
match server.accept() {
Ok((_socket, addr)) => {
println!("new client: {:?}", addr);
loop {
//do something
}
}
Err(e) => { println!("couldn't accept the client: {:?}", e) }
}
println!("server disconnect")
}
and also I have some client code that connect to listener
fn client() {
match TcpStream::connect("127.0.0.1:7878") {
Ok(mut stream) => {
println!("connect to server");
loop {
// do some work
}
}
Err(e) => { println!("couldn't connect to server: {:?}", e) }
}
println!("client disconnect")
}
When I run it from cmd everything works well but when I run it in docker conteiner I can not connect to listener
couldn't connect: Os { code: 10061, kind: ConnectionRefused, message: "No connection could be made because the target machine actively refused it." }
How can I fix it? I am new in Docker as in Rust so if you need more information pls leave a comment.
Dockerfile:
FROM rust:1.50
WORKDIR /usr/src/appname
COPY . .
RUN cargo install --path .
RUN cargo build --release
CMD cargo run
to run docker container I use docker run --rm -dp 7878:7878 appname
after that when I run client code it returns ok but server does not react (I expect that "new client" message will appear)