Node.js 웹서버를 도커 이미지로 만들어서 실행하기

ASHAPPYASIKNOW·2022년 7월 29일
0
post-thumbnail

Node.js 웹서버 만들기

app.js 파일 만들기

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/

실행결과


도커 이미지 만들기

Dockerfile 만들기

Dockerfile

FROM node:16
COPY /app.js /app.js
ENTRYPOINT ["node", "/app.js"]

이미지 만들기

> docker build -t my-hello-world .
  • --tag , -t: Name and optionally a tag in the 'name:tag' format (도커이미지에 tag를 달아 준다.)
  • .: Dockerfile의 위치 (.: 현재 폴더)

> docker images
REPOSITORY                        TAG       IMAGE ID       CREATED          SIZE
my-hello-world                    latest    7d1c555e62ab   23 seconds ago   907MB

만들어진 my-hello-world 이미지를 확인할 수 있다.

이미지 실행

참고: [Windows10][WSL2] 도커(Docker)로 MySQL 띄우기

> 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가 생성된 것을 확인 할 수 있음


REFERENCES

docker build

profile
36.9 It's good time to start something new

0개의 댓글