0

I tried to containerize a server using Docker for practicing but it doesn't work when it's containerized.

My simple Node.js server looks likes this:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  console.log("data is recieved from the server");
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello world');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

I suspect something might be wrong with my package.json, it looks like this:

{
  "name": "dockerservers",
  "version": "1.0.0",
  "description": "first server for dockering",
  "main": "app.js",
  "dependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start":"node app.js"
  },


  "author": "scully86",
  "license": "MIT"
}

And my Dockerfile looks like this:

# Use the official Node.js 18 image as the base image
FROM node:18
# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy the rest of the application code to the working directory
COPY . /usr/src/app/

# Install dependencies
RUN npm install


# Expose the port on which the server listens
EXPOSE 3000

ENV NAME dockerservers

# Start the server
CMD ["node","app.js"]

I successfully built the image and ran it with this command:

docker run -p 3000:3000 nodeserver

All the things working properly until now.

The problem is, the Node.js server is not sending response to the exposed port and this error is shown:

image of error

Can anyone help? I've been stuck for almost a day now.

  • try changing `hostname` to `0.0.0.0` – Asplund Jul 05 '23 at 13:24
  • I tried this well but the error is shown as ** This site can’t be reachedThe webpage at http://0.0.0.0:3000/ might be temporarily down or it may have moved permanently to a new web address. ** – Madhu mohan Jul 05 '23 at 13:34
  • you would still access the website at `localhost/127.0.0.1`, listening on `0.0.0.0` just means listening on all interfaces – Asplund Jul 05 '23 at 13:47
  • can you send logs from the docker server so that we could understand more what happened? also the output of a command `curl -v http://localhost:3000` would be quite helpful – blek__ Jul 05 '23 at 14:24

1 Answers1

0

I can confirm that changing the 127.0.0.1 to 0.0.0.0 fixes this.

Using original code -

iarc13@13 so1 % curl -v localhost:3000
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Empty reply from server
* Closing connection 0
curl: (52) Empty reply from server

After changing to 0.0.0.0, rebuilding the image and running..

iarc13@13 so1 % curl -v localhost:3000
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Wed, 05 Jul 2023 15:15:35 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Content-Length: 11
< 
* Connection #0 to host localhost left intact
Hello world%
iArc13
  • 162
  • 8