admin管理员组

文章数量:1316665

I'm trying to send a request in my getStaticProps function to my backend api from another docker container. However, even though the api url is written correctly, the static page still is not created. This is because for the static page to be built, the backend should be up already, and because it is build-time the other container is not up yet and waits for the build to be finished and the build can not be finished without the backend.

So what's the solution to this approach? I tried setting the depends_on value to my other container, still doesn't work. what solution would you suggest?

I'm trying to send a request in my getStaticProps function to my backend api from another docker container. However, even though the api url is written correctly, the static page still is not created. This is because for the static page to be built, the backend should be up already, and because it is build-time the other container is not up yet and waits for the build to be finished and the build can not be finished without the backend.

So what's the solution to this approach? I tried setting the depends_on value to my other container, still doesn't work. what solution would you suggest?

Share Improve this question edited Jul 7, 2021 at 22:49 Dory Nguyen 3712 silver badges15 bronze badges asked Apr 19, 2021 at 12:56 DastoDasto 2008 bronze badges 7
  • Have You configured network bridge correclty? – Davit Gyulnazaryan Commented Jun 30, 2021 at 19:15
  • @Davit does this help the connectivity during the docker build? The backend isn't up yet, it's only been built. I have the same situation. – lexeme Commented Jun 30, 2021 at 19:28
  • As far as I know, services are just built in the same order they are defined in the pose file. depends_on makes the docker-pose up run and stop the services in correct order, and it should be more important, on the other hand, building order shouldn't really matter (at least that is what I think). – Davit Gyulnazaryan Commented Jun 30, 2021 at 19:36
  • 1 @lexeme please check out the answer and tell me if it works. I really hope I was helpful, cause I hate it when you write all the code but just can't even start it :) Really annoying. – Davit Gyulnazaryan Commented Jun 30, 2021 at 20:59
  • 1 What about explicitly building and starting back-end service like in the example and building the next.js service? I can't see why that wouldn't work, but please let me know if it does not. – Davit Gyulnazaryan Commented Jul 1, 2021 at 20:12
 |  Show 2 more ments

2 Answers 2

Reset to default 9 +75

There are 2 solutions I can think of.

Apparently, the Next.js build fails because the service it is querying is not running. Thus why not build and start the service it depends on explicitly and build the rest like this.

docker-pose build some_services
docker-pose up -d some_services
docker-pose build the_rest

This way the Next.js app will be able to make the request. Please keep in mind that You still need to configure the ports and networks correctly. Pretty sure this will resolve the issue.

A more 'fancy' solution would be using build-time networks which are added in the later versions, 3.4+ if I am not mistaken.

docker-pose.yml

build:
    context: ./service_directory
    network: some_network
   

For more details please see Docker-pose network

Running a new service that depends whether another service is up or not is a tricky part of docker orchestration. Keep in mind that even with Healthcheck, it doesn't guarantee you database is ready before the next stage. depends_on is not going to be really helpful, because it just determine the order of running service. docker documentation:

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. What docker doc suggests is to write a wait-for-it script or [wait-for][2] and run it after or before depends-on. For example:

build: next_service
mand: sh -c './wait-for db:5432 -- npm start'
depends_on:
  - db

And of course, you explicitly run separate docker-pose mand but that loses the point of docker orchestration.

本文标签: javascriptCan39t send request during build time in nextjs with dockerStack Overflow