@@ -19,20 +19,22 @@ will run the image as a container.
1919Docker allows you to package an application with all of its dependencies into a
2020standardized unit, called a container, for software development. A container is
2121a stripped-to-basics version of a Linux operating system. An image is software
22+ you load into a container.
2223-->
2324
2425# Node.js 웹 앱의 도커라이징
2526
2627이 예제에서는 Node.js 애플리케이션을 Docker 컨테이너에 넣는 방법을 보여줍니다. 이 가이드는
2728개발 목적이지 프로덕션 배포용이 * 아닙니다* .
2829[ Docker가 설치] ( https://docs.docker.com/engine/installation/ ) 되어 있고
29- Node.js 애플리케이션을 구조화하는 방법에 관해 기본적인 지식이 있어야 합니다.
30+ Node.js 애플리케이션의 구조에 대한 기본적인 지식이 있어야 합니다.
3031
3132먼저 간단한 Node.js 웹 애플리케이션을 만든 후에 이 애플리케이션을 위한 Docker 이미지를
3233만들어서 컨테이너로 실행할 것입니다.
3334
3435Docker를 사용하면 애플리케이션과 모든 의존성을 소프트웨어 개발에서 컨테이너라고 부르는 표준화된
3536단위로 패키징할 수 있습니다. 컨테이너는 리눅스 운영체제의 간단 버전입니다.
37+ 이미지는 컨테이너에 로드하는 소프트웨어를 말합니다.
3638
3739<!--
3840## Create the Node.js app
@@ -79,7 +81,6 @@ create a `package.json` file that describes your app and its dependencies:
7981```
8082
8183<!--
82-
8384With your new `package.json` file, run `npm install`. If you are using `npm`
8485version 5 or later, this will generate a `package-lock.json` file which will be copied
8586to your Docker image.
@@ -111,6 +112,9 @@ container using the official Docker image. First, you'll need to build a Docker
111112image of your app.
112113-->
113114
115+ ` package.json ` 파일을 만든 후, ` npm install ` 을 실행하세요. 버전 5 이상의 ` npm ` 을
116+ 사용한다면, Docker 이미지에 복사할 ` package-lock.json ` 파일을 ` npm ` 에서 생성할 것입니다.
117+
114118이제 [ Express.js] ( https://expressjs.com/ ) 프레임워크로 웹앱을 정의하는 ` server.js ` 를 만들겠습니다.
115119
116120``` javascript
@@ -195,33 +199,40 @@ COPY package*.json ./
195199
196200RUN npm install
197201# If you are building your code for production
198- # RUN npm install --only=production
202+ # RUN npm ci --only=production
199203```
200204-->
201205
202206다음으로 이미지 안에 애플리케이션 코드를 넣기 위해 디렉터리를 생성할 것입니다.
203- 이 디렉터리가 애플리케이션의 워킹 디렉터리가 됩니다.
207+ 이 디렉터리가 애플리케이션의 작업 디렉터리가 됩니다.
204208
205209``` docker
206210# 앱 디렉터리 생성
207- RUN mkdir -p /usr/src/app
208211WORKDIR /usr/src/app
209212```
210213
211214이 이미지에는 이미 Node.js와 NPM이 설치되어 있으므로 ` npm ` 바이너리로
212- 앱의 의존성을 설치하기만 하면 됩니다.
215+ 앱의 의존성을 설치하기만 하면 됩니다. 버전 4 이하의 ` npm ` 은 ` package-lock.json `
216+ 파일을 생성하지 * 않을* 것입니다.
213217
214218``` docker
215219# 앱 의존성 설치
220+ # 가능한 경우(npm@5+) package.json과 package-lock.json을 모두 복사하기 위해
221+ # 와일드카드를 사용
216222COPY package*.json ./
223+
217224RUN npm install
225+ # 프로덕션을 위한 코드를 빌드하는 경우
226+ # RUN npm ci --only=production
218227```
219228
220229<!--
221230Note that, rather than copying the entire working directory, we are only copying
222231the `package.json` file. This allows us to take advantage of cached Docker
223232layers. bitJudo has a good explanation of this
224233[here](http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/).
234+ Furthermore, the `npm ci` command, specified in the comments, helps provide faster, reliable, reproducible builds for production environments.
235+ You can read more about this [here](https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable).
225236
226237To bundle your app's source code inside the Docker image, use the `COPY`
227238instruction:
@@ -239,6 +250,14 @@ EXPOSE 8080
239250```
240251-->
241252
253+ 작업 디렉터리 전체가 아닌 ` package.json ` 파일만을 복사하고 있는데, 이는 캐시된
254+ Docker 레이어의 장점을 활용하기 위함입니다. bitJudo가 이에 대해
255+ [ 여기] ( http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/ ) 에
256+ 잘 설명해 두었습니다. 추가로, 주석에 언급된 ` npm ci ` 커맨드는 프로덕션 환경을
257+ 위한 더 빠르고, 신뢰할 수 있고, 재현 가능한 빌드를 제공합니다. 이에 대해
258+ [ 여기] ( https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable ) 에서
259+ 자세히 알아볼 수 있습니다.
260+
242261Docker 이미지 안에 앱의 소스코드를 넣기 위해 ` COPY ` 지시어를 사용합니다.
243262
244263``` docker
@@ -276,7 +295,7 @@ COPY package*.json ./
276295
277296RUN npm install
278297# If you are building your code for production
279- # RUN npm install --only=production
298+ # RUN npm ci --only=production
280299
281300# Bundle app source
282301COPY . .
@@ -302,8 +321,13 @@ FROM node:8
302321WORKDIR /usr/src/app
303322
304323# 앱 의존성 설치
324+ # 가능한 경우(npm@5+) package.json과 package-lock.json을 모두 복사하기 위해
325+ # 와일드카드를 사용
305326COPY package*.json ./
327+
306328RUN npm install
329+ # 프로덕션을 위한 코드를 빌드하는 경우
330+ # RUN npm ci --only=production
307331
308332# 앱 소스 추가
309333COPY . .
@@ -365,7 +389,7 @@ node 8 1934b0b038d1 5 days ago
365389## 이미지 빌드
366390
367391작성한 ` Dockerfile ` 이 있는 디렉터리로 가서 Docker 이미지를 빌드하는 다음 명령어를 실행하세요.
368- ` -t ` 플래그로 이미지에 태그를 추가하기 때문에 나중에 ` docker images ` 명령어로
392+ ` -t ` 플래그로 이미지에 태그를 추가할 수 있어 나중에 ` docker images ` 명령어로
369393쉽게 찾을 수 있습니다.
370394
371395``` bash
@@ -418,7 +442,7 @@ $ docker exec -it <container id> /bin/bash
418442## 이미지 실행
419443
420444` -d ` 로 이미지를 실행하면 분리 모드로 컨테이너를 실행해서 백그라운드에서 컨테이너가 돌아가도록 합니다.
421- ` -p ` 플래그는 공개 포트를 컨테이너 내의 비밀 포트로 리다이렉트합니다. 앞에서 만들 이미지를 실행하세요.
445+ ` -p ` 플래그는 공개 포트를 컨테이너 내의 비공개 포트로 리다이렉트합니다. 앞에서 만든 이미지를 실행하세요.
422446
423447``` bash
424448$ docker run -p 49160:8080 -d < your username> /node-web-app
@@ -504,6 +528,7 @@ Content-Length: 12
504528ETag: W/" c-M6tWOb/Y57lesdjQuHeB1P/qTV0"
505529Date: Mon, 13 Nov 2017 20:53:59 GMT
506530Connection: keep-alive
531+
507532Hello world
508533```
509534
@@ -527,6 +552,6 @@ following places:
527552
528553* [ 공식 Node.js Docker 이미지] ( https://hub.docker.com/_/node/ )
529554* [ Node.js Docker 사용사례 문서] ( https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md )
530- * [ 공시 Docker 문서] ( https://docs.docker.com/ )
555+ * [ 공식 Docker 문서] ( https://docs.docker.com/ )
531556* [ Stack Overflow에 Docker 태그로 올라온 질문] ( https://stackoverflow.com/questions/tagged/docker )
532557* [ Docker 레딧] ( https://reddit.com/r/docker )
0 commit comments