ð¯ ç®ç
ãã®ã¬ã€ãã¯ãDockerãšNext.jsã䜿çšããéã«çºçãããnode_modulesããã©ã«ãã®æ¶å€±åé¡ã«ã€ããŠèª¬æãããã®è§£æ±ºæ¹æ³ã詳现ãªè§£èª¬ä»ãã§æäŸããŸãã
ð åºæ¬æŠå¿µ
𧱠Docker
ä»®æ³ã®ã³ã³ãã¥ãŒã¿ïŒã³ã³ããïŒãäœããã¢ããªã±ãŒã·ã§ã³ãåããããã®ããŒã«ã§ãã
âïž Next.js
ãŠã§ããµã€ããã¢ããªã±ãŒã·ã§ã³ãäœãããã®äººæ°ã®ããJavaScriptãã¬ãŒã ã¯ãŒã¯ã§ãã
ð node_modules
ã¢ããªã±ãŒã·ã§ã³ãæ£ããåäœããããã«å¿ èŠãªéšåïŒã¢ãžã¥ãŒã«ïŒãä¿åãããŠãããã©ã«ãã§ãã
ðš åé¡ïŒnode_modulesãæ¶ããŠããŸã
çç¶
- Dockerã䜿ã£ãŠNext.jsã®ã¢ããªãèµ·åãããšã
node_modules
ãã©ã«ããæ¶å€±ããŸãã - ã¢ããªã±ãŒã·ã§ã³ãæ£åžžã«åäœããªããªããŸãã
éèŠæ§
node_modules
ãã©ã«ãã¯ã¢ããªã±ãŒã·ã§ã³ã®åäœã«äžå¯æ¬ ã§ãããã®ãã©ã«ããæ¶ãããšãã¢ããªã¯å¿
èŠãªéšåãèŠã€ããããªããªããŸãã
ðµïž åå ïŒãã€ã³ãããŠã³ãã«ããäžæžã
ãã€ã³ãããŠã³ããšã¯ïŒ
- ãã¹ãã³ã³ãã¥ãŒã¿ïŒããªãã®PCïŒã®ãã©ã«ããDockerã³ã³ããå ãšå ±æããä»çµã¿ã§ãã
- ç®çã¯ããã¹ãåŽã§ã®å€æŽãã³ã³ããã«ãªã¢ã«ã¿ã€ã ã§åæ ãããããšã§ãã
åé¡ã®è©³çŽ°
- ãã¹ãåŽã®
node_modules
ãã©ã«ãã空ã®å Žåããã®ç¶æ ãã³ã³ããå ã«åæ ãããŸãã - çµæãšããŠãã³ã³ããå
ã®
node_modules
ãã©ã«ãã空ã«ãªã£ãŠããŸããŸãã
æè¡çèæ¯
Dockerã®ä»æ§äžããã€ã³ãããŠã³ãã§ã¯ãã¹ãåŽã®ç¶æ ãåªå ããããããã³ã³ããå ã®ããŒã¿ãäžæžããããŠããŸããŸãã
ð¡ 解決æ¹æ³ïŒè©³çŽ°è§£èª¬ä»ãïŒ
1ïžâ£ ç°å¢å€æ°ãèšå®ããŠnpmã®ãã¹ãéã
èšå®æé
-
Dockerfile
ã«ä»¥äžã®è¡ãè¿œå ããŸãïŒENV PATH /app/node_modules/.bin:$PATH
-
Dockerfile
ã®å®å šãªäŸïŒFROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . ENV PATH /app/node_modules/.bin:$PATH CMD ["npm", "run", "dev"]
ãªããã®æ¹æ³ã§è§£æ±ºã§ããã®ã
ãã®æ¹æ³ã¯ãã³ã³ããå
ã®ç°å¢å€æ°PATH
ã«/app/node_modules/.bin
ãè¿œå ããããšã§åé¡ã解決ããŸãã
-
ãã¹ã®åªå
é äœ: ãã®èšå®ã«ãããã³ã³ããã¯
node_modules
å ã®å®è¡å¯èœãã¡ã€ã«ãæåã«æ¢ãããã«ãªããŸãã - ããŒã«ã«ã€ã³ã¹ããŒã«ã®åªå : ãããžã§ã¯ãåºæã®äŸåé¢ä¿ãã°ããŒãã«ã€ã³ã¹ããŒã«ãããåªå ãããŸãã
- ã³ã³ããå ã§ã®äžè²«æ§: ãã¹ããã·ã³ã®ç¶æ ã«é¢ããããã³ã³ããå ã§äžè²«ãããã¹èšå®ãä¿èšŒãããŸãã
ãã®æ¹æ³ã¯node_modules
ã®æ¶å€±èªäœãé²ãããã§ã¯ãããŸããããã³ã³ããããããžã§ã¯ãåºæã®äŸåé¢ä¿ãæ£ããèŠã€ããããããã«ããããšã§ãåé¡ã®åœ±é¿ã軜æžããŸãã
ã¡ãªãã
- èšå®ãç°¡åã§ãè¿œå ã®ãã¡ã€ã«ãè€éãªæ§æãäžèŠã§ãã
-
node_modules
ã®å Žæãæ瀺çã«æå®ããããããã¹ã®åé¡ã解決ã§ããŸãã - ã³ã³ããå ã§ã®ã¿æå¹ãªèšå®ãªã®ã§ããã¹ããã·ã³ã«åœ±é¿ãäžããŸããã
ãã¡ãªãã
- ã³ã³ããå
ã®ç°å¢å€æ°ã®ã¿ãå€æŽãããããæ ¹æ¬çãª
node_modules
ã®æ¶å€±åé¡ãå®å šã«ã¯è§£æ±ºããªãå¯èœæ§ããããŸãã
2ïžâ£ .dockerignoreãã¡ã€ã«ã®äœ¿çš
èšå®æé
-
ãããžã§ã¯ãã®ã«ãŒããã£ã¬ã¯ããªã«
.dockerignore
ãã¡ã€ã«ãäœæããŸãã -
以äžã®å 容ã
.dockerignore
ãã¡ã€ã«ã«è¿œå ããŸãïŒnode_modules npm-debug.log
-
éåžžã®
Dockerfile
ã䜿çšããŸãïŒFROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD ["npm", "run", "dev"]
ãªããã®æ¹æ³ã§è§£æ±ºã§ããã®ã
.dockerignoreãã¡ã€ã«
ã¯ãDockerã€ã¡ãŒãžã®ãã«ãæã«ç¹å®ã®ãã¡ã€ã«ããã£ã¬ã¯ããªãç¡èŠããããæ瀺ããŸãããã®æ¹æ³ã§åé¡ã解決ã§ããçç±ã¯ä»¥äžã®éãã§ãïŒ
-
ãã¹ãã®åœ±é¿æé€: ãã¹ãåŽã®
node_modules
ããã«ãã³ã³ããã¹ãããé€å€ããããšã§ããã¹ãã®ç¶æ ãã³ã³ããã«åœ±é¿ãäžããªããªããŸãã -
ã¯ãªãŒã³ã€ã³ã¹ããŒã«: ã³ã³ããå
ã§
npm install
ãå®è¡ããéãå®å šã«æ°ããnode_modules
ãäœæãããŸããããã«ãããäžè²«æ§ã®ããäŸåé¢ä¿ãä¿èšŒãããŸãã - ã³ã³ããªã¯ãåé¿: ãã¹ããšã³ã³ããã§ç°ãªãããŒãžã§ã³ã®äŸåé¢ä¿ãååšããå¯èœæ§ãæé€ããŸãã
ãã®æ¹æ³ã¯ããã¹ããšã³ã³ããã®node_modules
ãå®å
šã«åé¢ããããšã§åé¡ã解決ããŸããã³ã³ããã¯åžžã«èªèº«ã®æ°ããnode_modules
ã䜿çšããããããã¹ãåŽã®ç¶æ
ã«å·Šå³ãããŸããã
ã¡ãªãã
- ãã¹ãåŽã®
node_modules
ãDockerãã«ãããã»ã¹ã«åœ±é¿ãäžããŸããã - ã³ã³ããå
ã§æ°ãã«
npm install
ãå®è¡ããããããåžžã«æ£ããnode_modules
ã䜿çšãããŸãã - ãã«ãæéãççž®ãããã€ã¡ãŒãžãµã€ãºãæé©åãããŸãã
ãã¡ãªãã
- å¿ èŠãªãã¡ã€ã«ã誀ã£ãŠé€å€ããªãããã«æ³šæãå¿ èŠã§ãã
- ãã«ãæéãè¥å¹²é·ããªãå¯èœæ§ããããŸãïŒæ¯å
npm install
ãå®è¡ããããïŒã
3ïžâ£ ããªã¥ãŒã ã®èšå®
èšå®æé
-
docker-compose.yml
ãã¡ã€ã«ãéããŸãã -
volumes
ã»ã¯ã·ã§ã³ã«ä»¥äžã®èšå®ãè¿œå ããŸãïŒvolumes: - ./src:/app/src - node_modules:/app/node_modules
ãªããã®æ¹æ³ã§è§£æ±ºã§ããã®ã
Dockerããªã¥ãŒã ã䜿çšããæ¹æ³ã¯ãnode_modules
ãã©ã«ããç¬ç«ããæ°žç¶çãªã¹ãã¬ãŒãžãšããŠæ±ãããšã§åé¡ã解決ããŸãïŒ
-
åé¢ãããã¹ãã¬ãŒãž:
node_modules
ãDockerããªã¥ãŒã ãšããŠç®¡çãããããããã¹ãã®ãã¡ã€ã«ã·ã¹ãã ãšå®å šã«åé¢ãããŸãã -
æ°žç¶æ§: ããªã¥ãŒã ã¯ã³ã³ããã®ã©ã€ããµã€ã¯ã«ãšã¯ç¬ç«ããŠååšãããããã³ã³ãããåèµ·åããŠã
node_modules
ã®å 容ãä¿æãããŸãã -
äžè²«æ§: äžåºŠ
npm install
ãå®è¡ãããšããã®çµæãããªã¥ãŒã ã«ä¿åããã以éã®ã³ã³ããèµ·åæã«åå©çšãããŸãã
ãã®æ¹æ³ã¯ãnode_modules
ããã¹ããšã³ã³ããã®äž¡æ¹ããç¬ç«ãããããšã§ããã€ã³ãããŠã³ãã«ããäžæžãåé¡ãæ ¹æ¬çã«è§£æ±ºããŸãã
ã¡ãªãã
-
node_modules
ãã©ã«ããDockerããªã¥ãŒã ãšããŠç®¡çããããã¹ãåŽã®åœ±é¿ãåããªããªããŸãã - ã³ã³ããåèµ·ååŸã
node_modules
ã®å 容ãä¿æãããŸãã - ãã«ãæéãççž®ã§ããŸãïŒ2åç®ä»¥éã®ãã«ãæïŒã
ãã¡ãªãã
- ãã¹ãåŽã«ç©ºã®
node_modules
ãã©ã«ããäºåã«äœæããŠããå¿ èŠããããŸãã - ããªã¥ãŒã ã®ç®¡çãå¿ èŠã«ãªããèšå®ãå°ãè€éã«ãªããŸãã
ð ãŸãšã
-
åé¡: Dockerã§Next.jsã®ã³ã³ãããäœæãããšã
node_modules
ãã©ã«ããæ¶å€±ããã -
åå : ãã€ã³ãããŠã³ãã«ããããã¹ãåŽã®ç©ºã®
node_modules
ãã©ã«ããã³ã³ããå ãäžæžãããŠããŸãã -
解決ç:
- ç°å¢å€æ°ãèšå®ããŠnpmã®ãã¹ãéã
- .dockerignoreãã¡ã€ã«ã®äœ¿çš
- ããªã¥ãŒã ã®èšå®
ãããã®æ¹æ³ã䜿ãã°ãDockerãšNext.jsãåé¡ãªã䜿çšã§ããã¢ããªã±ãŒã·ã§ã³ãæ£åžžã«åäœãããããšãã§ããŸãããããžã§ã¯ãã®èŠä»¶ãéçºç°å¢ã«å¿ããŠãæé©ãªè§£æ±ºçãéžæããŠãã ããã
ð ãããã質åïŒFAQïŒ
-
Q: ã©ã®è§£æ±ºçãæãç°¡åã§ããïŒ
A: ç°å¢å€æ°ãèšå®ããŠnpmã®ãã¹ãéãæ¹æ³ãæãç°¡åã§ããDockerfileã«1è¡è¿œå ããã ãã§æžã¿ãŸãã -
Q: ããã©ãŒãã³ã¹ãæãéèŠããå Žåãã©ã®æ¹æ³ãããããã§ããïŒ
A: ããªã¥ãŒã ã®èšå®ãæãããã©ãŒãã³ã¹ã«åªããŠããŸããååã®ãã«ãåŸã¯node_modules
ãä¿æããããããsubsequent builds are faster. -
Q: éçºç°å¢ãšæ¬çªç°å¢ã§ç°ãªã解決çã䜿çšããŠãåé¡ãããŸãããïŒ
A: ã¯ããåé¡ãããŸãããéçºç°å¢ã§ã¯ç°¡åãªæ¹æ³ïŒç°å¢å€æ°ã®èšå®ãªã©ïŒããæ¬çªç°å¢ã§ã¯ããæé©åãããæ¹æ³ïŒããªã¥ãŒã ã®èšå®ãªã©ïŒã䜿çšããã®ãäžè¬çã§ãã -
Q: ãããã®è§£æ±ºçã¯ä»ã®Node.jsãã¬ãŒã ã¯ãŒã¯ã§ã䜿ããŸããïŒ
A: ã¯ãããããã®è§£æ±ºçã¯Next.js以å€ã®Node.jsããŒã¹ã®ãã¬ãŒã ã¯ãŒã¯ïŒReactãexpressãªã©ïŒã§ãåæ§ã«é©çšã§ããŸãã -
Q: 解決çãçµã¿åãããããšã¯ã§ããŸããïŒ
A: ã¯ããå¯èœã§ããäŸãã°ã.dockerignoreãã¡ã€ã«ã®äœ¿çšãšç°å¢å€æ°ã®èšå®ãçµã¿åãããããšã§ãããå ç¢ãªè§£æ±ºçãäœãããšãã§ããŸãã -
Q: ãããã®è§£æ±ºçã䜿çšããéã®æ³šæç¹ã¯äœã§ããïŒ
A: äž»ãªæ³šæç¹ã¯ä»¥äžã®éãã§ãïŒ- ç°å¢å€æ°ã®èšå®: ã»ãã¥ãªãã£ã«æ³šæããæ©å¯æ å ±ãç°å¢å€æ°ã«å«ããªãããšã
- .dockerignore: å¿ èŠãªãã¡ã€ã«ã誀ã£ãŠé€å€ããªãããšã
- ããªã¥ãŒã : ããŒã¿ã®æ°žç¶æ§ãšã³ã³ããã®ã©ã€ããµã€ã¯ã«ãç解ããããšã