Home > Enterprise >  Dockerfile CMD does not interpolate ENV
Dockerfile CMD does not interpolate ENV

Time:06-20

I have tried every combination I can think of.

docker-compose.yml

version: "3.1"
services:
  node-server:
    build: 
      context: .
      dockerfile: Dockerfile
      args:
        - MYVAR=${MYVAR}
    environment:
      - ENV_MYVAR=${MYVAR}
    ports:
      - 8000:8000
    env_file:
      - .env
    # command: ["npm", "run", "_${MYVAR}_"] This works, but for the sake of testing CMD in Dockerfile, I can't get that to work

Dockerfile

FROM node:14
ARG MYVAR

ENV ENV_MYVAR="${MYVAR}"

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE ${port}

RUN echo "1"
RUN echo $MYVAR # Works

CMD [ "npm", "run", "_${ENV_MYVAR}_"] # OUTPUTS as _${ENV_MYVAR}_". Does not interpolate!!

.env

MYVAR=boot

package.json

{
  "name": "node-server-ts",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": {
    "test": "echo 'test'",
    "boot":  "echo 'boot'",
    "_boot_":  "echo '_boot_'"
  }
}

terminal

docker-compose up

Expect:

npm run _boot_

error - node-server_1 | ${ENV_MYVAR}

CodePudding user response:

Try removing the square brackets.

CMD npm run "_${ENV_MYVAR}_"

See this StackOverflow question about the differences

  • Related