caprover / caprover

Scalable PaaS (automated Docker+nginx) - aka Heroku on Steroids
https://CapRover.com
Other
13.17k stars 843 forks source link

Setting environment variables doesn't add them during the building of an image #343

Closed Bensji closed 5 years ago

Bensji commented 5 years ago

Hi,

Normally you would have to add the -e parameter to docker build (or add Args) but yo can't do '-e' this with the captain-definition file. How would one add the environment variables to the build process of the container?

Example: While deploying a Symfony application I want to do composer install, and the post-composer script wants to build my parameters.yml based upon the env variables.

Thanks!

githubsaturn commented 5 years ago

captain-definition is being used in "docker build" step, not "docker update".

In general, environmental variables are either:

 {
  "schemaVersion" :1 ,
  "dockerfileLines" :[
                        "FROM node:8.7.0-alpine",
                        "RUN mkdir -p /usr/src/app",
                        "WORKDIR /usr/src/app",
                        "COPY ./src/package.json /usr/src/app/",
                        "RUN npm install && npm cache clean --force",
                        "COPY ./src /usr/src/app",
                        "ENV NODE_ENV production",
                        "ENV PORT 80",
                        "EXPOSE 80",
                        "CMD [ \"npm\", \"start\" ]"
                    ]
 }

Look how ENV is being used as part of the build.

stoutlabs commented 5 years ago

(Note: I can open a new issue on this if needed... but my search brought me here first, and I don't want to clog up your amazing repo with a similar issue.)

I'm having the same issue as the OP, although I'm building/exporting a SSR NextJS site. I'm using a 2 phase build in my Dockerfile - a 'build & export' phase, then a standard nginx image to host the static files output from phase 1. I fully understand that you can set env vars at build as shown above... as I'm super familiar with Docker. However, in my build phase, I need to access the values for variables set via the web UI... but they don't seem to be available?

Any ideas/help to remedy that would be really appreciated! 😅

JacobSamro commented 5 years ago

I am also facing the same problem. I was not able to access the environment variables in the build process. Is it possible to pass the variables a build arguments ?

githubsaturn commented 5 years ago

UPDATE: As of 1.6.0, we'll add environmental variables as build args to the build time as it turned out to be a common practice.

Adding environmental variables to build process is a no. As this is not even supported by Docker REST API. BuildArgs, on the other hand, is supported by Docker, but not ported to CapRover due to its very special use cases and the CapRover design philosophy to "avoid" blindly exporting everything from Docker to keep the interface simple. Anyways, build arg is usually used for the situation where there is a master Dockerfiles and you want to create multiple variants of this image. In CapRover though, there is one Dockerfile (captain-definition) specific to each app. Hence this use case should not exist. Anytime you rely on passing env vars and build args to the build process, you can simply edit your Dockerfile to have the env values and build args embedded in it.

I'll be happy to hear your use cases and discuss the best solution to resolve them.

douglasjunior commented 4 years ago

Is there an easy way to map all ARG to ENV, without having to write one by one?

Today I have to do:

ARG REACT_APP_URL_API
ARG REACT_APP_URL_STORAGE
ARG REACT_APP_URL_IMAGE
ARG REACT_APP_URL_UPLOAD
ARG GENERATE_SOURCEMAP

ENV REACT_APP_URL_API=$REACT_APP_URL_API
ENV REACT_APP_URL_STORAGE=$REACT_APP_URL_STORAGE
ENV REACT_APP_URL_IMAGE=$REACT_APP_URL_IMAGE
ENV REACT_APP_URL_UPLOAD=$REACT_APP_URL_UPLOAD
ENV GENERATE_SOURCEMAP=$GENERATE_SOURCEMAP

It is a static website compiled with create-react-app.

Perni1984 commented 3 years ago

@douglasjunior I researched aswell for a way to get a list of arguments inside the dockerfile, but unfortunately could not find any solution for it - for me it seems to be by design to not have a command in to get a list of build-args inside the Dockerfile.

@githubsaturn I really like that you add environmental variable as build args, but I think it should be stated in the documentation that any secrets passed into the docker container in this way may be leaked by the docker history command, if I interpret the warning at https://docs.docker.com/engine/reference/builder/#arg correctly.

githubsaturn commented 3 years ago

@Perni1984 - feel free to add it to the docs. In general, if you find something is missing in the docs, or it can be improved, please edit and add it to the docs. There is an edit button at the top right. image

graphiostudio commented 1 year ago

too bad caprover doesn't have a simple checkbox next to the env variable like "make this available during build time". Although caprover is considered "a complete project" which is why it is not updated as devs say, there are so many little things they could improve on.

Well, Coolify does this, and then some... I wish the devs could continue on this project. It's sad that I'll move on after so many years of caprover awesomeness :(

githubsaturn commented 1 year ago

@graphiostudio - what made you think CapRover doesn't have this option? All env vars are available at build time! It's clearly mentioned in the docs:

image

graphiostudio commented 1 year ago

too bad caprover doesn't have a simple checkbox next to the env variable like "make this available during build time".

Perhaps if you read my post properly without skipping you would understand better...