As I far as I know,
theWORKDIR 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 and then just use the following file:package.json
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
Great start!
You can check out how I accomplish this in my GitHub Docker/NodeJS/Typescript starter project
As another answer noticed, you should only use one command, so instead of using that CMD, you should use CMD. That will compile your Typescript using the RUN sh -c tsc webApp.ts command-line tool that you previously installed on the container using tsc.RUN npm install -g typescript
After doing this and running your Dockerfile, you may expect to see in your working folder (the folder that hosts your Dockerfile) now, but you actually won't see it. The container sees it, but you don't. What's going on?webApp.js
When you run these commands from your Dockerfile, you're actually running them inside the container which lives in a special secluded part of your machine. That secluded part of your machine, and the part of your machine that hosts the Dockerfile, are two completely separate places. Therefore you won't see that generated file. .js
Thankfully, Docker has a way of "mounting" or mirroring files between these two parts of your machine. This is called a "volume".
There's a good StackOverflow answer on volumes. The volume syntax is like this from within your dockerfile: . The command is structured like this: ADD {my_filepath} {container_filepath}. It means that whatever is on your machine at {my_filepath} will now be "mounted" or mirrored into the container at the path {container_filepath}.ADD . /path/inside/docker/container
would mount your current working directory to the container's main working directory.ADD . .
You can use a multi-stage build for this. The first stage includes all of the development dependencies, including ; the second stage only includes the files that are needed to run the built application.tsc
(I'm not familiar with the specific build environment you're using so this will be in terms of the standard image.)node
# First stage: compile things.
FROM node:12 AS build
WORKDIR /usr/src/app
# (Install OS dependencies; include -dev packages if needed.)
# Install the Javascript dependencies, including all devDependencies.
COPY package.json .
RUN npm install
# Copy the rest of the application in and build it.
COPY . .
# RUN npm build
RUN npx tsc -p ./tsconfig.json
# Now /usr/src/app/dist has the built files.
# Second stage: run things.
FROM node:12
WORKDIR /usr/src/app
# (Install OS dependencies; just libraries.)
# Install the Javascript dependencies, only runtime libraries.
COPY package.json .
RUN npm install --production
# Copy the dist tree from the first stage.
COPY --from=build /usr/src/app/dist dist
# Run the built application when the container starts.
EXPOSE 3000
CMD ["npm", "run", "serve"]