Here is my build command
docker build --build-arg APP_PROJ=proj1 --build-arg APP_ENV=dev -t proj1 .
Giving two argument,APP_ENV and APP_PROJ.And here is my Dockerfile content
FROM node:14.15 AS build
ARG APP_ENV
ARG APP_PROJ
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm cache clean --force
RUN npm i
COPY . .
RUN npm run build:${APP_PROJ}:${APP_ENV}
CMD [ "node", "dist/${APP_PROJ}/server/main.js" ]
I want to add an argument which can justify which project is building,but when I run docker image ,it returned an error
Error: Cannot find module '/usr/src/app/dist/${APP_PROJ}/server/main.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47{
code: 'MODULE_NOT_FOUND',
requireStack: []
}
- And I have checked my dist whether the main.js is exsist
It looks like dockerFile can't read enviroment variable APP_PROJ.How can I pass an argument to let docker read the correct main.js?
Appreciate help ~
CodePudding user response:
It looks like you mixed up "ARGuments" and "ENVironments'.
This doc is helping understanding the difference. Mainly, it is about that:
ARG is only available during the build of a Docker image (RUN etc), not after the image is created and containers are started from it (ENTRYPOINT, CMD)
Thus, you will have to use ENV variables to set them up for your CMD to know about them....
Actually, you can set ENV from ARG at build time following this documentation (same website). This is the common (not intuitive) way.
The alternative is hardcoding them in the Dockerfile (and you could use a "sed" command previous to your build to set up your DOckerfile according to your project)
Also : this may not work because variable substitution is not used when you use CMD as a list of arguments. You may have to switch to raw CMD (CMD dosomething "$myvar ${anotherVar}") or provide a shell as first argument (CMD ["sh","-c","mycommands","etc"])
