Skip to content

Commit

Permalink
example of rust hello world container (#88)
Browse files Browse the repository at this point in the history
* example of rust hello world container

* typo

* review
  • Loading branch information
thomas-tacquet authored Jun 24, 2024
1 parent 3ee1cbe commit f02cc9d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions containers/rust-hello-world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions containers/rust-hello-world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

[dependencies]
20 changes: 20 additions & 0 deletions containers/rust-hello-world/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use the official Rust image as the build stage
FROM rust:1.79.0-alpine3.20 as builder

# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy the source code to the working directory
COPY . .

# Build the Rust application
RUN cargo build --release

# Use a smaller base image for the final container
FROM alpine:3.20

# Copy the compiled binary from the build stage
COPY --from=builder /usr/src/app/target/release/hello-world /usr/local/bin/hello-world

# Set the entrypoint command
CMD ["hello-world"]
77 changes: 77 additions & 0 deletions containers/rust-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Rust hello world

This example demonstrates the deployment of a simple rust http service on Scaleway Serverless Containers.

This can be useful if you come from Serverless Functions and you need to install specific dependencies on your system.

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/).
You can also use the CLI directly from Scaleway console without having to use your credentials.

## Workflow

Here are the different steps we are going to proceed:

- Quick set-up of Container Registry to host our rust container
- Deploy the Serverless Container
- Test the container

## Deployment

### Requirements

To complete the actions presented below, you must have:
- installed and configured the [Scaleway CLI](https://www.scaleway.com/en/docs/developer-tools/scaleway-cli/quickstart/)
- installed [Docker](https://docs.docker.com/engine/install/) to build the image

### Building the image

1. Run the following command in a terminal to create Container Registry namespace to store the image:

```bash
scw registry namespace create name=hello-rust
```

The registry namespace information displays.

1. Copy the namespace endpoint (in this case, `rg.fr-par.scw.cloud/hello-rust`).

1. Log into the Container Registry namespace you created using Docker:

```bash
docker login rg.fr-par.scw.cloud/hello-rust -u nologin --password-stdin <<< "$SCW_SECRET_KEY"
```

At this point, you have correctly set up Docker to be able to push your image online.

1. In a terminal, access this directory (containing the Dockerfile), and run the following command to build the image:

```bash
docker build -t crabnet:v1.0.0 .
```

1. Tag and push the image to the registry namespace:

```bash
docker tag crabnet:v1.0.0 rg.fr-par.scw.cloud/hello-rust/crabnet:v1.0.0
docker push rg.fr-par.scw.cloud/hello-rust/crabnet:v1.0.0
```

### Deploying the image

In a terminal, run the following command to create a Serverless Containers namespace:

```bash
scw container namespace create name=crabnet
```
The namespace information displays.

1. Copy the namespace ID.

1. Run the following command to create and deploy the container:

```bash
scw container container create namespace-id=<PREVIOUS_NAMESPACE_ID> name=crabnet registry-image=rg.fr-par.scw.cloud/hello-rust/crabnet:v1.0.0
```
The container information displays.

1. Copy the DomainName (endpoint) to test your container, you can put the endpoint in your web browser for testing.
33 changes: 33 additions & 0 deletions containers/rust-hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};

fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();

for stream in listener.incoming() {
let stream = stream.unwrap();

handle_connection(stream);
}
}

fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&mut stream);

// variable not used but you can manipulate it to get useful informations of the incoming request and manage parameters.
let _http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();

let status_line = "HTTP/1.1 200 OK";
let contents = "hello world";
let length = contents.len();

let response = format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");

stream.write_all(response.as_bytes()).unwrap();
}

0 comments on commit f02cc9d

Please sign in to comment.