I am trying to set up a generic TCP socket using node.js and host the container using Google Compute Engine. My problem is that I am unable to connect to the socket after deployment. The connection just times out.
I built a simple server in ‘index.mjs’
import { createServer } from 'node:http';
const server = createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World!n');
});
server.on('connection', socket => {
console.log('Server connected...');
socket.on('error', err => log.error(err.message));
socket.on('data', async data => {
const received = data.toString().trim();
console.log(`Data received from client ${socket.remoteAddress}:${socket.remotePort}:`, received);
});
});
server.listen(3000, () => {
console.log('Listening on 3000');
});
and used this Dockerfile:
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install
EXPOSE 3000
CMD ["node", "index.mjs"]
Thereafter I built the container using Docker, pushed it to the Artifact Registry and created a VM instance using Compute Engine, ensuring that the firewall allowed HTTP and HTTPS traffic. I found the external IP listed in the GCE instance and tried to connect to that using the SocketTest tool and the 3000 port and the connection just times out.
I have run the server locally and I can confirm it runs, printing out “Listening on 3000” to console but was unsure the correct IP address to use to test connection locally, none seemed to work.
Eventually I’ll save data from the port to Firestore but just trying to get basic functionality for now. Thanks in advance!
Alan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.