dockerrustrust-actix

Rust actix_web inside docker isn't attainable, why?


I'm trying to make a docker container of my rust programme, let's look

Dockerfile

FROM debian

RUN apt-get update && \
    apt-get -y upgrade && \
    apt-get -y install git curl g++ build-essential

RUN curl https://sh.rustup.rs -sSf | bash -s -- -y

WORKDIR /usr/src/app

RUN git clone https://github.com/unegare/rust-actix-rest.git

RUN ["/bin/bash", "-c", "source $HOME/.cargo/env; cd ./rust-actix-rest/; cargo build --release; mkdir uploaded"]

EXPOSE 8080

ENTRYPOINT ["/bin/bash", "-c", "echo 'Hello there!'; source $HOME/.cargo/env; cd ./rust-actix-rest/; cargo run --release"]

cmd to run: docker run -it -p 8080:8080 rust_rest_api/dev

but curl from outside curl -i -X POST -F files[]=@img.png 127.0.0.1:8080/upload results into curl: (56) Recv failure: Соединение разорвано другой стороной i.e. refused by the other side of the channel

but inside the container:

root@43598d5d9e85:/usr/src/app# lsof -i
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
actix_003   6 root    3u  IPv4 319026      0t0  TCP localhost:http-alt (LISTEN)

but running the programme without docker works properly and processes the same request from curl adequately.

and inside the container:

root@43598d5d9e85:/usr/src/app# curl -i -X POST -F files[]=@i.jpg 127.0.0.1:8080/upload
HTTP/1.1 100 Continue

HTTP/1.1 201 Created
content-length: 70
content-type: application/json
date: Wed, 24 Jul 2019 08:00:54 GMT

{"keys":["uploaded/5nU1nHznvKRGbkQaWAGJKpLSG4nSAYfzCdgMxcx4U2mF.jpg"]}

What is the problem from outside?


Solution

  • If you're like myself and followed the examples on the Actix website, you might have written something like this, or some variation thereof:

    fn main() {
        HttpServer::new(|| {
            App::new()
                .route("/", web::get().to(index))
                .route("/again", web::get().to(index2))
        })
        .bind("127.0.0.1:8088")
        .unwrap()
        .run()
        .unwrap();
    }
    

    The issue here is that you're binding to a specific IP, rather than using 0.0.0.0 to bind to all IPs on the host container. I had the same issue as you and solved it by changing my code to:

    fn main() {
        HttpServer::new(|| {
            App::new()
                .route("/", web::get().to(index))
                .route("/again", web::get().to(index2))
        })
        .bind("0.0.0.0:8088")
        .unwrap()
        .run()
        .unwrap();
    }
    

    This might not be the issue for you, I couldn't know without seeing the code to run the server.