-
-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: BuildArgs in custom docker builds #241
Comments
hi, @lorenzomigliorero I recently add support for enviroment variables in build time for Dockerfiles (Not released yet), is not args but it makes almost the same purpose to have variables in build time #227 |
Oh cool! I didn't see that issue. Is there a way to test the feature? |
Yes, you can install the canary version |
I ran the canary branch locally, but variables are unavailable at build time. |
@Siumauricio , I did a quick test, and I can confirm that the only way is to use the buildArgs flag. Creating the env file during the DockerFile creation isn't enough. const stream = await docker.buildImage(tarStream, {
t: image,
buildargs: {
foo: "bar" // this works
},
dockerfile: dockerFilePath.substring(dockerFilePath.lastIndexOf("/") + 1),
}); The easiest may be converting the env variable into a JSON: So, I suggest creating another field or flagging which variables are also build-time (as Coolify does). |
@lorenzomigliorero can you share your dockerfile? |
Sure, the NPM_TOKEN is needed as ARG for the FROM node:12-alpine as build
ARG NPM_TOKEN
ARG NODE_ENV=production
RUN env
WORKDIR /opt/
COPY package.json yarn.lock .npmrc ./
# Strapi requirements
RUN apk add --no-cache python2 make g++ vips-dev
RUN yarn --production
WORKDIR /opt/app
COPY . .
ENV PATH /opt/node_modules/.bin:$PATH
RUN yarn run build
FROM node:12-alpine
RUN apk add --no-cache vips-dev
ARG NPM_TOKEN
ENV NPM_TOKEN=$NPM_TOKEN
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY --from=build /opt/node_modules ./node_modules
ENV PATH /opt/node_modules/.bin:$PATH
WORKDIR /opt/app
COPY --from=build /opt/app .
EXPOSE 1337
CMD ["yarn", "start"] |
You have 2 options
FROM node:12-alpine as build
RUN env
WORKDIR /opt/
COPY package.json yarn.lock .npmrc ./
# Strapi requirements
RUN apk add --no-cache python2 make g++ vips-dev
RUN yarn --production
WORKDIR /opt/app
COPY . .
ENV PATH /opt/node_modules/.bin:$PATH
RUN yarn run build
FROM node:12-alpine
RUN apk add --no-cache vips-dev
WORKDIR /opt/
COPY --from=build /opt/node_modules ./node_modules
ENV PATH /opt/node_modules/.bin:$PATH
WORKDIR /opt/app
COPY --from=build /opt/app .
EXPOSE 1337
CMD ["yarn", "start"] |
@lorenzomigliorero I understand, wouldn't it be easier and better to pass the enviroment variables and convert them to an object and pass them directly to the ARGS? so we don't have to create a new UI or new fields? |
@Siumauricio It's easier for sure, but not better. Sharing all environment variables as build arguments in a Docker build command can have implications for both security and performance. We should share only the needed ones. I'd love to prepare a PR of the first screenshot above. I'm comfortable with the project stack, and I appreciate the project, so I want to contribute. What do you think? |
@lorenzomigliorero Thanks, if you have time would be awesome, use the second implementation you shared, and show only when we select the Dockerfile as |
Sure thing, I'll let you know soon! |
What problem will this feature address?
Currently, there's no way to specify build-args for custom dockerfile projects.
Example use-case: In case of a private npm registry, there's no way to declare an NPM_TOKEN build variable.
Describe the solution you'd like
I see three possible ways:
This is just pseudo-code, but the buildImage command should accept a buildargs parameter:
server/utils/builders/docker-file.ts
Dockerode issue: apocas/dockerode#326
Engine API docs: https://docs.docker.com/engine/api/v1.24/#build-image-from-a-dockerfile
Describe alternatives you've considered
There are no other alternatives since the buildargs argument is the only param available.
Additional context
No response
The text was updated successfully, but these errors were encountered: