Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axum in docker fails returns 404 but cargo run works fine #2576

Closed
thebetterjort opened this issue Feb 8, 2024 · 0 comments
Closed

Axum in docker fails returns 404 but cargo run works fine #2576

thebetterjort opened this issue Feb 8, 2024 · 0 comments

Comments

@thebetterjort
Copy link

thebetterjort commented Feb 8, 2024

  • [𝕏] I have looked for existing issues (including closed) about this

Bug Report

I'm not able to access axum server from inside of a docker container.

Version

β”œβ”€β”€ axum v0.7.4
β”‚ β”œβ”€β”€ axum-core v0.4.3

Platform

Linux f44bf27f15bb 6.6.12-linuxkit #1 SMP Fri Jan 19 08:53:17 UTC 2024 aarch64 GNU/Linux
or M2 Pro
uname -r
23.3.0

Crates

[dependencies]
axum = {version = "0.7.2", features = ["tracing"]}
dotenv = "0.15.0"
tokio = { version = "1.27.0", features = ["full"] }
tower-http = { version = "0.5.0", features = ["cors", "fs"] }
common = {path = "../common"}
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
askama = "0.12.1"
rand = "0.8.5"

[dev-dependencies]
anyhow = "1.0.48"
httpc-test = "0.1.1"

Description

main.rs

mod route;

use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

use axum::http::{
    header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
    HeaderValue, Method,
};
// use dotenv::dotenv;
use tower_http::cors::CorsLayer;

#[tokio::main]
async fn main() {
    // Load environment variables from a .env file.
    // dotenv().ok();

    let num = 10;
    println!(
        "Common lib loaded: {num} plus one is {}!",
        common::add_one(num)
    );

    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "web=debug".into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    info!("hello, web server!");

    // Create a CORS layer to handle Cross-Origin Resource Sharing.
    let cors = CorsLayer::new()
        .allow_origin("http://0.0.0.0:3000".parse::<HeaderValue>().unwrap())
        .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE])
        .allow_credentials(true)
        .allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]);
    // Create the application router and attach the CORS layer.
    let app = route::create_index().layer(cors);

    // Bind the server to listen on the specified address and port.
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    println!("->> LISTENING on {:?}\n", listener.local_addr());
    // Start serving the application.
    axum::serve(listener, app).await.unwrap();
}

route.rs

use axum::Router;

use tower_http::services::ServeFile;

pub fn create_index() -> Router {
    Router::new().route_service("/", ServeFile::new("dist/index.html"))
}

Dockerfile

ARG PACKAGE=web
FROM rust:1.71.1 as builder
WORKDIR /app

COPY Cargo.toml .
RUN mkdir src && echo "fn main() {}" > src/main.rs
RUN cargo remove common -v
RUN cargo build --release --bin web

COPY src src
RUN touch src/main.rs

RUN cargo add --git https://github.com/federal-courts-software-factory/open-case-filing-system.git common
RUN cargo build --release --bin web

RUN strip target/release/web

FROM gcr.io/distroless/cc-debian12 as release
WORKDIR /app
COPY --from=builder /app/target/release/web .


EXPOSE 3000

CMD ["./web"]

Docker cmd
docker run -p 0.0.0.0:3000:3000 ghcr.io/federal-courts-software-factory/open-case-filing-system/web@sha256:f9aca3996cf60553d26907d3e987ba0fd654011e1b18f3e99078045e99b13215

Docker output:
2024-02-08T05:52:32.006717Z INFO web: hello, web server! ->> LISTENING on Ok(0.0.0.0:3000)

Cargo run works as expected with the index.html being served as expected.

@tokio-rs tokio-rs locked and limited conversation to collaborators Feb 8, 2024
@davidpdrsn davidpdrsn converted this issue into discussion #2577 Feb 8, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant