///
Search

12.Test(Nginx, Node.js)

Nginx를 이용한 정적 페이지 서버 만들기

index.html 코드
<html> <head> <title>영광스러운 도커 이미지 예제</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1>Nginx 서버를 도커 이미지로 만들어서 공개합니다.</h1> </body> </html>
HTML
복사
Dockerfile 코드
FROM nginx COPY index.html /usr/share/nginx/html/index.html
Docker
복사
컴파일 방법
$ docker build -t lab02/exam1 . $ docker run -d --rm \ -p 50001:80 \ lab02/exam1
Docker
복사
실행화면 (50000포트 사용중이라서 50001로 변경함)
(base) glory@Gloryui-MacBookPro ~ % cd Desktop (base) glory@Gloryui-MacBookPro Desktop % cd nginx (base) glory@Gloryui-MacBookPro nginx % docker build -t lab02/exam1 . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM nginx ---> 298ec0e28760 Step 2/2 : COPY index.html /usr/share/nginx/html/index.html ---> Using cache ---> bbb2e07507fb Successfully built bbb2e07507fb Successfully tagged lab02/exam1:latest (base) glory@Gloryui-MacBookPro nginx % docker run -d --rm \ -p 50000:80 \ lab02/exam1 c6e8e42d59efb70c6fd1bef9fd99 docker: Error response from daemon: driver failed programming external connectivity on endpoint nifty_shockley (65c8cc331cd6ddaf4de4b642b0c3af248ac4de86e9fd5bc4db52c4c443764231): Bind for 0.0.0.0:50000 failed: port is already allocated. (base) glory@Gloryui-MacBookPro nginx % docker run -d --rm \ -p 50001:80 \ lab02/exam1 05f5e7d86fa63221b0d9 (base) glory@Gloryui-MacBookPro nginx %
Docker
복사
참고링크
실습정보
이미지: nginx:latest 포트: 80 HTML 경로: /usr/share/nginx/html

Node.js 기반의 웹서비스를 빌드하기

서버를 담당하는 server.js 파일을 만든다.
server.js 소스코드
const http = require('http'); const os = require('os'); const port = process.env.PORT || 8080; process.on('SIGINT', function() { console.log('shutting down...'); process.exit(1); }); var handleRequest = function(request, response) { console.log(`Received request for URL: ${request.url}`); response.writeHead(200); response.end(`Hello, World!\nHostname: ${os.hostname()}\n`); }; var www = http.createServer(handleRequest); www.listen(port, () => { console.log(`server listening on port ${port}`); });
JavaScript
복사
그리고 도커파일에 내용을 집어 넣는다.
도커파일 소스코드
FROM node:12-alpine COPY server.js /app/ EXPOSE 8080 CMD ["node", "/app/server.js"]
Docker
복사
다음 코드를 cmd에 입력한다.
$ docker build -t hellonode . $ docker run --rm -d \ -p 60000:8080 \ hellonode
Docker
복사
실행화면
(base) glory@Gloryui-MacBookPro Desktop % cd server (base) glory@Gloryui-MacBookPro server % docker build -t hellonode . Sending build context to Docker daemon 3.584kB Step 1/4 : FROM node:12-alpine 12-alpine: Pulling from library/node 0a6724ff3fcd: Pull complete b68849637432: Pull complete 7dda491622fb: Pull complete ce97b0f80161: Pull complete Digest: sha256:77fe10dbc042121e94db7f43b4b912a52514759b847978588eec4d5d0eb5282c Status: Downloaded newer image for node:12-alpine ---> e136be20a0bd Step 2/4 : COPY server.js /app/ ---> d6f6c68b684a Step 3/4 : EXPOSE 8080 ---> Running in 38051ae04f19 Removing intermediate container 38051ae04f19 ---> e2ea0ff245f7 Step 4/4 : CMD ["node", "/app/server.js"] ---> Running in ce7a269a8b3d Removing intermediate container ce7a269a8b3d ---> 7861f2e71592 Successfully built 7861f2e71592 Successfully tagged hellonode:latest (base) glory@Gloryui-MacBookPro server % docker run --rm -d \ -p 60000:8080 \ hellonode 624f40fd60a1cd9e7da4458b06417c3040e6a9f14a36869b0acfbd842c5ee9f5 (base) glory@Gloryui-MacBookPro server %
Docker
복사
localhost:60000으로 실행한다. 그러면 hostname을 확인할 수 있다.