Node.js "Hello, World!" with Docker

Create a project directory.

mkdir nodejs
cd nodejs

Make Dockerfile.

FROM node:alpine

WORKDIR /home/app
USER node

EXPOSE 8080

ENTRYPOINT /bin/ash

Builds Docker images from a Dockerfile.

docker build -t node-alpine .

Creates and starts a container.

docker run --rm -it --name hello-world \
-v $PWD:/home/app -p 8080:8080 node-alpine

Make helloworld.js

const http = require('http');

const server = http.createServer();

server.on('request',function(req,res) {
    res.writeHead(200,{'Content-Type': 'text/plain'});
    res.write('Hello, world!');
    res.end();
})

server.listen(8080);

Initiate helloworld.js.

node helloworld.js

Access localhost:8080.

Screen Shot 2021-01-01 at 22.59.01.png

No Comments Yet