I have a Typescript node application. The main file is server.ts. I want to run this application in a Docker container. I have a build step in my Dockerfile that runs the Typescript transpilation, example:
FROM node:9.9 as build
WORKDIR /app
COPY package*.json ./
COPY ... # copy the source files to the docker container
RUN npm install && \
npm run build # <-- runs Typescript transpile
FROM node:9.9
COPY --from=build /path/to/source/files /app
WORKDIR /path/to/server.js directory
CMD ["node", "server.js"]
I use docker-compose to configure the container for development
my-app:
build:
context: /path/to/build/context
dockerfile: Dockerfile
command: bash -c "npm install -g nodemon && nodemon server.js"
network_mode: "host"
volumes:
- /path/to/server.js/directory/local:/path/to/server.js/directory/in/container
Since the Typescript is transpiled during the image build phase, the server.js file is created within the container. So if I run docker-compose without the volumes included, the server.js file is there and the container starts fine. If I run docker-compose with the volumes, the server.js file is not there, just server.ts. I think this is because locally, I don't have server.js since it is built during the image build phase, and since the volume is referencing that directory, it is not there in the container. Is there any way that I could build the container and have the transpiled js files from the build phase in that volume?