Skip to content

Commit

Permalink
Add Docker for Server-Based gRPC and REST gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
rsachdeva committed Oct 10, 2024
1 parent 2111620 commit 062466b
Show file tree
Hide file tree
Showing 23 changed files with 311 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RUST_LOG = { value = "drive_deposits_rest_types=debug,drive_deposits_proto_grpc_types=debug,drive_deposits_event_source=debug,drive_deposits_lambda_db_types=debug,drive_deposits_logs_lambda_target=debug,by_level_lambda_writer=debug,drive_deposits_lambda_dynamodb_reader=debug,by_level_lambda_reader=debug,drive_deposits_cal_types=debug,drive_deposits_check_cmd=debug,drive_deposits_grpc_server=debug,drive_deposits_rest_gateway_server=debug", force = true }


GRPC_SERVER_ADDRESS = "http://[::]:50052"

# default settings -- can be changed by setting these env. variables on command line
SEND_CAL_EVENTS = "true"
USE_LOCALSTACK = "false"
Expand Down
34 changes: 34 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
.idea/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/.idea
**/*.iml
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose.y*ml
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/secrets.dev.yaml
**/values.dev.yaml
/bin
/target
LICENSE
README.md
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ target/
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

**/.env

20 changes: 10 additions & 10 deletions Cargo.lock

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

83 changes: 83 additions & 0 deletions Dockerfile.for.compose.grpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# syntax=docker/dockerfile:1

ARG RUST_VERSION=1.81.0
ARG APP_NAME=drive-deposits-grpc-server

################################################################################
# Create a stage for building the application.

FROM rust:${RUST_VERSION}-alpine AS build
ARG APP_NAME
# not used as we are using a bind mount with absolute path
WORKDIR /drive-deposits

# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev git

RUN apk add --no-cache protobuf-dev


# Build the application.
# Mounting for caching dependencies and build artifacts
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/tmp/cargo-target \
--mount=type=bind,source=.,target=/tmp/build \
set -x && \
ls -la /drive-deposits && \
cd /tmp/build && \
mkdir -p /tmp/cargo-target && \
CARGO_TARGET_DIR=/tmp/cargo-target cargo build --package $APP_NAME --locked --release && \
echo "Contents of /tmp/cargo-target/release/ directory:" && \
ls -la /tmp/cargo-target/release/ && \
if [ -f /tmp/cargo-target/release/$APP_NAME ]; then \
mkdir -p /bin/server && \
cp /tmp/cargo-target/release/$APP_NAME /bin/server/$APP_NAME && \
echo "Contents of /bin/server directory:" && \
ls -la /bin/server; \
else \
echo "Binary $APP_NAME not found in /tmp/cargo-target/release/"; \
exit 1; \
fi

#################################################################################
# Create a new stage for running the application
# could use distroless
# FROM gcr.io/distroless/cc-debian11
# or slim
# FROM debian:buster-slim
# this is docker init by default
FROM alpine:3.18 AS final

WORKDIR /bin

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser

WORKDIR /bin

COPY --from=build /bin/server/$APP_NAME .

RUN echo "Contents of workdir /bin directory for drive-deposits-grpc-server in final:" && \
ls -la /bin


EXPOSE 50052

ENV RUST_LOG="drive_deposits_rest_types=debug,drive_deposits_proto_grpc_types=debug,drive_deposits_event_source=debug,drive_deposits_cal_types=debug,drive_deposits_grpc_server=debug"

ENV SEND_CAL_EVENTS="true"
ENV USE_LOCALSTACK="false"


CMD ["/bin/drive-deposits-grpc-server"]
80 changes: 80 additions & 0 deletions Dockerfile.for.compose.rest.gateway
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# syntax=docker/dockerfile:1

ARG RUST_VERSION=1.81.0
ARG APP_NAME=drive-deposits-rest-gateway-server

################################################################################
# Create a stage for building the application.

FROM rust:${RUST_VERSION}-alpine AS build
ARG APP_NAME
# not used as we are using a bind mount with absolute path
WORKDIR /drive-deposits

# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev git

RUN apk add --no-cache protobuf-dev


