7

I usually write Dockerfiles for Java / Go applications and it's the first time I have encountered a situation where I have to write a Dockerfile for an already existing (and production running) Node.js application. As per my little knowledge about the Node.js which I acquired in the past couple of days, dist folder is generated after we build a Node.js project which carries the source code (please correct me if I am wrong here). So I am interested in copying the dist folder from parent Docker image to child Docker image.

However, after I copy everything from an application into my parent Docker image (line 6) and run 'npm run build' command, dist folder is not generated for me (please note the node_modules and package-lock.json are being generated).

My Dockerfile is as below:

FROM node:10-alpine as BUILD
WORKDIR /src
COPY package*.json /src
RUN apk add --update --no cache \
    python \
    make \
    g++
RUN npm install
COPY . /src
RUN npm run build

How can I resolve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
PS04
  • 75
  • 1
  • 1
  • 3
  • 2
    Does it work outside of Docker? What do or don't you see? What does the `"build"` command do inside your `package.json` file? – David Maze Jan 11 '20 at 11:45
  • @DavidMaze - Hi, I just tried locally building the project and I am surprised to see that even when I locally build it, **dist** folder is not getting generated. Not sure what would be the reason behind this (please note that node_modules and package-lock,json is generated). Also, I didn't get any error post build and **Compiled Successfully** message was shown. Coming to your 2nd point - build command inside package.json is written as - "build" = "next build". Please let me know if you need any additional information – PS04 Jan 11 '20 at 14:56
  • You may find the following reading interesting: https://stackoverflow.com/questions/37975500/npm-install-doesnt-create-dist-folder – Zeitounator Jan 11 '20 at 14:59
  • @Zeitounator - I went through the article and modified Dockerfile to include below 2 commands - ```RUN npm install -g grunt-cli RUN npm install grunt``` just before the line ```COPY . /src```. Still I am not able to see dist folder generated in my docker image – PS04 Jan 11 '20 at 17:11

3 Answers3

11

If you are using typescript in your node application then follow these instructions.

Please add the below entry under compilerOptions section on "tsconfig.json"

tsconfig.json

**"outDir": "./dist/"**

package.json - Add the below script too.

"scripts": { "build": "tsc" }

Now, re-run the "npm run build". You will see the dist folder.

SkyArc
  • 406
  • 1
  • 6
  • 17
1

Try by ignoring the 'tsconfig.tsbuildinfo' file. don't copy this file into docker container.

Raman Garg
  • 91
  • 1
  • 2
0

Anyone using nestjs, Oddly in my Dockerfile I had RUN npm run build and was not pulling any folders/files besides tsconfig.build.tsbuildinfo, once I made it RUN npx nest build it copied all of the files over.

jawn
  • 851
  • 7
  • 10