Skip to content
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

nginx buildpack overwrite the existing nginx.conf file #667

Open
dmitry-mightydevops opened this issue Jan 26, 2024 · 5 comments
Open

nginx buildpack overwrite the existing nginx.conf file #667

dmitry-mightydevops opened this issue Jan 26, 2024 · 5 comments

Comments

@dmitry-mightydevops
Copy link

nginx buildpack rewrites nginx.conf file, confirmed with

✦ ➜ ls -la                                                                 
total 500K
drwxr-xr-x. 1 dmitry dmitry  508 Jan 25 21:13 .
drwxrwxr-x. 1 dmitry dmitry  284 Jan 25 15:49 ..
drwxr-xr-x. 1 dmitry dmitry  186 Jan 25 21:11 .git
drwxr-xr-x. 1 dmitry dmitry   38 Jan 25 15:49 .github
drwxr-xr-x. 1 dmitry dmitry   40 Jan 25 19:01 .tekton
drwxr-xr-x. 1 dmitry dmitry   74 Jan 25 18:09 build
drwxr-xr-x. 1 dmitry dmitry  15K Jan 25 16:49 node_modules
drwxr-xr-x. 1 dmitry dmitry   42 Jan 25 15:49 public
drwxr-xr-x. 1 dmitry dmitry  246 Jan 25 15:49 src
-rw-r--r--. 1 dmitry dmitry  290 Jan 25 15:49 .editorconfig
-rw-r--r--. 1 dmitry dmitry   62 Jan 25 15:49 .env.development
-rw-r--r--. 1 dmitry dmitry 5.1K Jan 25 15:49 .eslintrc
-rw-r--r--. 1 dmitry dmitry  323 Jan 25 15:49 .gitignore
-rw-r--r--. 1 dmitry dmitry   98 Jan 25 15:49 .stylelintrc.json
-rw-r--r--. 1 dmitry dmitry  195 Jan 25 15:49 .swcrc
-rw-r--r--. 1 dmitry dmitry 1.4K Jan 25 15:49 README.md
-rw-rw-r--. 1 dmitry dmitry 5.0K Jan 25 21:06 a.conf
-rw-r--r--. 1 dmitry dmitry  411 Jan 25 15:49 index.html
-rw-r--r--. 1 dmitry dmitry  773 Jan 25 15:49 jest.config.ts
-rw-r--r--. 1 dmitry dmitry  427 Jan 25 15:49 jestSetup.ts
-rw-r--r--. 1 dmitry dmitry 2.0K Jan 25 21:05 mime.types
-rw-r--r--. 1 dmitry dmitry 2.3K Jan 25 21:05 nginx.conf
-rw-r--r--. 1 dmitry dmitry 423K Jan 25 16:58 package-lock.json
-rw-r--r--. 1 dmitry dmitry 2.5K Jan 25 16:47 package.json
-rw-r--r--. 1 dmitry dmitry  371 Jan 25 16:58 project.toml
-rw-r--r--. 1 dmitry dmitry  757 Jan 25 15:49 tsconfig.json
-rw-r--r--. 1 dmitry dmitry  468 Jan 25 15:49 vite.config.ts


