I'm trying to deploy a pretty standard TypeScript Node container to Heroku. Here is the Dockerfile, that works just fine for local build.
FROM node:14-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm ci --only=production
COPY ./compiled .
COPY ./public ./public
EXPOSE 4040
CMD [ "node", "index.js" ]
// heroku.yml
build:
docker:
web: Dockerfile
Now, I know that on Heroku, the port will be provided by the platform, but I didn't make it far enough to have that problem. I'm stuck before that in the Activity log:
Step 4/7 : RUN npm ci --only=production
---> Running in 61cf46d0be1f
npm WARN lifecycle myapp_backend@1.0.0~postinstall: cannot run in wd myapp_backend@1.0.0 tsc (wd=/usr/src/app)
added 60 packages in 1.075s
Removing intermediate container 61cf46d0be1f
---> 8cce5940d0f5
Step 5/7 : COPY ./compiled .
COPY failed: stat /var/lib/docker/tmp/docker-builder777398979/compiled: no such file or directory
So I went on to fix the '...cannot run wd...' error, using this post: Npm install failed with "cannot run in wd"
// package.json
"scripts": {
"build": "tsc",
"start": "node .",
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^4.4.1",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/node": "^14.14.35",
"@types/node-fetch": "^2.5.8",
"typescript": "^4.2.3"
},
"config": {
"unsafe-perm":true
}
// Dockerfile
RUN npm ci --only=production --unsafe-perm
Step 4/7 : RUN npm ci --only=production --unsafe-perm
---> Running in 0555a5061172
added 60 packages in 1.087s
Removing intermediate container 0555a5061172
---> a9ed9b662358
Step 5/7 : COPY ./compiled .
COPY failed: stat /var/lib/docker/tmp/docker-builder271351523/compiled: no such file or directory
Ok, so I figured tsc is missing because I only install production dependencies. But if this is the case, how come it was alright with the local build? Anyway, I removed the --only=production flag, and got this:
Step 4/7 : RUN npm ci --unsafe-perm
---> Running in f3ab9181ff90
> nodemon@2.0.7 postinstall /usr/src/app/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
added 904 packages in 9.533s
Removing intermediate container f3ab9181ff90
---> 81bcb481c282
Step 5/7 : COPY ./compiled .
COPY failed: stat /var/lib/docker/tmp/docker-builder740874603/compiled: no such file or directory
UPDATE
As jonrsharpe pointed out, the Dockerfile was indeed missing the build sript, so I added it. RUN npm run build
This is how it looks like:
Step 5/8 : RUN npm run build
---> Running in b2dc7db58473
> myapp_backend@1.0.0 build /usr/src/app
> tsc
Version 4.2.3
Syntax: tsc [options] [file...]
.
.
.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myapp_backend@1.0.0 build: `tsc`
npm ERR! Exit status 1