Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomic listening port on docker execution #2301

Open
BossHogg97 opened this issue Mar 19, 2024 · 7 comments
Open

Randomic listening port on docker execution #2301

BossHogg97 opened this issue Mar 19, 2024 · 7 comments

Comments

@BossHogg97
Copy link

Hi, i'm working with Node 20.11.0 and the following package.json:

"devDependencies": {
    "fastify": "^4.26.2",
    "typescript": "^5.4.2"
  },
  "dependencies": {
    "@platformatic/db": "^1.28.1",
    "platformatic": "^1.28.1"
  },

I have two main issues:

  1. Plugin not start in time
2024-03-19 14:52:22 {"level":50,"time":1710856342852,"pid":7,"hostname":"7726d3494555","name":"mizar","err":{"type":"FastifyError","message":"Plugin did not start in time: 'platformaticDB'. You may have forgotten to call 'done' function or to resolve a Promise","stack":"FastifyError: Plugin did not start in time: 'platformaticDB'. You may have forgotten to call 'done' function or to resolve a Promise\n    at Timeout._onTimeout (/home/platformatic/node_modules/avvio/lib/plugin.js:112:31)\n    at listOnTimeout (node:internal/timers:569:17)\n    at process.processTimers (node:internal/timers:512:7)","code":"AVV_ERR_PLUGIN_EXEC_TIMEOUT","name":"FastifyError","statusCode":500},"msg":"Plugin did not start in time: 'platformaticDB'. You may have forgotten to call 'done' function or to resolve a Promise"}
  1. When i create the docker container it automatically change the PORT with a random one and server listen on it. I defined the port inside the .env file

Following dockerfile:

FROM platformatic/platformatic:latest
 
USER root
 
RUN corepack enable && corepack prepare pnpm@latest --activate
 
WORKDIR /app
 
COPY package.json pnpm-lock.yaml tsconfig.json ./
 
COPY ./services/mizar/global.d.ts ./services/mizar/platformatic.json ./
 
COPY ./services/mizar/migrations   migrations
COPY ./services/mizar/types        types
COPY ./services/mizar/routes       routes
COPY ./services/mizar/plugins      plugins
COPY ./services/mizar/public       public
 
RUN pnpm install --fix-lockfile --frozen-lockfile
RUN pnpm run build
 
EXPOSE 5004
 
CMD ["platformatic", "start"]
 
#ENTRYPOINT ["tail", "-f", "/dev/null"]

Here my docker-compose:

name : mizar-compose
 
services:
  mizar-api-new:
    image: mizar-api
    container_name: mizar-api-new
    restart: unless-stopped
    environment:
      - PLT_SERVER_HOSTNAME=0.0.0.0
      - PORT=5004
      - PLT_SERVER_LOGGER_LEVEL=info
      - PLT_MIZAR_DATABASE_URL=postgresql://postgres:changeme@postgresql:5432/mizardb
      - PLT_MIZAR_APPLY_MIGRATIONS=true
      - PLT_SERVER_CORS_ORIGIN=http://localhost:3004/
    ports:
      - 5004:5004

Finally the server listening to random PORT (in this execution is the 42779). So the container became unreachable

2024-03-19 14:52:37 {"level":30,"time":1710856357497,"pid":7,"hostname":"7726d3494555","name":"mizar","msg":"All routes loaded! Check your console for the route details."}
2024-03-19 14:52:37 └── / (GET, HEAD)
2024-03-19 14:52:37     ├── documentation (GET, HEAD)
2024-03-19 14:52:37     │   └── /
2024-03-19 14:52:37     │       ├── json (GET, HEAD)
2024-03-19 14:52:37     │       ├── yaml (GET, HEAD)
2024-03-19 14:52:37     │       └── @scalar/fastify-api-reference/js/browser.js (GET, HEAD)
2024-03-19 14:52:37     └── shop
2024-03-19 14:52:22 The runtime exited before the operation completed
2024-03-19 14:52:37         ├── carts (GET, HEAD, POST, PUT)
2024-03-19 14:52:37         │   └── / (GET, HEAD, POST, PUT)
2024-03-19 14:52:37         │       └── :uuid (GET, HEAD, PUT, DELETE)
2024-03-19 14:52:37         │           └── /shopitems (GET, HEAD)
2024-03-19 14:52:37         └── items (GET, HEAD, POST, PUT)
2024-03-19 14:52:37             └── / (GET, HEAD, POST, PUT)
2024-03-19 14:52:37                 └── :uuid (GET, HEAD, PUT, DELETE)
2024-03-19 14:52:37                     └── /uuid (GET, HEAD)
2024-03-19 14:52:37 
2024-03-19 14:52:37 {"level":30,"time":1710856357509,"pid":7,"hostname":"7726d3494555","name":"mizar","msg":"Server listening at
http://127.0.0.1:42779"}

How can i solve these issues? Thanks

@mcollina
Copy link
Member

what's your config file?

@BossHogg97
Copy link
Author

I have these files:

{
  "$schema": "[https://platformatic.dev/schemas/v1.19.0/runtime"](https://platformatic.dev/schemas/v1.19.0/runtime%22),
  "entrypoint": "mizar",
  "allowCycles": false,
  "hotReload": true,
  "autoload": {
    "path": "services",
    "exclude": [
      "docs"
    ]
  },
  "server": {
    "hostname": "{PLT_SERVER_HOSTNAME}",
    "port": 5004,
    "cors": {
      "origin": "{PLT_SERVER_CORS_ORIGIN}"
    },
    "logger": {
      "level": "{PLT_SERVER_LOGGER_LEVEL}"
    }
  }
}

And these:

{
  "$schema": "[https://platformatic.dev/schemas/v1.19.0/db"](https://platformatic.dev/schemas/v1.19.0/db%22),
  "db": {
    "connectionString": "{PLT_MIZAR_DATABASE_URL}",
    "graphql": false,
    "openapi": true,
    "schemalock": true
  },
  "watch": {
    "ignore": [
      "*.sqlite",
      "*.sqlite-journal"
    ]
  },
  "migrations": {
    "dir": "migrations",
    "autoApply": "{PLT_MIZAR_APPLY_MIGRATIONS}"
  },
  "plugins": {
    "paths": [
      {
        "path": "./plugins",
        "encapsulate": false
      },
      {
        "path": "./routes"
      }
    ],
    "typescript": true
  },
  "types": {
    "autogenerate": true
  }
}

@mcollina
Copy link
Member

It does not seem you are copying platformatic.json inside the image

@BossHogg97
Copy link
Author

I solved the issue about the missing configuration file (inside image) and now i can create the docker. I read the documentation about the composition of dockerfile but i think is not complied to the latest version of PLT

Following the actual dockerfile composition:

FROM platformatic/platformatic:latest
USER root
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
COPY . .
RUN pnpm install --fix-lockfile --frozen-lockfile
RUN pnpm run build
EXPOSE 5003
CMD ["platformatic", "start"]

I know that the command COPY . . is not the best practice for build the image but at the moment i haven't found a better way. How can i improve my dockerfile?

Thanks

@mcollina
Copy link
Member

It depends if you want to deploy your platformatic runtime application or just your service individually.
Your repo is configured for the platformatic runtime but you are trying to build your dockerfile for the individual service.

What's the case?

@BossHogg97
Copy link
Author

BossHogg97 commented Mar 21, 2024

My use case is service. But how can i configure my PLT db and docker correctly?
Thanks

@mcollina
Copy link
Member

You need to add a "server": { ... }" config block in the platformatic.json` inside your service folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants