如何在Docker容器中连接套接字与Socket.io?

前端之家收集整理的这篇文章主要介绍了如何在Docker容器中连接套接字与Socket.io?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在服务器外观的docker容器中有一个小型套接字服务器

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server,{origins: 'localhost:*'});
io.on('connection',function (socket) {
console.log('Connected');
});
const PORT = 8081;
const HOST = '0.0.0.0';
server.listen(PORT,HOST);

和docker文件

FROM keymetrics/pm2-docker-alpine:latest
WORKDIR /root
RUN apk update && \
   apk upgrade && \
   apk add git
ENV HOME /root
COPY socket.js ./
COPY package.json ./
RUN npm install
COPY pm2.json ./
EXPOSE 8081
CMD [ "pm2-docker","start","pm2.json" ]

pm2.json看起来像

{
"apps": [{
    "name": "socket-server","script": "socket.js","exec_mode" : "cluster","instances"  : 2,"env": {
    "production": true
    }
}]
}

的package.json

{
"name": "socket-server","version": "1.0.0","description": "","main": "socket.js","scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
},"author": "","license": "ISC","dependencies": {
"express": "^4.15.3","socket.io": "^2.0.3"
}
}

一切都运行得很好

docker run -d -p 8081:8081 socket-server

直到我尝试从另一个容器中运行的网站连接到它,网站连接如下…

猜你在找的Docker相关文章