I am writing a Rust application based on Building a Single-Threaded Web Server . Starting the server is as follows:
// -- let listener = TcpListener::bind(&self.url).unwrap(); // URL: http://127.0.0.1:8080/ println!("Server started on {}", &self.url); for stream in listener.incoming() { let stream = stream.unwrap(); println!("Got connection"); handle_connection(stream); } // -- fn handle_connection(mut stream: TcpStream) { println!("Reading request"); let mut buffer = [0; 512]; stream.read(&mut buffer).unwrap(); let contents = r#" <SOME HTML> "#; let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents); println!("Writing response"); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); } // -- I want to deploy this to my remote server using Docker. But I don’t want my image to take up much space, so I use alpine as the basis for the image:
FROM alpine WORKDIR /usr/local/bin COPY target/x86_64-unknown-linux-musl/release/<filename> . COPY Config.toml . EXPOSE 8080 CMD ["<filename>"] In order to use alpine, before this I have to compile a file using the rust-musl-builder:
sudo docker run --rm -it -v `pwd`:/home/rust/src ekidd/rust-musl-builder After that I compile and run the Docker image:
sudo docker build --no-cache -t <tag> -f <dockerfile from above> <directory> sudo docker run --rm -dit --name <name> -p "8080:8080" <tag> After that, if I try to open http://127.0.0.1:8080/ in the browser, the connection is dropped: 
Moreover, if I launch netcat -l -p 8080 inside the image (instead of <filename> run sh , I will launch netcat -l -p 8080 ), when I try to open http://127.0.0.1:8080/ in my browser, netcat appears http request code and I can write an answer to it manually (for example, HTTP/1.1 404 NOT FOUND ... ), the answer reaches the browser.
Moreover, if I run <filename> locally, when opening http://127.0.0.1:8080/ everything works fine: 
At the same time, if you look at what <filename> displays running in the container, you can see that it writes only Server started on http://127.0.0.1:8080 , but never writes Got connection .
Question: what happens, why does the connection fail, if I run the program in Docker, but at the same time everything works locally?