Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile typescript in Dockerfile

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?

like image 988
TietjeDK Avatar asked Jun 28 '18 12:06

TietjeDK


1 Answers

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"]
like image 147
Max Avatar answered Sep 30 '22 01:09

Max