Skip to content

Commit f02cc9d

Browse files
example of rust hello world container (#88)
* example of rust hello world container * typo * review
1 parent 3ee1cbe commit f02cc9d

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

containers/rust-hello-world/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "hello-world"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the official Rust image as the build stage
2+
FROM rust:1.79.0-alpine3.20 as builder
3+
4+
# Set the working directory inside the container
5+
WORKDIR /usr/src/app
6+
7+
# Copy the source code to the working directory
8+
COPY . .
9+
10+
# Build the Rust application
11+
RUN cargo build --release
12+
13+
# Use a smaller base image for the final container
14+
FROM alpine:3.20
15+
16+
# Copy the compiled binary from the build stage
17+
COPY --from=builder /usr/src/app/target/release/hello-world /usr/local/bin/hello-world
18+
19+
# Set the entrypoint command
20+
CMD ["hello-world"]

containers/rust-hello-world/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Rust hello world
2+
3+
This example demonstrates the deployment of a simple rust http service on Scaleway Serverless Containers.
4+
5+
This can be useful if you come from Serverless Functions and you need to install specific dependencies on your system.
6+
7+
For this example, we will use the CLI to deploy the container, but you can use [other methods](https://www.scaleway.com/en/docs/serverless/containers/reference-content/deploy-container/).
8+
You can also use the CLI directly from Scaleway console without having to use your credentials.
9+
10+
## Workflow
11+
12+
Here are the different steps we are going to proceed:
13+
14+
- Quick set-up of Container Registry to host our rust container
15+
- Deploy the Serverless Container
16+
- Test the container
17+
18+
## Deployment
19+
20+
### Requirements
21+
22+
To complete the actions presented below, you must have:
23+
- installed and configured the [Scaleway CLI](https://www.scaleway.com/en/docs/developer-tools/scaleway-cli/quickstart/)
24+
- installed [Docker](https://docs.docker.com/engine/install/) to build the image
25+
26+
### Building the image
27+
28+
1. Run the following command in a terminal to create Container Registry namespace to store the image:
29+
30+
```bash
31+
scw registry namespace create name=hello-rust
32+
```
33+
34+
The registry namespace information displays.
35+
36+
1. Copy the namespace endpoint (in this case, `rg.fr-par.scw.cloud/hello-rust`).
37+
38+
1. Log into the Container Registry namespace you created using Docker:
39+
40+
```bash
41+
docker login rg.fr-par.scw.cloud/hello-rust -u nologin --password-stdin <<< "$SCW_SECRET_KEY"
42+
```
43+
44+
At this point, you have correctly set up Docker to be able to push your image online.
45+
46+
1. In a terminal, access this directory (containing the Dockerfile), and run the following command to build the image:
47+
48+
```bash
49+
docker build -t crabnet:v1.0.0 .
50+
```
51+
52+
1. Tag and push the image to the registry namespace:
53+
54+
```bash
55+
docker tag crabnet:v1.0.0 rg.fr-par.scw.cloud/hello-rust/crabnet:v1.0.0
56+
docker push rg.fr-par.scw.cloud/hello-rust/crabnet:v1.0.0
57+
```
58+
59+
### Deploying the image
60+
61+
In a terminal, run the following command to create a Serverless Containers namespace:
62+
63+
```bash
64+
scw container namespace create name=crabnet
65+
```
66+
The namespace information displays.
67+
68+
1. Copy the namespace ID.
69+
70+
1. Run the following command to create and deploy the container:
71+
72+
```bash
73+
scw container container create namespace-id=<PREVIOUS_NAMESPACE_ID> name=crabnet registry-image=rg.fr-par.scw.cloud/hello-rust/crabnet:v1.0.0
74+
```
75+
The container information displays.
76+
77+
1. Copy the DomainName (endpoint) to test your container, you can put the endpoint in your web browser for testing.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::{
2+
io::{prelude::*, BufReader},
3+
net::{TcpListener, TcpStream},
4+
};
5+
6+
fn main() {
7+
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
8+
9+
for stream in listener.incoming() {
10+
let stream = stream.unwrap();
11+
12+
handle_connection(stream);
13+
}
14+
}
15+
16+
fn handle_connection(mut stream: TcpStream) {
17+
let buf_reader = BufReader::new(&mut stream);
18+
19+
// variable not used but you can manipulate it to get useful informations of the incoming request and manage parameters.
20+
let _http_request: Vec<_> = buf_reader
21+
.lines()
22+
.map(|result| result.unwrap())
23+
.take_while(|line| !line.is_empty())
24+
.collect();
25+
26+
let status_line = "HTTP/1.1 200 OK";
27+
let contents = "hello world";
28+
let length = contents.len();
29+
30+
let response = format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
31+
32+
stream.write_all(response.as_bytes()).unwrap();
33+
}

0 commit comments

Comments
 (0)