Next.js Discord

Discord Forum

Next.js Docker container with Strapi

Unanswered
Bengal posted this in #help-forum
Open in Discord
BengalOP
I'm using a dockerfile similar to the one here:
"https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile"

My issue is that it tries to build the app in the image, but I need my strapi app to be running during build time so that it fetches data from it, how can I achieve that?

33 Replies

Pyrrhuloxia
hey man i'm exactly having the same issue and i'm now thinking that using docker compose is not possible way to do it since building process is not ordered in docker compose instead of using the docker compose just going to create a bash file and create network volues there and will do accordingly
this problem is "static site generation fails with docker compose"
Golden-winged Warbler
Normally, wouldn't the strapi server always be running, since it acts more like a 3rd party service that stores data?

We use https://dagger.io as our post-dockerfile solution: https://github.com/hofstadter-io/hof/blob/docs/updates/next/ci/dagger.mts

It is possible to run "services" or "sidecars" during the build/test process with Dagger.
Pyrrhuloxia
hm so separate solutions instead of composing
so intead of docker compose maybe compose.sh
Golden-winged Warbler
for your case, just use depends_on for docker compose
Pyrrhuloxia
yup i used it, but it doesn't re-order the builds, it just re-order the container starts
i can use healthchecks also
Golden-winged Warbler
you should always use alive/ready probes
Pyrrhuloxia
service_healhty?
Golden-winged Warbler
not sure what they are in DC, we use k8s
Pyrrhuloxia
okay but how to know if a container is initialized completely, i mean it's a separate process how the base os can know that the mysql is started in the container for example
@Golden-winged Warbler not sure what they are in DC, we use k8s
Pyrrhuloxia
would love to use that but we are really low on resources
Golden-winged Warbler
knowing when a service is ready is always specific to that service
Pyrrhuloxia
curl or ping with an interval?
Golden-winged Warbler
whatever your services' documentation tells you
I assume strapi and mysql have something like this
Pyrrhuloxia
strapi is not very well documented the dockerization and for mysql it's quite straight forward
Golden-winged Warbler
BengalOP
The issue is that even if build depends on strapi server being running
that means it wouldn't build during container runtime
because it only has one command during runtime
like if that was even possible, it would just skip building altogether i think
a workaround might be that i build the project myself
and use docker to copy the pre existing build files
so that it doesn't need to b uild
BengalOP
I think I found a possible solution, in the packages.json for next.js you can edit the command you want to use for docker runtime (either "start" or create your own) and basically just add the two commands as you would usually do in packages.json, then use that command to run the container:
this should implement the wanted behavior, in which the website is built at runtime instead of build time, so that it's doing that while server is running
BengalOP
this is the error i'm currently facing:
BengalOP
ok this was just my api tokens
i think this solution should work