Dockerize CMS 和 NextJS 并处理依赖关系(NextJS 需要运行 CMS 才能构建)。

huangapple go评论97阅读模式
英文:

Dockerize CMS and NextJS and handle depedencies (NextJS needs CMS running to build)

问题

Context

我正在尝试将我的应用程序容器化,该应用程序由一个无界面的 CMS(Strapi)和一个 NextJS 客户端组成。

NextJS 需要我的 CMS 在运行时才能构建(它会在端口 1337 上获取内容)。

Code

我的 docker-compose.yaml

  1. version: "3.8"
  2. services:
  3. db:
  4. image: mariadb:latest
  5. environment:
  6. - MYSQL_ROOT_PASSWORD=root
  7. - MYSQL_USER=root
  8. - MYSQL_PASSWORD=root
  9. - DATABASE_SSL=false
  10. ports:
  11. - "3306:3306"
  12. volumes:
  13. - ./server/strapi_portfolio.sql:/docker-entrypoint-initdb.d/strapi_portfolio.sql
  14. networks:
  15. - portfolio-network
  16. portfolio-strapi:
  17. build:
  18. context: ./server
  19. ports:
  20. - "1337:1337"
  21. volumes:
  22. - ./portfolio-strapi:/usr/src/app
  23. environment:
  24. - NODE_ENV=development
  25. networks:
  26. - portfolio-network
  27. depends_on:
  28. - db
  29. portfolio-nextjs:
  30. build:
  31. context: ./client
  32. ports:
  33. - "3000:3000"
  34. volumes:
  35. - ./portfolio-nextjs:/usr/src/app
  36. environment:
  37. - NODE_ENV=development
  38. networks:
  39. - portfolio-network
  40. depends_on:
  41. - portfolio-strapi
  42. networks:
  43. portfolio-network:
  44. driver: bridge

NextJS Dockerfile

  1. FROM node:18-alpine as builder
  2. WORKDIR "/app"
  3. COPY package.json package-lock.json ./
  4. RUN npm install --frozen-lockfile --production=true
  5. COPY . .
  6. RUN npm run build-prod
  7. EXPOSE 3000
  8. CMD ["npm", "start"]

Strapi(CMS)Dockerfile

  1. FROM node:18-alpine AS builder
  2. WORKDIR "/app"
  3. COPY package.json package-lock.json ./
  4. RUN npm install --frozen-lockfile
  5. COPY . .
  6. RUN npm run build
  7. FROM node:18-alpine AS runner
  8. COPY --from=builder /app/package.json /app/package-lock.json ./
  9. COPY --from=builder /app/dist ./
  10. RUN npm install --frozen-lockfile --omit=dev
  11. EXPOSE 1337
  12. CMD ["npm", "start"]

What I tried

  • 我尝试了 Docker 的 depends_on 功能,但它不起作用,即使使用了健康检查也不行。
    是的,CMS 构建会在之前触发,但我的 NextJS 应用程序在构建之前并不等待它提供服务。
  • 我尝试创建一个 npm 脚本,在运行构建脚本之前等待我的 API 可用:
  1. const MAX_RETRIES = 10;
  2. let retries = 0;
  3. const waitForStrapi = async () => {
  4. while (retries < MAX_RETRIES) {
  5. console.log(`[ATTEMPT ${retries}] Trying to reach Strapi...`);
  6. try {
  7. await fetch("http://localhost:1337");
  8. console.log("Strapi is ready!");
  9. break;
  10. } catch (e) {
  11. console.log("Waiting for Strapi...");
  12. await new Promise((resolve) => setTimeout(resolve, 30000));
  13. }
  14. retries++;
  15. }
  16. };
  17. waitForStrapi();
  18. console.log("[ERROR - Couldn't connect to Strapi]");

