Docker Community Forums

Share and learn in the Docker community.

Hapijs application not responses correctly. HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers

docker

(Lucas Simon Rodrigues Magalhaes) #1

My server

import Hapi from 'hapi';
import { load } from 'dotenv';
import mongoose from 'mongoose';
import plugins from './plugins';
import urls from './urls';

load();

console.log(
  process.env.MONGO_URL,
  process.env.PORT
);
// Create a server with a host and port
const server = Hapi.server({
  host: '127.0.0.1',
  port: process.env.PORT,
  routes: { cors: { origin: ['*'] } },
});

mongoose.connect(
  process.env.MONGO_URL,
  { useNewUrlParser: true }
);

mongoose.connection.once('open', () => {
  console.log('connected to database');
});

// Start the server
async function start() {
  try {
    server.route(urls);
    await server.register(plugins);

    await server.start();
  } catch (err) {
    process.exit(1);
  }

  console.log('Server running at:', server.info.uri);
}

start();

My package.json

{
  "name": "civilizations",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "build": "babel src --out-dir dist --ignore spec.js",
    "dev": "NODE_ENV=dev nodemon ./src/server.js --exec babel-node",
    "debug": "NODE_ENV=test ndb ./src/server.js --exec babel-node",
    "flow": "flow",
    "lint": "eslint src/**",
    "pretty": "prettier --single-quote --trailing-comma es5 --write \"./src/**/*.js\"",
    "start": "node ./dist/server.js",
    "test": "jest --coverage",
    "test:watch": "jest --watch --coverage"
  },
  ....
}

My Dockerfile

FROM node:10-slim
ENV HOME=/home/app
COPY package.json package-lock.json $HOME/lib/
WORKDIR $HOME/lib
COPY . $HOME/lib
RUN npm install --silent --progress=false && npm run build && npm cache clean --force
EXPOSE 5000
CMD ["npm", "start"]

Docker build.

$ docker build -t civilizations .
Sending build context to Docker daemon  1.287MB
Step 1/8 : FROM node:10-slim
 ---> 441869a4d9e2
Step 2/8 : ENV HOME=/home/app
 ---> Running in 521fee7ec29d
Removing intermediate container 521fee7ec29d
 ---> 6323749477fc
Step 3/8 : COPY package.json package-lock.json $HOME/lib/
 ---> 099554094c78
Step 4/8 : WORKDIR $HOME/lib
 ---> Running in e746f8839a75
Removing intermediate container e746f8839a75
 ---> 821ae99dcf16
Step 5/8 : COPY . $HOME/lib
 ---> 01013dc516fd
Step 6/8 : RUN npm install --silent --progress=false && npm run build && npm cache clean --force
 ---> Running in 7073aaf05d74
husky > setting up git hooks
husky > done
Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

added 1072 packages from 466 contributors and audited 52088 packages in 23.969s
found 0 vulnerabilities


> civilizations@1.0.0 build /home/app/lib
> babel src --out-dir dist --ignore spec.js

src/cities/models.js -> dist/cities/models.js
src/cities/schema.js -> dist/cities/schema.js
src/cities/urls.js -> dist/cities/urls.js
src/cities/views.js -> dist/cities/views.js
src/core/schemaError.js -> dist/core/schemaError.js
src/plugins.js -> dist/plugins.js
src/professions/models.js -> dist/professions/models.js
src/professions/schema.js -> dist/professions/schema.js
src/professions/urls.js -> dist/professions/urls.js
src/professions/views.js -> dist/professions/views.js
src/server.js -> dist/server.js
src/server.test.js -> dist/server.test.js
src/spots/models.js -> dist/spots/models.js
src/spots/schema.js -> dist/spots/schema.js
src/spots/urls.js -> dist/spots/urls.js
src/spots/views.js -> dist/spots/views.js
src/urls.js -> dist/urls.js
npm WARN using --force I sure hope you know what you are doing.
Removing intermediate container 7073aaf05d74
 ---> c4c8b9e185ee
Step 7/8 : EXPOSE 5000
 ---> Running in 0b58e382a5bc
Removing intermediate container 0b58e382a5bc
 ---> bc4a4de7a0d1
Step 8/8 : CMD ["npm", "start"]
 ---> Running in 5462a444e235
Removing intermediate container 5462a444e235
 ---> ad039184f02e
Successfully built ad039184f02e
Successfully tagged civilizations:latest

Docker run:

$ docker run -itd -e MONGO_URL='mongodb://zzz:zzzz@zzz.zzz.zzz:zzz/zzzz' -e PORT=5000 --name civilizations -p 5000:5000  civilizations
0e5438489683c437534198d90ce378971b639e3d06b851d8648743dcd0122fe7

Docker logs

$ docker logs -f 0e5438489683c

> civilizations@1.0.0 start /home/app/lib
> node ./dist/server.js

mongodb://zzz:zzzz@zzz.zzz.zzz:zzz/zzzz 5000
Server running at: http://127.0.0.1:5000
connected to database

Tests inside container /bin/bash

$ docker exec -i -t 0e5438489683c /bin/bash
root@0e5438489683:~/lib# wget 127.0.0.1:5000/api/professions/
converted 'http://127.0.0.1:5000/api/professions/' (ANSI_X3.4-1968) -> 'http://127.0.0.1:5000/api/professions/' (UTF-8)
--2018-09-09 23:52:54--  http://127.0.0.1:5000/api/professions/
Connecting to 127.0.0.1:5000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 60 [application/json]
Saving to: 'index.html'

index.html                                 100%[=======================================================================================>]      60  --.-KB/s   in 0s     

2018-09-09 23:52:54 (8.20 MB/s) - 'index.html' saved [60/60]

And my logs:

Server running at: http://127.0.0.1:5000
connected to database


180909/235254.013, (1536537174013:0e5438489683:20:jlvigq8v:10000) [response,api,professions,get all professions] http://127.0.0.1:5000: get /api/professions/ {"limit":10,"offset":0} 200 (188ms)

Tests from my shell. External

$ wget 127.0.0.1:5000/api/professions/
--2018-09-09 20:54:15--  http://127.0.0.1:5000/api/professions/
Connecting to 127.0.0.1:5000... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.

--2018-09-09 20:54:16--  (try: 2)  http://127.0.0.1:5000/api/professions/
Connecting to 127.0.0.1:5000... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.