rust program (server) will remain running, but will close immediately running as a docker image

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 –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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();
}
</code>
<code>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(); } </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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"]
</code>
<code>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"] </code>
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.

7

Not sure what’s wrong on your end, but on my machine your code works differently than what you described.

main.rs and Cargo.toml are the same, I modified Dockerfile slightly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ARG RUST_VERSION=1.70.0
ARG APP_NAME=rust-image
FROM rust:${RUST_VERSION}-alpine AS build
RUN apk add --no-cache clang lld musl-dev git
WORKDIR /app
COPY src /app/src
COPY Cargo.toml /app
ARG APP_NAME
RUN cargo build --release &&
cp ./target/release/axum-project /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"]
</code>
<code>ARG RUST_VERSION=1.70.0 ARG APP_NAME=rust-image FROM rust:${RUST_VERSION}-alpine AS build RUN apk add --no-cache clang lld musl-dev git WORKDIR /app COPY src /app/src COPY Cargo.toml /app ARG APP_NAME RUN cargo build --release && cp ./target/release/axum-project /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"] </code>
ARG RUST_VERSION=1.70.0
ARG APP_NAME=rust-image

FROM rust:${RUST_VERSION}-alpine AS build

RUN apk add --no-cache clang lld musl-dev git

WORKDIR /app
COPY src /app/src
COPY Cargo.toml /app

ARG APP_NAME


RUN cargo build --release && 
    cp ./target/release/axum-project /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"] 

Then:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cd /path/to/project
docker build -t foo .
docker run -it --rm -p 3000:3000 foo
</code>
<code>cd /path/to/project docker build -t foo . docker run -it --rm -p 3000:3000 foo </code>
cd /path/to/project
docker build -t foo .
docker run -it --rm -p 3000:3000 foo

in another terminal:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>curl localhost:3000
</code>
<code>curl localhost:3000 </code>
curl localhost:3000

each time I do curl I get Hello world back.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật