Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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
}

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading