Skip to content

Commit

Permalink
Basic example of docker rust
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunjun committed Feb 24, 2024
1 parent dabe44d commit 89a4f9b
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions rust/docker-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "docker-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.2.1"
10 changes: 10 additions & 0 deletions rust/docker-rust/Dockerfile-basic
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Use the main rust Docker image(메인 rust 도커 이미지를 사용한다)
FROM rust
# copy app into docker image(app을 도커 이미지에 복사한다)
COPY . /app
# Set the workdirectory(작업 디렉터리를 설정한다)
WORKDIR /app
# build the app(앱을 빌드한다)
RUN cargo build --release
# start the application(애플리케이션을 시작한다)
CMD ["./target/release/docker-rust"]
14 changes: 14 additions & 0 deletions rust/docker-rust/Dockerfile-lite
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Use the main rust Docker image(메인 rust 도커 이미지를 사용한다)
FROM rust as build
# copy app into docker image(app을 도커 이미지에 복사한다)
COPY . /app
# Set the workdirectory(작업 디렉터리를 설정한다)
WORKDIR /app
# build the app(앱을 빌드한다)
RUN cargo build --release
# start the application(애플리케이션을 시작한다)

FROM gcr.io/distroless/cc-debian12
COPY --from=build /app/target/release/docker-rust /app/docker/docker-rust

CMD ["./app/docker/docker-rust"]
14 changes: 14 additions & 0 deletions rust/docker-rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Docker-rust
===========
- build & execution

```
docker build -f Dockerfile-basic . -t docker-rust-basic
docker run -p 8080:8080 -t docker-rust-basic
docker build -f Dockerfile-lite . -t docker-rust-lite
docker run -p 8080:8080 -t docker-rust-lite
```
- ![SCR-20240224-rmfg.png](./images/SCR-20240224-rmfg.png)
- Necessary to use appropriate version, e.g. when using `cc-debian11`, got this error
- ![SCR-20240224-rmsa.png](./images/SCR-20240224-rmsa.png)
Binary file added rust/docker-rust/images/SCR-20240224-rmfg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rust/docker-rust/images/SCR-20240224-rmsa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions rust/docker-rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
#[get("/")]
async fn gm() -> impl Responder {
HttpResponse::Ok().body("Hello, Good morning!")
}
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello there!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(gm)
.route("/hello", web::get().to(hello))
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}

0 comments on commit 89a4f9b

Please sign in to comment.