This project demonstrates how to build and run a Rust application using Docker, with a focus on security and efficiency.
- Build the Docker image:
docker build -t servercrust/cloud-run-app .
- Run the Docker image:
docker run --init -p 8080:8080 -t servercrust/cloud-run-app
Note: The --init option is optional.
- Test app using curl
curl localhost:8000
- Load test using wrk with 8 threads, 256 connections for 30 seconds duration (about 50k req/sec)
wrk -t8 -c256 -d30s http://localhost:8080
Running 30s test @ http://localhost:8080
8 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.81ms 4.35ms 144.35ms 87.94%
Req/Sec 7.39k 839.75 15.28k 80.21%
1767602 requests in 30.07s, 219.14MB read
Requests/sec: 58780.61
Transfer/sec: 7.29MB
A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software. Images are built from a Dockerfile, which contains instructions to generate the image.
Multistage builds in Docker allow the use of multiple FROM statements in a Dockerfile. Each FROM instruction begins a new stage of the build, potentially using a different base image.
- Smaller final images
- Improved security
- Better organization
- Efficient caching
Our Dockerfile uses a four-stage build:
- Planner stage: Creates a dependency recipe using cargo-chef
- Cacher stage: Builds dependencies based on the recipe
- Builder stage: Compiles the application
- Final stage: Creates a lean runtime image
Distroless Docker images are minimalist images containing only the essentials to run an application. They lack typical OS tools and shells, resulting in a smaller attack surface and enhanced security.
- Reduced attack surface
- Minimized vulnerabilities
- No shell access for potential attackers
- Backed by Google's expertise in cloud security
Cargo Chef is a tool that optimizes Rust Docker builds by separating dependency building from application code building.
- Dependency Planning: Creates a "recipe" file of project dependencies
- Dependency Building: Builds dependencies using the recipe
- Caching: Allows Docker to cache the dependency-building step
- Application Building: Compiles application code with pre-built dependencies
- Faster builds
- Reduced build times
- Smaller images
Creating a least privilege user is crucial for container security. This practice ensures that processes have only the minimum necessary privileges to function.
- Reduced attack surface
- Prevention of privilege escalation
- Compliance with security policies
- Limited accidental damage
- Enhanced container isolation
We welcome contributions! Please see our Contributing Guide for more details.
This project is licensed under the MIT License.