Dockerizing Next.js Dev Server (Issue)
Unanswered
Abyssinian posted this in #help-forum
AbyssinianOP
So I am building an application with Laravel & Next.js. I dockerized it with docker-compose (using postgres, nginx, laravel, next.js). The thing is that the front-end Next.js part only works, when I stop the Next.js container (keeping all the other back-end containers up) and then running the dev server on my host machine (and sending a request to https://localhost:8000 being the back-end).
But when I boot up the Next.js container and try to run the site there I get a lot of connection issues and it does not really work. Is that because Next.js first pre-renders on the server and then after rendering sends requests from the browser, so it initially cannot talk to the localhost? The thing is that I tried this
const apiUrl = typeof window === 'undefined'
? "http://nginx:8000" // server-side, inside Docker container
: "http://localhost:8000"; // client-side, from the browser
but this still does not work. What could be the issue?
But when I boot up the Next.js container and try to run the site there I get a lot of connection issues and it does not really work. Is that because Next.js first pre-renders on the server and then after rendering sends requests from the browser, so it initially cannot talk to the localhost? The thing is that I tried this
const apiUrl = typeof window === 'undefined'
? "http://nginx:8000" // server-side, inside Docker container
: "http://localhost:8000"; // client-side, from the browser
but this still does not work. What could be the issue?
62 Replies
AbyssinianOP
please
help
me
Is that because Next.js first pre-renders on the server and then after rendering sends requests from the browser, so it initially cannot talk to the localhost?
No, that wouldn't be it.
I'd say it's highly likely to be an issue with your docker setup / docker networking setup.
Especially if you're able to spin up the frontend independently and hit the backend that way
Can you send through your compose file(s)?
AbyssinianOP
services:
# Backend service for Laravel
project_back:
build:
context: ./projectBack
dockerfile: Dockerfile
container_name: laravel_app
working_dir: /var/www
volumes:
- ./projectBack:/var/www
environment:
- DB_HOST=postxgres
- DB_PORT=xxx
- DB_DATABASE=xxxx
- DB_USERNAME=xxx
- DB_PASSWORD=xxxx
networks:
- laravel_network
depends_on:
- postgres
# Frontend service
project_front:
build:
context: ./projectFront
dockerfile: Dockerfile
container_name: nextjs_frontend
ports:
- "3000:3000"
env_file:
- ./projectFront/.env
volumes:
- ./projectFront:/app
networks:
- laravel_network
depends_on:
- project_back
# PostgreSQL service
postgres:
image: postgres:13
container_name: postgres_db
environment:
POSTGRES_USER: xxx
POSTGRES_PASSWORD: xxx
POSTGRES_DB: xxx
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- laravel_network
# Nginx to serve the Laravel application
nginx:
image: nginx:alpine
container_name: nginx_web
working_dir: /var/www
volumes:
- ./projectBack:/var/www
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
networks:
- laravel_network
depends_on:
- project_back
networks:
laravel_network:
driver: bridge
volumes:
pgdata:
driver: local
# Backend service for Laravel
project_back:
build:
context: ./projectBack
dockerfile: Dockerfile
container_name: laravel_app
working_dir: /var/www
volumes:
- ./projectBack:/var/www
environment:
- DB_HOST=postxgres
- DB_PORT=xxx
- DB_DATABASE=xxxx
- DB_USERNAME=xxx
- DB_PASSWORD=xxxx
networks:
- laravel_network
depends_on:
- postgres
# Frontend service
project_front:
build:
context: ./projectFront
dockerfile: Dockerfile
container_name: nextjs_frontend
ports:
- "3000:3000"
env_file:
- ./projectFront/.env
volumes:
- ./projectFront:/app
networks:
- laravel_network
depends_on:
- project_back
# PostgreSQL service
postgres:
image: postgres:13
container_name: postgres_db
environment:
POSTGRES_USER: xxx
POSTGRES_PASSWORD: xxx
POSTGRES_DB: xxx
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- laravel_network
# Nginx to serve the Laravel application
nginx:
image: nginx:alpine
container_name: nginx_web
working_dir: /var/www
volumes:
- ./projectBack:/var/www
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
networks:
- laravel_network
depends_on:
- project_back
networks:
laravel_network:
driver: bridge
volumes:
pgdata:
driver: local
the back end completely works and runs and as i mentioned when i run the next js front end independent it works 100%, when i use it in my docker container it can somehow communicate with the back end apparently because some requests come through but overall the application is broken because it still cannot communicate correctly
Can you send it as a snippet or wrapped in a code block?
AbyssinianOP
services:
# Backend service for Laravel
project_back:
build:
context: ./projectBack
dockerfile: Dockerfile
container_name: laravel_app
working_dir: /var/www
volumes:
./projectBack:/var/www
environment:
DB_HOST=postxgres
DB_PORT=xxx
DB_DATABASE=xxxx
DB_USERNAME=xxx
DB_PASSWORD=xxxx
networks:
laravel_network
depends_on:
postgres
# Frontend service
project_front:
build:
context: ./projectFront
dockerfile: Dockerfile
container_name: nextjs_frontend
ports:
"3000:3000"
env_file:
./projectFront/.env
volumes:
./projectFront:/app
networks:
laravel_network
depends_on:
project_back
# PostgreSQL service
postgres:
image: postgres:13
container_name: postgres_db
environment:
POSTGRES_USER: xxx
POSTGRES_PASSWORD: xxx
POSTGRES_DB: xxx
ports:
"5432:5432"
volumes:
pgdata:/var/lib/postgresql/data
networks:
laravel_network
# Nginx to serve the Laravel application
nginx:
image: nginx:alpine
container_name: nginx_web
working_dir: /var/www
volumes:
./projectBack:/var/www
./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
"8000:80"
networks:
laravel_network
depends_on:
project_back
networks:
laravel_network:
driver: bridge
volumes:
pgdata:
driver: local
Thank you!
I can’t tell from a quick glance (not at the desk)
But we use Laravel backends / next frontends at our company with a similar setup
So I’ll try and run it when I get home and see
AbyssinianOP
okay would be nice!
do you use it for the development server as well?
id might try to run this with a production build as well, i cannot find a lot of docs about docker and dev servers
but i thought that it would be convinient to have everything through compose
On local we use sail/octane for Laravel and normal
npm run dev
/ pnpm dev
for nextOn prod we also deploy both separately
AbyssinianOP
yeah so on dev servers you don't use docker for next?
Nah, and never compose both
We do use a dockerfile for next on prod though
AbyssinianOP
maybe this is not common to dockerize the dev set-up? i just thought that it would be convinient to boot up everything at once
yea i have different dockerfiles as well
Yeah, not super common in the next/react world from what I’ve seen
AbyssinianOP
so probably i should just keep the back-end dockerized
I do like doing a nice compose for my backend to save the db hassle etc
AbyssinianOP
i mean yea its super convenient though
But honestly the tooling is so good these days for frontends that just a simple dev command and you’re good to go
AbyssinianOP
yea well i just used npm run dev and php artisan serve
all the time
but im working on it solo rn and the devs before used docker
so i thought that in order to have no problems in the future i should return to running it through docker
Haha, that’s fair. A mini bash script to automate spinning both up might be less of a headache honestly lol
AbyssinianOP
but i think they only dockerized the back-end
yea true, i just learned docker and thought that well why not use it, its popular and modern
yk
and tbh booting up the laravel back-end really is very convinient that way
Or tmux as well
AbyssinianOP
+ the postgres database
Yeah I definitely hear you
I’d stick with dockerized backend and normal front end imo for sure
AbyssinianOP
okay then ill probably do that, i think they did that as well because i could only find one dockerfile in the project
Side note, we use dedoc scramble to generate openapi specs from our laravel backends and generate types for the frontend from the specs
It’s super cool
On the topic of Laravel / next dev
AbyssinianOP
oh so it just looks through the laravel back end and generates docs about your application that way?
do i understand correctly
api docs
Yep you’ve got it!
Sometimes you have to nudge it with some documentation comments
But it’s a cool flow
And then there are a ton of tools that can take the openapi spec and turn it into TS types, zod schemas
Some can even do full fetch clients, axios, react query
Just thought I’d share that with a fellow Laravel / next dev 🫡
AbyssinianOP
yes ill try that out tomorrow because i just wanted to create docs and also generating types is also great because im always so lazy about that
do you have any suggestions how to level up my knowledge on laravel other than the docs? because im coming for a node.js background and while i know php and understand laravel kind-off, i don't want to ruin all the code
because i understand migrations, routes, controllers etc but laravel is still a little overwhelming
Sorry, had to tend to something
Hmm, that's a good question. I'd say some advice in regards to leveling up/not wanting to ruin the code/being overwhelmed, I'd say
1. Don't feel like you have to use or understand all of the offerings that Laravel has. It is an extensive framework and you aren't meant to use every single feature. We have plenty of relatively large scale products, and none of them use all of it.
2. Unlike JS, it's relatively more difficult to ruin your project when writing a Laravel application, so don't worry. Don't get me wrong, you can achieve spaghetti in any language. But, Laravel is a fairly opinionated, rigid framework, that forces you into a solid structure. Even the worst Laravel codebase I've ever seen doesn't hold weight to the worst JS codebase I've ever seen.
1. Don't feel like you have to use or understand all of the offerings that Laravel has. It is an extensive framework and you aren't meant to use every single feature. We have plenty of relatively large scale products, and none of them use all of it.
2. Unlike JS, it's relatively more difficult to ruin your project when writing a Laravel application, so don't worry. Don't get me wrong, you can achieve spaghetti in any language. But, Laravel is a fairly opinionated, rigid framework, that forces you into a solid structure. Even the worst Laravel codebase I've ever seen doesn't hold weight to the worst JS codebase I've ever seen.