➜ pack build app --builder paketobuildpacks/builder-jammy-base:latest
...sh
[builder]     Completed in 19.606s
[builder] 
[builder] Paketo Buildpack for Nginx Server 0.15.6
[builder]   Resolving Nginx Server version
[builder]     Candidate version sources (in priority order):
[builder]       buildpack.toml -> "1.25.*"
[builder] 
[builder]     Selected Nginx Server version (using buildpack.toml): 1.25.2
[builder] 
[builder]   Generating /workspace/nginx.conf
[builder]     Setting server root directory to '{{ env "APP_ROOT" }}/build'
[builder]     Setting server location path to '/'
[builder]     Enabling push state routing
[builder] 
[builder]   Reusing cached layer /layers/paketo-buildpacks_nginx/nginx
[builder] 
===> EXPORTING
[exporter] Reusing layer 'paketo-buildpacks/ca-certificates:helper'
[exporter] Reusing layer 'paketo-buildpacks/nginx:nginx'
[exporter] Reusing layer 'buildpacksio/lifecycle:launch.sbom'
[exporter] Adding 1/1 app layer(s)
[exporter] Reusing layer 'buildpacksio/lifecycle:launcher'
[exporter] Reusing layer 'buildpacksio/lifecycle:config'
[exporter] Reusing layer 'buildpacksio/lifecycle:process-types'
[exporter] Adding label 'io.buildpacks.lifecycle.metadata'
[exporter] Adding label 'io.buildpacks.build.metadata'
[exporter] Adding label 'io.buildpacks.project.metadata'
[exporter] Setting default process type 'web'
[exporter] Saving app...
[exporter] *** Images (146b0d67292e):
[exporter]       app
[exporter] Reusing cache layer 'paketo-buildpacks/node-engine:node'
[exporter] Reusing cache layer 'paketo-buildpacks/npm-install:build-modules'
[exporter] Reusing cache layer 'paketo-buildpacks/npm-install:npm-cache'
[exporter] Reusing cache layer 'buildpacksio/lifecycle:cache.sbom'
Successfully built image app

as well as in tekton

/cnb/lifecycle/creator \
          -app=${source_subpath} \
          -cache-dir=${cache_path} \
          -cache-image=${cache_image} \
          -uid=${user_id} \
          -gid=${group_id} \
          -layers=/layers \
          -platform=/platform \
          -report=/layers/report.toml \
          -process-type=${process_type} \
          -skip-restore=${skip_restore} \
          -previous-image=${app_image} \
          -run-image=${runner} \
          ${app_image}

...

[build-buildpack : build] Paketo Buildpack for Nginx Server 0.15.6
[build-buildpack : build]   Resolving Nginx Server version
[build-buildpack : build]     Candidate version sources (in priority order):
[build-buildpack : build]       buildpack.toml -> "1.25.*"
[build-buildpack : build] 
[build-buildpack : build]     Selected Nginx Server version (using buildpack.toml): 1.25.2
[build-buildpack : build] 
[build-buildpack : build]   Generating /workspace/source/app/nginx.conf
[build-buildpack : build]     Setting server root directory to '{{ env "APP_ROOT" }}/build'
[build-buildpack : build]     Setting server location path to '/'
[build-buildpack : build]     Enabling push state routing
[build-buildpack : build] 
[build-buildpack : build]   Executing build process
[build-buildpack : build]     Installing Nginx Server 1.25.2
[build-buildpack : build]       Completed in 234ms
[build-buildpack : build] 
[build-buildpack : build]   Configuring build environment
[build-buildpack : build]     PATH -> "$PATH:/layers/paketo-buildpacks_nginx/nginx/sbin"
[build-buildpack : build] 
[build-buildpack : build]   Configuring launch environment
[build-buildpack : build]     APP_ROOT   -> "/workspace/source/app"
[build-buildpack : build]     EXECD_CONF -> "/workspace/source/app/nginx.conf"
[build-buildpack : build]     PATH       -> "$PATH:/layers/paketo-buildpacks_nginx/nginx/sbin"
[build-buildpack : build]     PORT       -> "8080"
[build-buildpack : build] 
[build-buildpack : build]   Assigning launch processes:
[build-buildpack : build]     web (default): nginx -p /workspace/source/app -c /workspace/source/app/nginx.conf -g pid /tmp/nginx.pid;
[build-buildpack : build] 
[build-buildpack : build]   Generating SBOM for /layers/paketo-buildpacks_nginx/nginx
[build-buildpack : build]       Completed in 0s
[build-buildpack : build] 

content of the nginx.conf in the container is 5.1kb, vs 2.3kb in the repo.

✦ ✗  docker exec -ti app bash                                                                
cnb@ccb7ee566e67:/workspace$ ls -la nginx.conf 
-rw-rw-r--. 1 1001 cnb 5102 Jan 26 03:50 nginx.conf
cnb@ccb7ee566e67:/workspace$ 

Expected Behavior

nginx.conf to be reused

Current Behavior

nginx.conf is replaced by the buildpack entirely

Possible Solution

N/A

Steps to Reproduce

project.toml:

