1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

update docker sample

This commit is contained in:
Rob Ede 2022-01-27 02:58:13 +00:00
parent 6e34f07fbf
commit d327e8800b
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 887 additions and 1090 deletions

1908
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
Dockerfile

View File

@ -6,3 +6,5 @@ edition = "2018"
[dependencies]
actix-web = "3"
env_logger = "0.9"
log = "0.4"

View File

@ -1,24 +1,38 @@
FROM rust:1-slim-buster AS base
ENV USER=root
# NB: This is not a production-grade Dockerfile.
#################
## build stage ##
#################
FROM rust:1-slim-bullseye AS builder
WORKDIR /code
RUN cargo init
COPY Cargo.toml /code/Cargo.toml
# Download crates-io index and fetch dependency code.
# This step avoids needing to spend time on every build downloading the index
# which can take a long time within the docker context. Docker will cache it.
RUN USER=root cargo init
COPY Cargo.toml Cargo.toml
RUN cargo fetch
COPY src /code/src
# copy app files
COPY src src
CMD [ "cargo", "test", "--offline" ]
# compile app
RUN cargo build --release
FROM base AS builder
###############
## run stage ##
###############
FROM debian:bullseye-slim
WORKDIR /app
RUN cargo build --release --offline
# copy server binary from build stage
COPY --from=builder /code/target/release/docker_sample docker_sample
FROM debian:buster-slim
# set user to non-root unless root is required for your app
USER 1001
COPY --from=builder /code/target/release/docker_sample /usr/bin/docker_sample
# indicate what port the server is running on
EXPOSE 8080
EXPOSE 5000
ENTRYPOINT [ "/usr/bin/docker_sample" ]
# run server
CMD [ "/app/docker_sample" ]

View File

@ -1,26 +1,28 @@
#[macro_use]
extern crate actix_web;
use actix_web::{App, HttpResponse, HttpServer, Responder};
use actix_web::{get, middleware::Logger, 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_web::main]
async fn main() -> std::io::Result<()> {
println!("Starting actix-web server");
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
HttpServer::new(|| App::new().service(index).service(again))
.bind("0.0.0.0:5000")?
.run()
.await
log::info!("Starting HTTP server: go to http://localhost:8080");
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.service(index)
.service(again)
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}