Home > OS >  Rust actix-web not working in docker "refused to connect" error
Rust actix-web not working in docker "refused to connect" error

Time:12-10

Running this locally works as expected, however, when running it inside docker I cannot access the site. Everything I have seen has said make sure to bind to 0.0.0.0, which I am already so I'm at a loss.

Dockerfile

FROM rust:latest as builder
RUN apt-get update && apt-get -y install ca-certificates cmake musl-tools libssl-dev && rm -rf /var/lib/apt/lists/*
COPY . .
RUN rustup target add x86_64-unknown-linux-musl
ENV PKG_CONFIG_ALLOW_CROSS=1
RUN cargo build --target x86_64-unknown-linux-musl --release


FROM scratch
COPY --from=builder /target/x86_64-unknown-linux-musl/release/rusty-bits .
CMD ["/rusty-bits"]

Cargo.toml

[package]
name = "rusty-bits"
version = "0.1.0"
edition = "2018"


[dependencies]
actix-web = "3"
actix-rt = "2.0.0-beta.1"

main.rs

use actix_web::{web, App, HttpRequest, HttpServer, Responder};

async fn greet(req: HttpRequest) -> impl Responder {
    let name = req.match_info().get("name").unwrap_or("World");
    format!("Hello {}!", &name)
}

async fn health(_req: HttpRequest) -> impl Responder {
    "OK"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(greet))
            .route("/{name}", web::get().to(greet))
            .route("/health", web::get().to(health))
    })
        .bind(("0.0.0.0", 8000))?
        .run()
        .await
}

CodePudding user response:

I think there are 2 problems here:

  • Looks like you forgot to add EXPOSE 8000 to your dockerfile.
  • Since you forgot to add expose I supposed you also forgot to bind your port when running your image: docker run -p 8000:8000 image_name
  • Related