Containerization simplifies full-stack development by packaging your application with its dependencies in an isolated environment. In this guide, weβll walk through how to containerize a Next.js app, link it with a PostgreSQL database, and add pgAdmin for database managementβall using Docker.
π Source Code: GitHub β next-docker-template
Why Use Docker for a Full-Stack Setup?
- Uniform Development Environments: Works the same on every machine
- Separation of Concerns: App, DB, and tooling run independently
- Ease of Onboarding: New team members can start instantly
- Scalability: Ready for CI/CD pipelines and production deployment
Project Structure Overview
The entire project resides in the next-app directory, following this layout:
next-app/
βββ app/ # Core Next.js app code
βββ public/ # Static files
βββ nginx/ # Optional: reverse proxy setup
βββ Dockerfile # Docker build instructions
βββ docker-compose.yml # Multi-service orchestration
βββ .env # Environment config
βββ generate-docker-files.js # Script to generate Docker files
π For complete project files and folder structure, refer to the GitHub repository linked above.
Dockerfile: Building the Next.js Application
The Dockerfile defines how the container image is built. We use a minimal Node.js base image, install dependencies, copy in the code, and expose the necessary port.
To keep this post clean, you can explore the file here:
π Dockerfile on GitHub
docker-compose.yml: Service Orchestration
docker-compose.yml handles the setup of three services:
- web: The main Next.js development server
- db: A PostgreSQL database (version 16)
- pgadmin: A web interface to manage PostgreSQL visually
All services run in their own containers but are connected via Dockerβs internal network.
π Explore the full config file:
π docker-compose.yml
PostgreSQL + pgAdmin: Local Database Setup
PostgreSQL is configured with default credentials (postgres for both user and password). pgAdmin is served at http://localhost:5050 and provides a GUI for managing the database.
Connecting pgAdmin to PostgreSQL
- Open pgAdmin in your browser (
localhost:5050) - Log in using:
-
Email:
admin@admin.com -
Password:
admin
-
Email:
- Add a new server:
- Name: anything you like
-
Host:
db(the Docker service name) -
Username/Password:
postgres/postgres
.dockerignore: Optimizing the Build
The .dockerignore file ensures certain files and directories are excluded from your Docker image to keep it lean and secure. Example entries include:
node_modules
.next
npm-debug.log
.env
.DS_Store
Bonus: Auto-Generate Docker Setup Files
To speed up your setup, a helper script generate-docker-files.js is included. Run it once to auto-generate your Dockerfile, docker-compose.yml, and .dockerignore.
βοΈ Script source:
generate-docker-files.js
Running Everything
With all configuration files in place, start your app using:
docker-compose up --build
Then access the following:
- Next.js App:
http://localhost:3000 - pgAdmin:
http://localhost:5050 - PostgreSQL: available locally on port
5432
Wrap-Up
You now have a fully dockerized Next.js development environment with PostgreSQL and pgAdmin. This stack provides a solid base for full-stack development, CI/CD pipelines, and scalable deployment.
β Clone and explore the full project:
https://github.com/ankitparashar700/next-docker-template
Want to contribute or suggest improvements? Open an issue or fork the repo on GitHub.
Happy coding!
Top comments (0)