Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile how to make start command different between dev and prod?

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?

like image 721
Raz Buchnik Avatar asked Feb 25 '26 17:02

Raz Buchnik


1 Answers

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.
like image 68
Davide Madrisan Avatar answered Feb 28 '26 06:02

Davide Madrisan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!