1

I want to setup a CD pipeline for a typescript/node API. I am using a Dockerfile to upload images to AWS ECR manually and it works as expected.

FROM node:11-slim
EXPOSE 8080
WORKDIR /usr/src/app
RUN mkdir /usr/src/app/src
COPY  ./node_modules node_modules
COPY  ./dist dist
COPY  ./package.json package.json
ENV PORT=8080
USER node
CMD ["node", "./dist/app.js"]

To upload images to AWS ECR I am using the following Dockerfile:

FROM node:11-slim
EXPOSE 8080
WORKDIR /usr/src/app
RUN mkdir /usr/src/app/src
COPY  ./package.json package.json
RUN npm add -g typescript
RUN npm install tsc -g
RUN npm install
RUN sh -c tsc -p .
ENV PORT=8080
USER node
CMD ["node", "./dist/app.js"]

I use the RUN sh -c tsc -p . command to build my project. Even if the build succeeds, it does not contain the dist folder which contains the built project in the image.

Here is an excerpt from my projects package.json file:

"scripts": {
    "start": "node dist/app.js",
    "install": "yarn install",
    "postinstall": "tsc -p .",
    "watch": "tsc -w -p .",
    "debug": "nodemon --watch ./dist --inspect=0.0.0.0:9222 --nolazy ./dist/app.js",
    "docker-debug": "sudo tsc  && docker-compose up --build"
},

When I use RUN npm run postinstall, the compiler errors: missing script postinstall.

1 Answer 1

5

The only source file that's in your image is the package.json file; there's nothing there for tsc to build. You need to COPY in the rest of your application source before you RUN tsc.

A typical Dockerfile here would look more like:

FROM node:11-slim
WORKDIR /usr/src/app

# Also copy the lock file
COPY ./package.json ./package.lock .

# typescript is a devDependencies, no need to separately install it
RUN npm install

# Copy the rest of your application in
# (include `node_modules` in a `.dockerignore` file)
COPY ./ ./

# Now build it (Docker supplies a `sh -c` wrapper for you)
RUN tsc -p .

# Runtime metadata as above
ENV PORT=8080
EXPOSE 8080
USER node
CMD ["node", "./dist/app.js"]
Sign up to request clarification or add additional context in comments.

1 Comment

Is RUN tsc -p . preferable to tsc --build ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.