nx-tools icon indicating copy to clipboard operation
nx-tools copied to clipboard

Docker context (directory) not set consistently?

Open sherland opened this issue 1 year ago • 6 comments

I'm new to Nx, so not sure if / how this is supposed to work, but here goes: I want to create a nginx base-container that will be used by some of the other containers in the monorepo.

I create a new folder "Applications\nginx", (where all the other applications are located) and added package.json and project.json. (Content at the end)

Then I add a nginx.conf with appropriate content, and created the Dockerfile with this content:

FROM docker.io/nginx:stable-alpine
COPY /nginx.conf /etc/nginx/nginx.conf

By coincidence, I was in the Applications\nginx directory. when I ran nx run nginx-base-image:container

This was working as it should. However then I navigated to the root of the project and ran nx run nginx-base-image:container again. Now the container could not be built. I had to change Dockerfile to this:

FROM docker.io/nginx:stable-alpine
COPY Applications/nginx/nginx.conf /etc/nginx/nginx.conf

Does this mean that the docker build context is just set by random to whatever directory i'm starting the build from?

BTW: I'm using Rancher Desktop, not Docker Desktop, and I get this to the console when building:

>  Buildx version 
[command]"C:\Program Files\Rancher Desktop\resources\resources\win32\bin\docker.exe" buildx version
github.com/docker/buildx v0.12.0 542e5d810e4a1a155684f5f3c5bd7e797632a12f
Unsupported CI Provider... Using Local Environment as fallback
[command]"C:\Program Files\Git\cmd\git.exe" log -1 --pretty=format:%ae
[email protected][command]"C:\Program Files\Git\cmd\git.exe" symbolic-ref HEAD
refs/heads/master
[command]"C:\Program Files\Git\cmd\git.exe" rev-parse HEAD
01ea355452c4bce35ce9916d50ce46d08d7a4b54

--- Other files package.json:

{
  "name": "nginx-base-image",
  "version": "1.0.0",
  "scripts": {},
  "author": "Team Triton",
  "dependencies": {},
  "devDependencies": {},
  "browser": {}
}

project.json:

{
  "name": "nginx-base-image",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "prefix": "",
  "targets": {
    "container": {
      "executor": "@nx-tools/nx-container:build",
      "dependsOn": [],
      "options": {
        "engine": "docker",
        "metadata": {
          "images": ["nginx-base-image"],
          "load": true,
          "tags": [
            "type=schedule",
            "type=ref,event=branch",
            "type=ref,event=tag",
            "type=ref,event=pr",
            "type=sha,prefix=sha-"
          ]
        }
      }
    }
  }
}

sherland avatar Mar 03 '24 13:03 sherland

Not sure if the same issue but as of NX 20.0.0 I cannot use this tool as it appears to be referencing the root DOCKERFILE to build apps which are normally pointing to their own app-level dockerfile.

patrickmichalina avatar Oct 17 '24 12:10 patrickmichalina

Any news on the issue in 20.0.0?

stefanellid-mms avatar Oct 30 '24 11:10 stefanellid-mms

Just to let people know if you manually set the context reference in the project.json you can overcome what was before a default to the project. It unblocks the migration to NX

patrickmichalina avatar Oct 30 '24 12:10 patrickmichalina

Just to let people know if you manually set the context reference in the project.json you can overcome what was before a default to the project. It unblocks the migration to NX

Can you maybe please show how the config would look like? We just upgraded to 20 and no matter what options we set we always get the error that the Dockerfile can't be found. Our Dockerfiles are all in each app's root directory and everything was working fine until the upgrade to 20.

Thanks

ThorstenKunz avatar Dec 13 '24 09:12 ThorstenKunz

We also ran into this problem after migrating to Nx 20. The Dockerfile could not be found because the plugin was looking for it in the workspace root instead of at the application level.

According to this example in the docs you can tell the plugin where to look for the Dockerfile with the "file" property. This worked for us:

"build:container": {
  "executor": "@nx-tools/nx-container:build",
  "options": {
    "file": "{projectRoot}/Dockerfile",

alpalla avatar Apr 01 '25 13:04 alpalla

Seeing the same behavior. It appears that the cwd key in options is not actually setting the current working directory to the application's directory.

This means that when executing the Dockerfile, it throws a file not found error for COPY operations. Adding the full app folder prefix for such operations resolves the issue.

avadhanij avatar Sep 24 '25 16:09 avadhanij