I have a very simple “Hello world” server that i wrote in RUST, the program is running correctly locally (not as an image), cargo run
Here is my main.rs file –
use axum::{routing::get, Router};
use std::net::SocketAddr;
use tracing_subscriber;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
here is my dockerfile that was generated using docker init
https://forms.gle/ybq9Krt8jtBL3iCk7
ARG RUST_VERSION=1.70.0
ARG APP_NAME=rust-image
FROM rust:${RUST_VERSION}-alpine AS build
ARG APP_NAME
WORKDIR /app
RUN apk add --no-cache clang lld musl-dev git
RUN --mount=type=bind,source=src,target=src
--mount=type=bind,source=Cargo.toml,target=Cargo.toml
--mount=type=bind,source=Cargo.lock,target=Cargo.lock
--mount=type=cache,target=/app/target/
--mount=type=cache,target=/usr/local/cargo/git/db
--mount=type=cache,target=/usr/local/cargo/registry/
cargo build --locked --release &&
cp ./target/release/$APP_NAME /bin/server
FROM alpine:3.18 AS final
ARG UID=10001
RUN adduser
--disabled-password
--gecos ""
--home "/nonexistent"
--shell "/sbin/nologin"
--no-create-home
--uid "${UID}"
appuser
USER appuser
COPY --from=build /bin/server /bin/
EXPOSE 3000
CMD ["/bin/server"]
Whenever i run the image, ill get 2024-07-10 12:23:01 Hello, world!
and the container will shut down.