瀏覽代碼

add docker support

gemer 3 年之前
父節點
當前提交
2a43f747df
共有 6 個文件被更改,包括 49 次插入4 次删除
  1. 2 0
      .dockerignore
  2. 3 2
      .env
  3. 11 0
      Dockerfile
  4. 1 1
      demo/index.html
  5. 30 0
      docker-compose.yml
  6. 2 1
      src/connection/redis.js

+ 2 - 0
.dockerignore

@@ -0,0 +1,2 @@
+node_modules
+npm-debug.log

+ 3 - 2
.env

@@ -1,7 +1,8 @@
 PORT=3000
-REDIS_HOST=localhost
+VOLUME_DIR=./data
+REDIS_HOST=redis
 REDIS_PORT=6379
-REDIS_USER=root
+REDIS_USER=redis
 REDIS_PASSWORD=s1mpl3
 WATCH_USER=4dage
 WATCH_PASSWORD=4dage168.

+ 11 - 0
Dockerfile

@@ -0,0 +1,11 @@
+FROM node:gallium-alpine3.15
+
+WORKDIR /usr/src/app
+
+COPY . .
+
+RUN npm install
+
+EXPOSE $PORT
+
+CMD [ "node", "index.js" ]

+ 1 - 1
demo/index.html

@@ -25,7 +25,7 @@
       // 'http://47.115.43.159:3000'
       // var socket = io("http://0.0.0.0:3000");
       // const { roomId, userId, sceneNum, isClient, role, userLimitNum, roomType } = user
-      var socket = io("http://0.0.0.0:3000", {
+      var socket = io("http://0.0.0.0:8060", {
         path: "/test",
         query: {
           userId: "user_001",

+ 30 - 0
docker-compose.yml

@@ -0,0 +1,30 @@
+version: "3.4"
+
+services:
+  chat-socket:
+    restart: always
+    build:
+      context: .
+    ports:
+      - 8060:$PORT
+    environment:
+      REDIS_HOST: $REDIS_HOST
+      REDIS_PORT: $REDIS_PORT
+      REDIS_PASSWORD: $REDIS_PASSWORD
+    # volumes:
+    depends_on:
+      - redis
+    networks:
+      - chat-socket
+
+  redis:
+    image: redis:5-alpine
+    command: redis-server --requirepass ${REDIS_PASSWORD}
+    ports:
+      - 6666:$REDIS_PORT
+    volumes:
+      - ${VOLUME_DIR}/redis:/data
+    networks:
+      - chat-socket
+networks:
+  chat-socket:

+ 2 - 1
src/connection/redis.js

@@ -1,6 +1,7 @@
 import { createClient } from "redis";
 
-const pubClient = createClient({ url: "redis://localhost:6379" });
+const url = `redis://:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`;
+const pubClient = createClient({ url: url });
 const subClient = pubClient.duplicate();
 
 export { pubClient, subClient };