const http = require('http')
const os = require('os')
const hostname = '127.0.0.1'
const port = 8080
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hostname: ' + os.hostname() + '\n')
})
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})
참고: WSL2-Node.js로-웹서버-생성하고-실행하기
> node app.js
Server running at http://127.0.0.1:8080/
FROM node:16
COPY /app.js /app.js
ENTRYPOINT ["node", "/app.js"]
> docker build -t my-hello-world .
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-hello-world latest 7d1c555e62ab 23 seconds ago 907MB
만들어진 my-hello-world 이미지를 확인할 수 있다.
> docker run --name my-hello-world-1 -p 8082:8080 --restart=always -d my-hello-world:latest
5102119363bf2eee28f615ec6caae34c4504c8494d037875a17db409f179d400
> docker run --name my-hello-world-2 -p 8083:8080 --restart=always -d my-hello-world:latest
80ea60825624a8c0af3803ac35408804fbe7343a48900a30d022303543d351a8
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80ea60825624 my-hello-world:latest "node app.js" 43 seconds ago Up 43 seconds 0.0.0.0:8083->8080/tcp my-hello-world-2
5102119363bf my-hello-world:latest "node app.js" 50 seconds ago Up 50 seconds 0.0.0.0:8082->8080/tcp my-hello-world-1
Docker image가 생성된 것을 확인 할 수 있음