Dokploy / dokploy

Open Source Alternative to Vercel, Netlify and Heroku.
https://dokploy.com/
Other
7.4k stars 369 forks source link

Connecting Next.js with server actions to database with Internal Connection URL #507

Open milanpanin opened 1 week ago

milanpanin commented 1 week ago

To Reproduce

After I created the Next application and deployed it to Dokploy and connected to the Dokploy database (PostgreSQL), communication works if I connect via an external connection string, but when I change that connection string to internal in the environment, then the build of Next application crashes.

Current vs. Expected behavior

I expect to have a connection to the database through internal communication on the server and not through the Internet. I see internet connect to databse as a huge security flaw.

Provide environment information

Ubuntu, Hetzner cax11 4gb.

Error in dokploy after I made change in ENV from external to internal connection string...

Initializing deployment
Build heroku_buildpacks: ✅
Source Type: github: ✅
24: Pulling from heroku/builder
Digest: sha256:11c05fd10f421614227b72ad21c5e0d44ca3ca1a3e1f6592da92f039e09d33f9
Status: Image is up to date for heroku/builder:24
24: Pulling from heroku/heroku
Digest: sha256:b2e3a768f6a89d3c47def9bda468b15eb9ba66ca4973e05546417b99bdefe94a
Status: Image is up to date for heroku/heroku:24
===> ANALYZING
Restoring data for SBOM from previous image
===> DETECTING
2 of 5 buildpacks participating
heroku/nodejs-engine      3.2.14
heroku/nodejs-npm-install 3.2.14
===> RESTORING
Restoring metadata for "heroku/nodejs-engine:web_env" from app image
Restoring metadata for "heroku/nodejs-engine:dist" from app image
Restoring metadata for "heroku/nodejs-engine:node_runtime_metrics" from app image
Restoring metadata for "heroku/nodejs-npm-install:npm_runtime_config" from app image
Restoring metadata for "heroku/nodejs-npm-install:npm_cache" from cache
Restoring data for "heroku/nodejs-engine:dist" from cache
Restoring data for "heroku/nodejs-npm-install:npm_cache" from cache
===> BUILDING

[Heroku Node.js Engine Buildpack]

[Checking Node.js version]
Node.js version not specified, using 20.x
Resolved Node.js version: 20.17.0

[Installing Node.js distribution]
Reusing Node.js 20.17.0 (linux-arm64)
Installing application metrics scripts

# Heroku Node.js npm Install Buildpack

- Installing node modules
  - Using npm version `10.8.2`
  - Restoring npm cache
  - Configuring npm cache directory
  - Running `npm ci "--production=false"`

      npm warn config production Use `--omit=dev` instead.

      added 52 packages, and audited 53 packages in 6s

      3 packages are looking for funding
        run `npm fund` for details

      found 0 vulnerabilities

  - Done (6.321s)
- Running scripts
  - Running `npm run build`

      > blog@0.1.0 build
      > next build

      Attention: Next.js now collects completely anonymous telemetry regarding usage.
      This information is used to shape Next.js' roadmap and prioritize features.
      You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
      https://nextjs.org/telemetry

        ▲ Next.js 14.2.13

         Creating an optimized production build ...
       ✓ Compiled successfully
         Linting and checking validity of types ...
         Collecting page data ...
         Generating static pages (0/4) ...
         Generating static pages (1/4) 

         Generating static pages (2/4) 

         Generating static pages (3/4) 

      Error: getaddrinfo EAI_AGAIN testing-test-2b746e
          at /workspace/node_modules/pg-pool/index.js:45:11
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async e (/workspace/.next/server/app/page.js:1:2507)
          at async a (/workspace/.next/server/app/page.js:1:2581) {
        errno: -3001,
        code: 'EAI_AGAIN',
        syscall: 'getaddrinfo',
        hostname: 'testing-test-2b746e',
        digest: '2404072697'
      }
      Error: getaddrinfo EAI_AGAIN testing-test-2b746e
          at /workspace/node_modules/pg-pool/index.js:45:11
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async e (/workspace/.next/server/app/page.js:1:2507)
          at async a (/workspace/.next/server/app/page.js:1:2581) {
        errno: -3001,
        code: 'EAI_AGAIN',
        syscall: 'getaddrinfo',
        hostname: 'testing-test-2b746e',
        digest: '2404072697'
      }
      Error: getaddrinfo EAI_AGAIN testing-test-2b746e
          at /workspace/node_modules/pg-pool/index.js:45:11
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async e (/workspace/.next/server/app/page.js:1:2507)
          at async a (/workspace/.next/server/app/page.js:1:2581) {
        errno: -3001,
        code: 'EAI_AGAIN',
        syscall: 'getaddrinfo',
        hostname: 'testing-test-2b746e',
        digest: '2404072697'
      }

      Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error

      Error: getaddrinfo EAI_AGAIN testing-test-2b746e
          at /workspace/node_modules/pg-pool/index.js:45:11
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async e (/workspace/.next/server/app/page.js:1:2507)
          at async a (/workspace/.next/server/app/page.js:1:2581)
       ✓ Generating static pages (4/4)

      > Export encountered errors on following paths:
        /page: /

  - Done (17.285s)
- Debug info
  - Command failed `npm run build`
    exit status: 1
    stdout: <see above>
    stderr: <see above>

! Failed to execute build script.
!
! The `Heroku Node.js npm Install Buildpack` allows customization of the build process by executing the following scripts if they are defined in `package.json`:
! - `heroku-prebuild` 
! - `heroku-build` or `build` 
! - `heroku-postbuild`
!
! An unexpected error occurred while executing `npm run build`. See the log output above for more information.
!
! Ensure that this command runs locally without error and retry your build.

ERROR: failed to build: exit status 1
ERROR: failed to build: executing lifecycle: failed with status code: 51
Error ❌
ERROR: failed to build: executing lifecycle: failed with status code: 51

Which area(s) are affected? (Select all that apply)

Application, Docker Compose, Docker

Additional context

I'll pass the repository of an example Next application that accesses the database. It is necessary to set the environment variable in the Dokploy to the database for access. Varaible name: DATABASE_URL

https://github.com/milanpanin/test-dokploy

milanpanin commented 1 week ago

@Siumauricio Do you have any suggestions on how to fix this problem?

sangdth commented 1 week ago

I think I also face this issue, so if this is not intentional then it is a huge bug :(

Siumauricio commented 1 week ago

The error is normal to happen, one way to solve it is to use an internal network so that the services can communicate, I know nixpacks has an open issue https://github.com/railwayapp/nixpacks/issues/1057, I do not know if heroku has an option to pass a custom network, the only way to solve that could be using your own custom Dockerfile