我的 CMS 镜像已完全构建,但从未运行过(因此无法解析 http://localhost:1337),我不知道为什么...

我是不是漏掉了什么东西,还是我可能没有正确的方法来实现这一点?感谢您的帮助。

英文:

Context

I am trying to dockerize my app which is made off a headless CMS (Strpi) and a client NextJS.

NextJS needs to have my CMS running in order to build (it's fetching the content there on port 1337)

Code

My docker-compose.yaml

  1. version: &quot;3.8&quot;
  2. services:
  3. db:
  4. image: mariadb:latest
  5. environment:
  6. - MYSQL_ROOT_PASSWORD=root
  7. - MYSQL_USER=root
  8. - MYSQL_PASSWORD=root
  9. - DATABASE_SSL=false
  10. ports:
  11. - &quot;3306:3306&quot;
  12. volumes:
  13. - ./server/strapi_portfolio.sql:/docker-entrypoint-initdb.d/strapi_portfolio.sql
  14. networks:
  15. - portfolio-network
  16. portfolio-strapi:
  17. build:
  18. context : ./server
  19. ports:
  20. - &quot;1337:1337&quot;
  21. volumes:
  22. - ./portfolio-strapi:/usr/src/app
  23. environment:
  24. - NODE_ENV=development
  25. networks:
  26. - portfolio-network
  27. depends_on:
  28. - db
  29. portfolio-nextjs:
  30. build:
  31. context : ./client
  32. ports:
  33. - &quot;3000:3000&quot;
  34. volumes:
  35. - ./portfolio-nextjs:/usr/src/app
  36. environment:
  37. - NODE_ENV=development
  38. networks:
  39. - portfolio-network
  40. depends_on:
  41. - portfolio-strapi
  42. networks:
  43. portfolio-network:
  44. driver: bridge

NextJS Dockerfile

  1. FROM node:18-alpine as builder
  2. WORKDIR &quot;/app&quot;
  3. COPY package.json package-lock.json ./
  4. RUN npm install --frozen-lockfile --production=true
  5. COPY . .
  6. RUN npm run build-prod
  7. EXPOSE 3000
  8. CMD [&quot;npm&quot;, &quot;start&quot;]

Strapi (CMS) Dockerfile

  1. FROM node:18-alpine AS builder
  2. WORKDIR &quot;/app&quot;
  3. COPY package.json package-lock.json ./
  4. RUN npm install --frozen-lockfile
  5. COPY . .
  6. RUN npm run build
  7. FROM node:18-alpine AS runner
  8. COPY --from=builder /app/package.json /app/package-lock.json ./
  9. COPY --from=builder /app/dist ./
  10. RUN npm install --frozen-lockfile --omit=dev
  11. EXPOSE 1337
  12. CMD [&quot;npm&quot;, &quot;start&quot;]

What I tried

  • I tried the depends_on functionnality of docker but it's not working, even with healthchecks.
    Yes the CMS build is triggered before but my NextJS app is not waiting for it to be served before building
  • I tried to create a npm script that waits for my api to be available before running the build script :
  1. const MAX_RETRIES = 10;
  2. let retries = 0;
  3. const waitForStrapi = async () =&gt; {
  4. while (retries &lt; MAX_RETRIES) {
  5. console.log(`[ATTEMPT ${retries}] Trying to reach Strapi...`);
  6. try {
  7. await fetch(&quot;http://localhost:1337&quot;);
  8. console.log(&quot;Strapi is ready!&quot;);
  9. break;
  10. } catch (e) {
  11. console.log(&quot;Waiting for Strapi...&quot;);
  12. await new Promise((resolve) =&gt; setTimeout(resolve, 30000));
  13. }
  14. retries++;
  15. }
  16. };
  17. waitForStrapi();
  18. console.log(&quot;[ERROR - Couldn&#39;t connect to Strapi]&quot;);

My CMS image is fully built BUT is never running (so http://localhost:1337 can never be resolved) and I don't know why...

Am I missing something or may I not have the right philosophy to achieve this ?
Thanks for your help

答案1

得分: 0

我认为你缺少了docker-compose文件的"command"部分,所以它应该看起来像这样:

  1. version: "3.8"
  2. services:
  3. db:
  4. image: mariadb:latest
  5. environment:
  6. - MYSQL_ROOT_PASSWORD=root
  7. - MYSQL_USER=root
  8. - MYSQL_PASSWORD=root
  9. - DATABASE_SSL=false
  10. ports:
  11. - "3306:3306"
  12. volumes:
  13. - ./server/strapi_portfolio.sql:/docker-entrypoint-initdb.d/strapi_portfolio.sql
  14. networks:
  15. - portfolio-network
  16. portfolio-strapi:
  17. build:
  18. context: ./server
  19. ports:
  20. - "1337:1337"
  21. volumes:
  22. - ./portfolio-strapi:/usr/src/app
  23. environment:
  24. - NODE_ENV=development
  25. networks:
  26. - portfolio-network
  27. depends_on:
  28. - db
  29. command: npm run start
  30. portfolio-nextjs:
  31. build:
  32. context: ./client
  33. ports:
  34. - "3000:3000"
  35. volumes:
  36. - ./portfolio-nextjs:/usr/src/app
  37. environment:
  38. - NODE_ENV=development
  39. networks:
  40. - portfolio-network
  41. depends_on:
  42. - portfolio-strapi
  43. command: npm run start
  44. networks:
  45. portfolio-network:
  46. driver: bridge

希望这有所帮助。

英文:

I think you are missing the "command" section of the docker-compose file, so it would look something like this:

  1. version: &quot;3.8&quot;
  2. services:
  3. db:
  4. image: mariadb:latest
  5. environment:
  6. - MYSQL_ROOT_PASSWORD=root
  7. - MYSQL_USER=root
  8. - MYSQL_PASSWORD=root
  9. - DATABASE_SSL=false
  10. ports:
  11. - &quot;3306:3306&quot;
  12. volumes:
  13. - ./server/strapi_portfolio.sql:/docker-entrypoint-initdb.d/strapi_portfolio.sql
  14. networks:
  15. - portfolio-network
  16. portfolio-strapi:
  17. build:
  18. context : ./server
  19. ports:
  20. - &quot;1337:1337&quot;
  21. volumes:
  22. - ./portfolio-strapi:/usr/src/app
  23. environment:
  24. - NODE_ENV=development
  25. networks:
  26. - portfolio-network
  27. depends_on:
  28. - db
  29. command: npm run start
  30. portfolio-nextjs:
  31. build:
  32. context : ./client
  33. ports:
  34. - &quot;3000:3000&quot;
  35. volumes:
  36. - ./portfolio-nextjs:/usr/src/app
  37. environment:
  38. - NODE_ENV=development
  39. networks:
  40. - portfolio-network
  41. depends_on:
  42. - portfolio-strapi
  43. command: npm run start
  44. networks:
  45. portfolio-network:
  46. driver: bridge

Hope this helps

huangapple
  • 本文由 发表于 2023年6月15日 17:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定