Skip to content

Commit 78af80f

Browse files
committed
Handles Ctrl+C in docker run, but exit in docker-compose is still slow
1 parent 3a74e9d commit 78af80f

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.git
2+
node_modules

Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
FROM yolean/node@sha256:230b269710a1d09b9ebbdeeea0fc4e69ac1388ab71b0178452e817065f69c700
2-
COPY . /app
2+
33
WORKDIR /app
4-
RUN npm install
54

6-
ENTRYPOINT ["node"]
7-
CMD ["./index.js"]
5+
COPY package.json package-lock.json ./
6+
RUN npm install --production
7+
8+
COPY . .
9+
10+
ENTRYPOINT ["node", "./index.js"]
11+
CMD []

index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ app.all('*', (req, res) => {
3737
}
3838
}
3939
res.json(echo);
40-
})
40+
});
4141

42-
app.listen(process.env.PORT || 80)
42+
const server = app.listen(process.env.PORT || 80);
43+
let calledClose = false;
44+
45+
process.on('exit', function () {
46+
if (calledClose) return;
47+
console.log('Got exit event. Trying to stop Express server.');
48+
server.close(function() {
49+
console.log("Express server closed");
50+
});
51+
});
52+
53+
process.on('SIGINT', function() {
54+
console.log('Got SIGINT. Trying to exit gracefully.');
55+
calledClose = true;
56+
server.close(function() {
57+
console.log("Exoress server closed. Asking process to exit.");
58+
process.exit()
59+
});
60+
});

0 commit comments

Comments
 (0)