18

I'm having trouble compiling my nodejs typescript application from a Dockerfile. When I build my docker image an inspect it is missing the dist folder entirely.

Dockerfile:

# Template: Node.js dockerfile
# Description: Include this file in the root of the application to build a docker image.

# Enter which node build should be used. E.g.: node:argon 
FROM node:latest

# Create app directory for the docker image
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app/dist

# Install app dependencies from package.json. If modules are not included in the package.json file enter a RUN command. E.g. RUN npm install <module-name>
COPY package.json /usr/src/app/
RUN     npm install
RUN     npm install tsc -g
RUN     tsc

# Bundle app source
COPY . /usr/src/app

# Enter the command which should be used when the image starts up. E.g. CMD ["node", "app.js"]
CMD [ "node", "server.js"]

When I run the image locally and ls to reveal files/folders:

# ls
node_modules  package-lock.json  package.json  src

Any suggestions to where i am going wrong?

TietjeDK
  • 1,167
  • 2
  • 15
  • 43
  • 1
    I can see a few problems: 1. You mkdir /usr/src/app and WORKDIR to a non existing sub directory. 2. You run tsc before you copy your files – Meir Jun 28 '18 at 12:43

2 Answers2

24

As I far as I know,
the WORKDIR don't have to be created by yourself. Here's the documentation for WORKDIR.

Afterwards you don't have to copy by hand to the specific folder, because after WORKDIRcommand the copy command would copy the files for you.

Therefore I suggest you to use the following Dockerfile:

    FROM node:alpine
    WORKDIR /usr/yourapplication-name
    COPY package.json .
    RUN npm install\
        && npm install typescript -g
    COPY . .
    RUN tsc
    CMD ["node", "./dist/server.js"]

As a tiny tipp: I would use typescript as a dependency in my package.json and then just use the following file:

    FROM node:alpine
    WORKDIR /usr/yourapplication-name
    COPY package.json .
    RUN npm install
    COPY . .
    RUN tsc
    CMD ["node", "./dist/server.js"]
Amy
  • 399
  • 5
  • 12
Max
  • 1,203
  • 1
  • 14
  • 28
  • 5
    The latter returns: `/bin/sh: 1: tsc: not found`. – Derk Jan Speelman Feb 11 '20 at 23:58
  • 3
    @DerkJanSpeelman: For that typescript has to be a dependency for your project (as I wrote in my answer). Then the npm install would install all dependencies for your project and the command tsc will be found – Max Feb 12 '20 at 07:41
  • I'd like to eleborate my problem further in this chat room: https://chat.stackoverflow.com/rooms/207651/docker-npm-and-typescript, I'd be greatful, if you would find the time to help me! – Derk Jan Speelman Feb 12 '20 at 10:06
  • What is the difference between "npx tsc" and "tsc"? – Sadequs Haque Aug 13 '22 at 05:48
  • @SadequsHaque `npx tsc` would not dirrectly use the `node_modules`. Though you can't rely that the version from your `package.json` is used. Though `tsc` would be the version from `./node_modules/.bin/tsc`. And I would link this post https://www.freecodecamp.org/news/npm-vs-npx-whats-the-difference/ – Max Aug 15 '22 at 08:01
  • how would you watch the changes in src for development phase if you do this? – PirateApp Feb 07 '23 at 14:46
  • 1
    @Max adding typescript as a dependency does not magically add tsc to the PATH unless installed globally with `-g`. It's just wrong. – AbandonedCrypt May 21 '23 at 12:20
  • following this answer, I got the error `:/bin/sh: 1: tsc: not found` I thing we should install tsc not typescript in the docker file. – Sunil Sapkota Jul 07 '23 at 10:57
0

this code works for me hope this will work for you too.

FROM node:alpine
WORKDIR /usr/test
COPY . .
RUN yarn  && yarn add typescript tsc ts-node && yarn build
CMD ["node", "./dist/index.js"]
Sunil Sapkota
  • 918
  • 2
  • 11
  • 24