✦ ➜ cat project.toml                                                        
exclude = [
    "/README.md"
]

  [[build.buildpacks]]
  uri = "paketo-buildpacks/web-servers"

  [[ build.env ]]
    name="BP_NODE_RUN_SCRIPTS"
    value="build"

  [[ build.env ]]
    name="BP_WEB_SERVER"
    value="nginx"

  [[ build.env ]]
    name="BP_WEB_SERVER_ROOT"
    value="build"

  [[ build.env ]]
    name="BP_WEB_SERVER_ENABLE_PUSH_STATE"
    value="true"

place a nginx.conf in the roof of any repo and run

pack build app:new --builder paketobuildpacks/builder-jammy-base:latest
@colmbrady
Copy link

I noticed that if you remove:

[[ build.env ]]
   name="BP_WEB_SERVER"
   value="nginx"

add add this:

[[ build.env ]]
   name="BP_NGINX_CONF_LOCATION"
   value="./nginx.conf"

It will now use your nginx.conf file, however it fails to inject in the environment variables etc, so your file can not refer to them and must be a static file.

Im not sure if this ever worked?

@colmbrady
Copy link

Incidentally, the error I have is:

2024/02/29 03:06:23 [emerg] 1#0: invalid number of arguments in "location" directive in /workspace/nginx.conf:334

and the config I have at that line is:

location {{env "BP_WEB_SERVER_LOCATION_PATH"}} {

Seems that its not correctly substituting the environment variable, after running the container with this command:

docker run --tty --env PORT=19080 --env BP_WEB_SERVER_LOCATION_PATH=/ --publish 19080:19080 my-nginx-ap

@ajdergute
Copy link

We face the exact same problem. I'm curios how this ever worked, but it did. Unfortunatly i didn't found a workaround

@pr11me
Copy link

pr11me commented Oct 16, 2024

I encountered the failing env substitution today. I am using the web-servers build pack with nginx option for an Angular app. The generated nginx.conf contains {{ env "APP_ROOT" }}/dist/<app-name> as server root, where dist/... is the given value of BP_WEB_SERVER_ROOT. Passing the var to docker like @colmbrady leads to the same error.
I wonder if the resolution of {{ port }} works. According to the build pack doc, the {{ env "FOO" }} syntax is the generalized version of the former.
I've read somewhere that the buildpack launch command essentially runs envsubst, so maybe the entrypoint of the produced image is wrong and does not include this command (now).

@pr11me
Copy link

pr11me commented Oct 17, 2024

I encountered the failing env substitution today. I am using the web-servers build pack with nginx option for an Angular app. The generated nginx.conf contains {{ env "APP_ROOT" }}/dist/<app-name> as server root, where dist/... is the given value of BP_WEB_SERVER_ROOT. Passing the var to docker like @colmbrady leads to the same error. I wonder if the resolution of {{ port }} works. According to the build pack doc, the {{ env "FOO" }} syntax is the generalized version of the former. I've read somewhere that the buildpack launch command essentially runs envsubst, so maybe the entrypoint of the produced image is wrong and does not include this command (now).

I do not know what has changed but the same docker command as yesterday suddenly works, i.e. the APP_ROOT variable is being interpolated. So I list here my steps in case it helps anyone:

tasks.register<Exec>("buildImage") {
  group = "build"
  description =
      """
        Builds frontend application image using pack CLI with NodeJs and nginx buildpacks
        (https://paketo.io/docs/howto/web-servers/#build-and-serve-a-frontend-framework-app).
        """

  executable = "pack"
  args(
      "build",
      "my-angular-app",
      "--buildpack",
      "paketo-buildpacks/[email protected]",
      "--builder",
      "paketobuildpacks/builder-jammy-base",
      "--env-file",
      "buildpack.env",
      "--verbose"
  )

('tis a Gradle task executing pack CLI)

BP_NPM_VERSION=10.9.0
BP_NODE_RUN_SCRIPTS=build:pack
BP_WEB_SERVER_ROOT=dist/my-angular-app
BP_WEB_SERVER_ENABLE_PUSH_STATE=true
BP_WEB_SERVER=nginx

docker run -it --env PORT=4200 --env APP_ROOT=/workspace -p 4200:4200 my-angular-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants