cli icon indicating copy to clipboard operation
cli copied to clipboard

Support add extra options to `docker build` command.

Open link89 opened this issue 1 year ago • 3 comments

For some reason I have to add the following options to the docker build command:

--add-host=host.docker.internal:host-gateway --build-arg HTTPS_PROXY=http://host.docker.internal:1080

I find from this doc that I can use build.args to specific build-arg and here is my configuration

{
  "image": "mcr.microsoft.com/vscode/devcontainers/base:ubuntu",
  "build": {
    "args": {
      "http_proxy": "http://172.17.0.1:1080",
      "https_proxy": "http://172.17.0.1:1080"
    }
  },
  "appPort": 8080,
  "features": {
    "ghcr.dockerproxy.com/devcontainers/features/go:1": {
      "version": "1.21"
    },
    "ghcr.dockerproxy.com/devcontainers/features/node:1": {
      "version": "20"
    },
    "ghcr.dockerproxy.com/devcontainers/features/docker-in-docker:2": {},
    "ghcr.dockerproxy.com/devcontainers/features/python:1": {}
  },
  "forwardPorts": [9000, 9090, 2746, 8080],
  "hostRequirements": {
    "cpus": 4
  },
  "runArgs": [
    "--add-host=host.docker.internal:host-gateway",
    "--add-host=dex:127.0.0.1",
    "--add-host=minio:127.0.0.1",
    "--add-host=postgres:127.0.0.1",
    "--add-host=mysql:127.0.0.1",
    "--add-host=azurite:127.0.0.1"
  ],
  "postCreateCommand": ".devcontainer/pre-build.sh",
  "workspaceMount": "source=${localWorkspaceFolder},target=/home/vscode/go/src/github.com/argoproj/argo-workflows,type=bind",
  "workspaceFolder": "/home/vscode/go/src/github.com/argoproj/argo-workflows",
  "remoteEnv": {
    "PATH": "${containerEnv:PATH}:/home/vscode/go/bin",
    "GOPATH": "/home/vscode/go"
  }
}

But the actual command running is

 docker buildx build --load --build-context dev_containers_feature_content_source=/tmp/devcontainercli-chenglab/container-features/0.56.1-1707032528485 --build-arg _DEV_CONTAINERS_BASE_IMAGE=mcr.microsoft.com/vscode/devcontainers/base:ubuntu --build-arg _DEV_CONTAINERS_IMAGE_USER=root --build-arg _DEV_CONTAINERS_FEATURE_CONTENT_SOURCE=dev_container_feature_content_temp --target dev_containers_target_stage -t vsc-argo-workflows-main-786eedc0416670a1d14a7d9eff7e152b3e6c365adf4daac9c51d4fc9614302e7-features -f /tmp/devcontainercli-chenglab/container-features/0.56.1-1707032528485/Dockerfile.extended /tmp/devcontainercli-chenglab/empty-folder

It seems doesn't take effect. Is there any suggestion for solving this issue?

link89 avatar Feb 04 '24 07:02 link89

hi 👋

According to the docs 👇 , one can use build.args to pass arguments to the Dockerfile. In your case, the devcontainer does not have a dockerfile (but an image). Hence, it does not get applied.

Image

👇 works for me

{
	"build": {
		"dockerfile": "Dockerfile",
		"args": {
			"HTTP_PROXY": "test"
		}
	}
}

docker buildx build --load --build-arg BUILDKIT_INLINE_CACHE=1 -f /tmp/devcontainercli-root/container-features/0.56.1-1707172992374/Dockerfile-with-features -t vsc-user-repro-b7bdfb2c19fb573882d56359a1af21ec7cb1e8cb37388f1f4160434a54517ecc --target dev_containers_target_stage --build-arg HTTP_PROXY=test --build-arg _DEV_CONTAINERS_BASE_IMAGE=dev_container_auto_added_stage_label /var/lib/docker/codespacemount/workspace/user-repro/.devcontainer

samruddhikhandale avatar Feb 05 '24 22:02 samruddhikhandale

@samruddhikhandale That works for the --build-arg HTTP_PROXY= part, but --add-host=host.docker.internal:host-gateway also needs to be passed. Sometimes it's required to pass additional flags to docker build/docker buildx build in order to build an image, and that's what I believe @link89 is asking for.

trxcllnt avatar Feb 06 '24 00:02 trxcllnt

@link89 I just discovered the build.options field in devcontainer.json. It doesn't seem to be documented on containers.dev, I found it by accidentally hitting <ctrl><space> in VSCode while making an unrelated edit.

I can confirm build.options are passed to the initial docker buildx build (but not to the subsequent docker build call that updates the container's UID).

image

trxcllnt avatar Feb 09 '24 10:02 trxcllnt