Reputation: 2619
I have node running from a docker container and I tried to write some websocket example. But I an getting the next problem:
root@7cd5a41c8eea:/application/webSocketEx# node
> var connection = new WebSocket('ws://html5rocks.websocket.org/echo', ['soap', 'xmpp']);
ReferenceError: WebSocket is not defined
> const WebSocket = require('ws');
undefined
> var ws = new WebSocket("ws://www.websocket.org");
undefined
> { Error: Unexpected server response: 404
at ClientRequest.req.on (/application/webSocketEx/node_modules/ws/lib/websocket.js:579:7)
at ClientRequest.emit (events.js:182:13)
at ClientRequest.EventEmitter.emit (domain.js:460:23)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:555:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
at Socket.socketOnData (_http_client.js:441:20)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:460:23)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
domainEmitter:
WebSocket {
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
members: [] },
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
readyState: 2,
protocol: '',
_binaryType: 'nodebuffer',
_closeFrameReceived: false,
_closeFrameSent: false,
_closeMessage: '',
_closeTimer: null,
_closeCode: 1006,
_extensions: {},
_receiver: null,
_sender: null,
_socket: null,
_bufferedAmount: 0,
_isServer: false,
_redirects: 0,
url: 'ws://www.websocket.org',
_req:
ClientRequest {
domain: [Domain],
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
connection: [Socket],
_header:
'GET / HTTP/1.1\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: 2c4HivAKv2OJ6rO7qYyaNA==\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\nHost: www.websocket.org\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: undefined,
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/',
_ended: false,
res: [IncomingMessage],
aborted: 1563389938612,
timeoutCb: null,
upgradeOrConnect: false,
parser: [HTTPParser],
maxHeadersCount: null,
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object] } },
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] },
domainThrown: false }
>
Is there a configuration parameter I am missing to use websockets from a docker container and node? I installed ws using npm install ws already. I have the feeling I need to open a port for the docker container but I do not understand which one.
Thanks
Upvotes: 1
Views: 4353
Reputation: 78
Docker container runs on their own subnet and therefore they are not allowed to access any other hosts from it. You need to explicitly change your docker subnet to your host subnet (bridging network) and once your docker container is on the same network as host your container will be able to access internet.
You need to implement something like this.
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
version: '2'
services:
web:
image: some/image
networks:
- dockernet
networks:
dockernet:
external: true
For more information you can read the document here for your specific use case:
https://docs.docker.com/network/network-tutorial-standalone/
Upvotes: 3