From 81d7cf4781250a68a61fe706cbeed1daa890c042 Mon Sep 17 00:00:00 2001 From: Sean Tin Date: Sun, 20 Oct 2024 00:45:53 +0800 Subject: [PATCH 1/2] Add docker file --- apps/matching-service/Dockerfile | 27 +++++++++++++++++++++++++++ apps/matching-service/README.md | 12 ++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 apps/matching-service/Dockerfile diff --git a/apps/matching-service/Dockerfile b/apps/matching-service/Dockerfile new file mode 100644 index 0000000000..5c4b53cf03 --- /dev/null +++ b/apps/matching-service/Dockerfile @@ -0,0 +1,27 @@ +# Stage 1: Build Go Application + +FROM golang:1.23 AS builder + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ + +RUN go mod tidy && go mod download && go mod verify + +# Copy the .env file to the final image +COPY .env /usr/src/app/.env + +COPY . . + +RUN go build -v -o /usr/local/bin/app ./main.go + +# Stage 2: Setup Redis and combine with Go +FROM redis:latest + +# Copy the built Go binary from the builder stage +COPY --from=builder /usr/local/bin/app /usr/local/bin/app + +EXPOSE 8081 6379 + +# Start both Redis and the Go application +CMD ["sh", "-c", "redis-server & app; wait"] diff --git a/apps/matching-service/README.md b/apps/matching-service/README.md index 702d3dcdf3..a36bb0ffdd 100644 --- a/apps/matching-service/README.md +++ b/apps/matching-service/README.md @@ -121,6 +121,14 @@ Make sure to open the HTML file in a web browser while the WebSocket server is r You can open one instance of the HTML file in multiple tabs to simulate multiple clients connecting to the server. (In the future: ensure that only one connection is allowed per user) -## Docker Support +## Running the Application via Docker -TODO: Add section for Docker setup and usage instructions. +To run the application via Docker, run the following command: + +```bash +docker build -t matching-service . +``` + +```bash +docker run -d -p 8081:8081 -p 6379:6379 --name matching-service-container matching-service +``` From 35de547dda6cb46b2c921bae86ae239852a6fe15 Mon Sep 17 00:00:00 2001 From: Sean Tin Date: Sun, 20 Oct 2024 02:45:03 +0800 Subject: [PATCH 2/2] Add dockerfile for matching service --- apps/matching-service/.env.example | 7 ++++++- apps/matching-service/Dockerfile | 17 ++++------------- apps/matching-service/README.md | 16 +++++++++++++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/apps/matching-service/.env.example b/apps/matching-service/.env.example index fb40560876..4c1b93be9e 100644 --- a/apps/matching-service/.env.example +++ b/apps/matching-service/.env.example @@ -1,4 +1,9 @@ PORT=8081 MATCH_TIMEOUT=10 JWT_SECRET=you-can-replace-this-with-your-own-secret -REDIS_URL=localhost:6379 \ No newline at end of file + +# if you are NOT USING docker, use the below url +REDIS_URL=localhost:6379 + +# if you are USING docker, use the below url +# REDIS_URL=redis-container:6379 \ No newline at end of file diff --git a/apps/matching-service/Dockerfile b/apps/matching-service/Dockerfile index 5c4b53cf03..5abd2a597c 100644 --- a/apps/matching-service/Dockerfile +++ b/apps/matching-service/Dockerfile @@ -1,27 +1,18 @@ -# Stage 1: Build Go Application - -FROM golang:1.23 AS builder +FROM golang:1.23 WORKDIR /usr/src/app +# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change COPY go.mod go.sum ./ RUN go mod tidy && go mod download && go mod verify -# Copy the .env file to the final image COPY .env /usr/src/app/.env COPY . . RUN go build -v -o /usr/local/bin/app ./main.go -# Stage 2: Setup Redis and combine with Go -FROM redis:latest - -# Copy the built Go binary from the builder stage -COPY --from=builder /usr/local/bin/app /usr/local/bin/app - -EXPOSE 8081 6379 +EXPOSE 8081 -# Start both Redis and the Go application -CMD ["sh", "-c", "redis-server & app; wait"] +CMD ["app"] diff --git a/apps/matching-service/README.md b/apps/matching-service/README.md index a36bb0ffdd..dbbd97860e 100644 --- a/apps/matching-service/README.md +++ b/apps/matching-service/README.md @@ -27,7 +27,7 @@ go mod tidy - `PORT`: Specifies the port for the WebSocket server. Default is `8081`. - `JWT_SECRET`: The secret key used to verify JWT tokens. - `MATCH_TIMEOUT`: The time in seconds to wait for a match before timing out. -- `REDIS_URL`: The URL for the Redis server. Default is `localhost:6379`. +- `REDIS_URL`: The URL for the Redis server. Default is `localhost:6379`. If you are using docker, use `redis-container:6379` 4. Start a local redis server: @@ -123,12 +123,22 @@ You can open one instance of the HTML file in multiple tabs to simulate multiple ## Running the Application via Docker +Before running the following commands, ensure that the URL for the Redis server in `.env` file has been changed to `redis-container:6379` + To run the application via Docker, run the following command: ```bash -docker build -t matching-service . +docker build -f Dockerfile -t match-go-app . +``` + +```bash +docker network create redis-go-network +``` + +```bash +docker run -d --name redis-container --network redis-go-network redis ``` ```bash -docker run -d -p 8081:8081 -p 6379:6379 --name matching-service-container matching-service +docker run -d -p 8081:8081 --name go-app-container --network redis-go-network match-go-app ```