3

I have a TypeScript Node app. I have a dev and start npm scritps:

"dev": "ts-node-dev src/index.ts",
"build": "npm run test:ci && tsc",
"start": "node dist/index"

When developing I watch changes on the .ts files and when running in production I want to run the .js files from the dist dir (which is generated using the npm build script).

This is my Dockerfile:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./ 

RUN npm i --only=prod

COPY . . 

CMD ["npm", "run", "dev"] 

When its running on dev env its good, but on production the CMD command should be like that:

CMD ["npm", "start"]

Also the RUN npm i --only-prod command also needs to be changed respectively.

How to make it adjustable to dev vs prod?

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96
  • `docker run ... npm start`? – super Oct 03 '21 at 13:19
  • @super what do you mean by that? – Raz Buchnik Oct 03 '21 at 13:22
  • When you run the container, you can override the command it should run by simply supplying it on the command line when you start the container. – super Oct 03 '21 at 13:41
  • I am using kubernetes which runs the default command on the image, not manually via the cli. – Raz Buchnik Oct 03 '21 at 14:10
  • Does this answer your question? [Dockerfile if else condition with external arguments](https://stackoverflow.com/questions/43654656/dockerfile-if-else-condition-with-external-arguments) – derpirscher Oct 03 '21 at 14:10
  • @Raz Kubernetes can be configured to do whatever you want. If you are using a specific setup and need help with that you should include that in your question. Maybe you should take a refresher of the [tour] and [ask], plus how to make a [mre]. – super Oct 03 '21 at 15:52
  • @derpirscher not sure. this is not a solution, but a "wire end" – Raz Buchnik Oct 05 '21 at 05:21

1 Answers1

1

In kubernetes you can overwrite the default command args:

apiVersion: apps/v1
kind: Deployment
[...]
spec:
  template:
    spec:
      containers:
      - name: CONTAINER-NAME
        image: IMAGE-NAME
        args: [
          "npm",
          "start" ]

See the kubernetes documentation.

The detailed implementation depend on the deployment system you're using:

  • You can write two different .yaml files, one for the development and one for the production environment.
  • If you're deploying with helm, you can set this configuration in a value file per environment.
  • You can also use Kustomize as described in this example.
Davide Madrisan
  • 1,969
  • 2
  • 14
  • 22
  • They talk about arguments and env variables that can use to maybe solve the issue I asked about, but neither your answer or thier docs answer directly my question. I want to adjust the start command from dev to prod. How can it be done? you gave only one command which is not conditional. – Raz Buchnik Oct 05 '21 at 05:17
  • I've listed some possible options. – Davide Madrisan Oct 05 '21 at 07:37