Skip to content

Commit

Permalink
feat(docker): containerize the application
Browse files Browse the repository at this point in the history
In this commit:
- Define scripts, env file and Dockerfile to build and run the container
- Get astarte config from env vars
- Add dependabot checks for docker
- Update Readme with the information to build and run a docker container

Signed-off-by: Riccardo Gallo <[email protected]>
  • Loading branch information
rgallor committed Nov 29, 2024
1 parent cc4695e commit 9018d92
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0

/.github
/.reuse
/scripts/docker
!/scripts/docker/entrypoint.sh
4 changes: 4 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ updates:
directory: "/"
schedule:
interval: weekly
- package-ecosystem: docker
directories: "**/*"
schedule:
interval: weekly
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,52 @@ You can also set the stream options by using the following environment variables
- `INTERFACE_NAME`
- `INTERVAL_BTW_SAMPLES`
- `SCALE`

## Docker

### Build the Container

First, ensure you have Docker installed on your machine, then:
1. Clone the repository containing the Stream Rust Test code
2. Navigate to the root directory of the repository
3. Run the build script to create the Docker image:
```sh
./scripts/docker/build.sh
```

### Run the Container

To run the container with your configuration file:
1. Ensure you have defined your astarte configuration file `config.toml`
2. Run the Docker container, mounting the configuration file:
```sh
docker run -v /path/to/your/config.toml:<MOUNT_TO_THIS_PATH> -e ASTARTE_CONFIG_PATH="<MOUNT_TO_THIS_PATH>" stream-rust-test:latest
```

Replace `/path/to/your/config.toml` with the actual path to your configuration file.

Note: `MOUNT_TO_THIS_PATH` must be an absolute path.

### ENV variables

You can configure the application with environment variables by exporting them (e.g. configuring
them in the
[docker-compose.yaml](https://docs.docker.com/compose/environment-variables/set-environment-variables/))
or via the `--env-file` CLI options:

```sh
docker run --env-file /path/to/your/.env stream-rust-test:latest
```

Consult the `--help` for a full list of environment variable names and options.

### Run the container in a separate network

If you are running a local astarte instance, such the one in
[Astarte in 5 minutes](https://docs.astarte-platform.org/astarte/latest/010-astarte_in_5_minutes.html),
you can either modify the `docker-compose.yaml` by adding the `stream-rust-test` container to it or you can run it
by using the `--network="host"` parameter, which is required to make `localhost` work.

```sh
docker run --network="host" [ENV VARS] [MOUNT config.toml] stream-rust-test:latest
```
7 changes: 7 additions & 0 deletions scripts/docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0

MATH_FUNCTION=rect
INTERVAL_BTW_SAMPLES=2000
SCALE=5
24 changes: 24 additions & 0 deletions scripts/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0

FROM rust:1.81-alpine3.20 AS build

RUN mkdir /stream-rust-test && apk update && apk upgrade && \
apk add --no-cache alpine-sdk musl-dev pkgconfig openssl-dev openssl-libs-static sqlite-static sqlite-dev

COPY . /stream-rust-test

WORKDIR /stream-rust-test

RUN cargo build --release

FROM alpine:3.20

COPY scripts/docker/entrypoint.sh /entrypoint.sh

COPY --from=build /stream-rust-test/target/release/stream-rust-test /usr/bin/

RUN mkdir -p /tmp/stream-rust-test/store/

CMD ["/entrypoint.sh"]
14 changes: 14 additions & 0 deletions scripts/docker/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

# Copyright 2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0

set -exEuo pipefail

repo=$(git rev-parse --show-toplevel)

cd "$repo"

# Run the docker build context from the root of the repo
docker build -f scripts/docker/Dockerfile . -t stream-rust-test:latest
9 changes: 9 additions & 0 deletions scripts/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env sh

# Copyright 2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0

set -exu

exec stream-rust-test

0 comments on commit 9018d92

Please sign in to comment.