Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Created April 8, 2020 22:28
Show Gist options
  • Save PetarKirov/500394eaa11479a681eff4b16fcf96a1 to your computer and use it in GitHub Desktop.
Save PetarKirov/500394eaa11479a681eff4b16fcf96a1 to your computer and use it in GitHub Desktop.
Test Node.js Docker container
ARG BASE_IMAGE=node:lts-alpine
FROM $BASE_IMAGE
WORKDIR /src
ENV HOST=0.0.0.0 PORT=8080
RUN echo $'\
"use strict";\n\
const http = require("http");\n\
const server = http.createServer((request, response) => {\n\ response.writeHead(200, {"Content-Type": "text/plain"});\n\
response.end("Hello, World!");\n\
});\n\
server.listen({\n\
host: process.env.HOST,\n\
port: process.env.PORT,\n\
});\n\
server.on("listening", _ => {\n\
const { address, port } = server.address();\n\
console.log(`Running on http://${address}:${port}`);\n\
});\n\
process.on("SIGINT", () => { // Ctrl+C \n\
console.log(" <Caught Ctrl+C; exiting...>");\n\
process.exit();\n\
});\n\
process.on("SIGTERM", () => { // docker stop\n\
console.log(" <Caught kill; exiting...>");\n\
process.exit();\n\
});\
' | sed "s/\"/'/g" > ./node_server.js
RUN cat ./node_server.js
CMD [ "node", "/src/node_server.js" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment