Typescript multistage Dockerfile: Out of memory

I have a simple multi stage Dockerfile which compiles some TS code in the building stage, than runs the resulting JS code in the 2nd one. I’m at my 20th deploy already, changing the Dockerfile each time to try and get it right. Do you see any obvious mistakes?

I have no problem buying the next tier for some extra RAM but I don’t see why a simple WS server would require this much resources.

Dockerfile:

FROM node:18-alpine AS builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
ENV NODE_ENV=production
RUN npm prune --production

FROM node:18-alpine AS main
ENV NODE_ENV=production
WORKDIR /app
COPY --from=builder /app/dist/ dist/
COPY --from=builder /app/node_modules/ node_modules/
COPY --from=builder /app/.env .env
EXPOSE 8080
CMD ["node", "dist/src/index.js"]

fly.toml:

# fly.toml file generated for weat-ws on 2022-08-04T09:34:50+03:00

app = "weat-ws"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
builder = "heroku/buildpacks:20"

[build.args]

[env]
PORT = "8080"

[experimental]
allowed_public_ports = []
auto_rollback = true

[[services]]
http_checks = []
internal_port = 8080
processes = ["app"]
protocol = "tcp"
script_checks = []

[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443

[[services.tcp_checks]]
grace_period = "30s"
interval = "15s"
restart_limit = 0
timeout = "2s"

Error logs:

2022-08-12T08:03:16Z   [info]npm notice
2022-08-12T08:03:16Z   [info]npm notice New minor version of npm available! 8.12.1 -> 8.17.0
2022-08-12T08:03:16Z   [info]npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.17.0>
2022-08-12T08:03:16Z   [info]npm notice Run `npm install -g npm@8.17.0` to update!
2022-08-12T08:03:16Z   [info]npm notice
2022-08-12T08:03:16Z   [info]npm notice
2022-08-12T08:03:16Z   [info]npm notice New minor version of npm available! 8.12.1 -> 8.17.0
2022-08-12T08:03:16Z   [info]npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.17.0>
2022-08-12T08:03:16Z   [info]npm notice Run `npm install -g npm@8.17.0` to update!
2022-08-12T08:03:16Z   [info]npm notice
2022-08-12T08:03:17Z   [info]Stan child exited normally with code: 137
2022-08-12T08:03:17Z   [info]Starting clean up.
2022-08-12T08:03:23Z   [info]Starting instance
2022-08-12T08:03:23Z   [info]Configuring virtual machine
2022-08-12T08:03:23Z   [info]Pulling container image
2022-08-12T08:03:23Z   [info]Unpacking image
2022-08-12T08:03:23Z   [info]Preparing kernel init
2022-08-12T08:03:24Z   [info]Configuring firecracker
2022-08-12T08:03:24Z   [info]Starting virtual machine
2022-08-12T08:03:24Z   [info]Starting init (commit: c86b3dc)...
2022-08-12T08:03:24Z   [info]Preparing to run: `/cnb/process/web` as heroku
2022-08-12T08:03:24Z   [info]2022/08/12 08:03:24 listening on [fdaa:0:7d64:a7b:a992:290:4531:2]:22 (DNS: [fdaa::3]:53)
2022-08-12T08:03:24Z   [info]> websocket@1.0.0 prestart
2022-08-12T08:03:24Z   [info]> npm run build
2022-08-12T08:03:25Z   [info]> websocket@1.0.0 build
2022-08-12T08:03:25Z   [info]> tsc
2022-08-12T08:03:41Z   [info][   16.758920] Out of memory: Killed process 547 (node) total-vm:684680kB, anon-rss:140828kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:3132kB oom_score_adj:0
2022-08-12T08:03:41Z   [info]Killed
2022-08-12T08:03:41Z   [info]Main child exited normally with code: 137
2022-08-12T08:03:41Z   [info]Starting clean up.
--> v20 failed - Failed due to unhealthy allocations - rolling back to job version 19 and deploying as v21

--> Troubleshooting guide at https://fly.io/docs/getting-started/troubleshooting/
Error abort

Try removing the build section which is forcing your project to use a buildpack instead of the Dockerfile.

1 Like

Thank you so much, it’s working great now! I think I’ve missed that part of the documentation, I will make sure to read about the underlying builders.