From 50332ca491113d4fd6e2c6c75117589f25775391 Mon Sep 17 00:00:00 2001 From: curiousdev Date: Mon, 24 Feb 2020 10:48:04 -0700 Subject: [PATCH] Add `docker_sample` to examples collection --- Cargo.toml | 1 + docker_sample/Cargo.toml | 11 +++++++++++ docker_sample/Dockerfile | 15 +++++++++++++++ docker_sample/README.MD | 23 +++++++++++++++++++++++ docker_sample/src/main.rs | 30 ++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 docker_sample/Cargo.toml create mode 100644 docker_sample/Dockerfile create mode 100644 docker_sample/README.MD create mode 100644 docker_sample/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index c296bc23..dddde2da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,4 +45,5 @@ members = [ "websocket-chat", # "websocket-chat-broker", "websocket-tcp-chat", + "docker_sample", ] diff --git a/docker_sample/Cargo.toml b/docker_sample/Cargo.toml new file mode 100644 index 00000000..ba94f64a --- /dev/null +++ b/docker_sample/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "docker_sample" +version = "0.1.0" +authors = ["docker_sample "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +actix-rt = "1.0.0" +actix-web = "2.0.0" \ No newline at end of file diff --git a/docker_sample/Dockerfile b/docker_sample/Dockerfile new file mode 100644 index 00000000..768a885c --- /dev/null +++ b/docker_sample/Dockerfile @@ -0,0 +1,15 @@ +FROM rust:1 as builder + +COPY . . + +RUN cargo build --release + +FROM rust:1-slim-stretch + +COPY --from=builder /target/release/docker_sample . + +RUN ls -la /docker_sample + +EXPOSE 5000 + +ENTRYPOINT ["/docker_sample"] diff --git a/docker_sample/README.MD b/docker_sample/README.MD new file mode 100644 index 00000000..0586729a --- /dev/null +++ b/docker_sample/README.MD @@ -0,0 +1,23 @@ + +Build image: + +```shell + docker build -t docker_sample --force-rm --no-cache -f Dockerfile . +``` + +Run image: + +```shell + + echo "== start sample_docker" + docker run -d -p 5000:5000 docker_sample & + docker ps + + echo "== wait 3s for startup" + sleep 3s + + echo "== curl both routes" + curl http://localhost:5000 + curl http://localhost:5000/again + +``` \ No newline at end of file diff --git a/docker_sample/src/main.rs b/docker_sample/src/main.rs new file mode 100644 index 00000000..803e0b6b --- /dev/null +++ b/docker_sample/src/main.rs @@ -0,0 +1,30 @@ +#[macro_use] +extern crate actix_web; + +use actix_web::{web, App, HttpResponse, HttpServer, Responder}; + +#[get("/")] +async fn index() -> impl Responder { + println!("GET: /"); + HttpResponse::Ok().body("Hello world!") +} + +#[get("/again")] +async fn again() -> impl Responder { + println!("GET: /again"); + HttpResponse::Ok().body("Hello world again!") +} + +#[actix_rt::main] +async fn main() -> std::io::Result<()> { + println!("Starting actix-web server"); + + HttpServer::new(|| { + App::new() + .service(index) + .service(again) + }) + .bind("0.0.0.0:5000")? + .run() + .await +} \ No newline at end of file