# Build the application.
# Mounting for caching dependencies and build artifacts
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/tmp/cargo-target \
--mount=type=bind,source=.,target=/tmp/build \
set -x && \
ls -la /drive-deposits && \
cd /tmp/build && \
mkdir -p /tmp/cargo-target && \
CARGO_TARGET_DIR=/tmp/cargo-target cargo build --package $APP_NAME --locked --release && \
echo "Contents of /tmp/cargo-target/release/ directory:" && \
ls -la /tmp/cargo-target/release/ && \
if [ -f /tmp/cargo-target/release/$APP_NAME ]; then \
mkdir -p /bin/server && \
cp /tmp/cargo-target/release/$APP_NAME /bin/server/$APP_NAME && \
echo "Contents of /bin/server directory:" && \
ls -la /bin/server; \
else \
echo "Binary $APP_NAME not found in /tmp/cargo-target/release/"; \
exit 1; \
fi

#################################################################################
# Create a new stage for running the application
# could use distroless
# FROM gcr.io/distroless/cc-debian11
# or slim
# FROM debian:buster-slim
# this is docker init by default
FROM alpine:3.18 AS final

WORKDIR /bin

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser

WORKDIR /bin

COPY --from=build /bin/server/$APP_NAME .

RUN echo "Contents of workdir /bin directory for drive-deposits-rest-gateway-server in final:" && \
ls -la /bin

EXPOSE 3000

ENV RUST_LOG="drive_deposits_rest_types=debug,drive_deposits_proto_grpc_types=debug,drive_deposits_rest_gateway_server=debug"

ENV GRPC_SERVER_ADDRESS="http://drive-deposits-grpc-server:50052"

CMD ["/bin/drive-deposits-rest-gateway-server"]
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- [Synchronous Microservices Components](#synchronous-microservices-components)
- [Asynchronous Microservices Components](#asynchronous-microservices-components)
- [Bridging Synchronous and Asynchronous Components In DriveDeposits Microservices](#bridging-synchronous-and-asynchronous-components-in-drivedeposits-microservices)
- [Deployment of DriveDeposits Microservices](#deployment-of-drivedeposits-microservices)
- [Deployment of Microservices](#deployment-of-microservices)
- [Hybrid Integration Testing Tool](#hybrid-integration-testing-tool)
- [Running Tests](#running-tests)
- [Integration tests](#integration-tests)
Expand Down Expand Up @@ -251,7 +251,9 @@ microservices ecosystem.

[Back to Table of Contents](#table-of-contents)

### Deployment of DriveDeposits Microservices
### Deployment of Microservices

- **Serverless**: AWS using SAM

This project uses SAM (Serverless Application Model) for deploying the following AWS resources that support our
microservices architecture:
Expand Down Expand Up @@ -293,10 +295,18 @@ related resources.

`just deploy-drive-deposits-dynamodb-queries-only`

#### Run server-based microservices (REST gateway and gRPC Servers)
- **Server-based**:
- **Natively** (without Docker)

`just run-drive-deposits-grpc-server`
`just run-drive-deposits-rest-grpc-gateway-server`
`just run-drive-deposits-grpc-server`

`just run-drive-deposits-rest-grpc-gateway-server`

- **Docker Compose**

`just compose-up-grpc-server`

`just compose-up-rest-server`

#### Test microservices integration

Expand Down Expand Up @@ -444,7 +454,7 @@ Following is convenience so that in development can iterate faster:

Should see "Ready." -- There is a Terminal now in Docker Desktop itself so that is a good place to run this command.

#### deploy everything in localstack -- aws deployment related commands for EventBridge, EventBus, Cloudwatch log groups and Lambda target function for writing to DynamoDB and Lambda DynamoDB reader
#### deploy everything serverless in Localstack - aws deployment related commands for EventBridge, EventBus, Cloudwatch log groups and Lambda target function for writing to DynamoDB and Lambda DynamoDB reader

`just localstack-deploy-drive-deposits-dynamodb-queries`

Expand Down
18 changes: 18 additions & 0 deletions compose.grpc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
drive-deposits-grpc-server:
build:
context: .
target: final
dockerfile: Dockerfile.for.compose.grpc
ports:
- "50052:50052"
networks:
- drive-deposits-network
environment:
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}

networks:
drive-deposits-network:
driver: bridge
14 changes: 14 additions & 0 deletions compose.rest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
drive-deposits-rest-gateway-server:
build:
context: .
target: final
dockerfile: Dockerfile.for.compose.rest.gateway
ports:
- "3000:3000"
networks:
- drive-deposits-network

networks:
drive-deposits-network:
driver: bridge
2 changes: 1 addition & 1 deletion drive-deposits-cal-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "drive-deposits-cal-types"
version = "0.30.0"
version = "0.50.0"
edition = "2021"

[dependencies]
Expand Down
Loading

0 comments on commit 062466b

Please sign in to comment.