Is there a way to pass arguments to the docker bui...
# general
w
Is there a way to pass arguments to the docker build command during a
docker_image
target invocation? Context is I need to pass networking mode and a host override to use an existing ssh tunnel. I see
run_args
and
build_args
but I don’t think those work for this
c
unfortunately no way to pass arbitrary command line args for
docker build
out of the box. If you’re adventurous you could create a
docker
shim script and make pants pick that up forwarding your build request with any additional args as required.
w
k i guess that’ll hve to do for now
I imagine this is to make a shim, and use
executable_search_paths
to find it?
althgouh that seems to look for the actual binary? Trying to get a shim to a bash script does’nt seem to work
c
huh.. a
+x
executable script named
docker
on path ought to tick all the boxes we care about…
w
kk I’m probably not specifying it correctly.
c
I’ll have to try it out otherwise to be able to give clearer suggestions 😁
w
Copy code
$ ls -l /Users/nasron/gitroot/edge/pants-docker-shim/
total 16
-rwxr-xr-x  1 nasron  staff  128 30 Mar 17:20 docker
-rw-r--r--  1 nasron  staff  159 30 Mar 17:20 docker~
nasron aws:root py:3.9.10 (set by /Users/nasron/.pyenv/version) tf:tf:disabled ~/gitroot/edge on feature/EDGE-1519-fixing-pants-fl-server-client-docker
$ tail pants.toml
lockfile='pytest.lock'

[test]
use_coverage = true

[docker]
build_verbose = true
executable_search_paths = [
   '/Users/nasron/gitroot/edge/pants-docker-shim/',
]%
gives
Copy code
BinaryNotFoundError: Cannot find `docker` on `['/Users/nasron/gitroot/edge/pants-docker-shim/']`. Please ensure that it is installed so that Pants can interact with the docker daemon.
👀 1
c
what’s in your shim file, as that is the only thing I can think of that might be different. (i.e. works for me)
Copy code
pants.engine.process.ProcessExecutionFailure: Process 'Building docker image test-example:1.2.5' failed with exit code 1.
stdout:
DOCKER shim: build --pull=False --tag test-example:1.2.5 --file testprojects/src/python/docker/Dockerfile.example .

stderr:
error: open .docker/buildx/instances/colima: no such file or directory
with:
Copy code
#!/bin/bash

echo "DOCKER shim: $@"
exec /opt/homebrew/bin/docker "$@"
fails on subsequent step as I don’t have the rest properly setup…
w
Ah I'm not using exec let me try that
An update on this, I ended up working around the actual tunnel, but I'm probably delaying the inevitable and may need to try the shim down the line.
n
I had a similar issue - wondered if it was hyphens in the file path, so moved the shim to a different and it worked. But then it worked when I moved it back to the original location. So suspect something with caching? Running with
--no-pantsd
might help?
Actually I think in the end it may have been that when the interpolation of options is incorrect, the 'BinaryNotFoundError' is raised, which is a bit misleading. In the end I came up with
Copy code
#!/bin/bash
echo "Using Docker shim with args: $@"

if [ "$1" == "build" ]; then
  echo "Running build with '--network=host'"
  shift
  # Needed for GHA on EKS to set network mode.
  exec /usr/bin/docker build --network=host "$@"
  exit 0
fi

exec /usr/bin/docker "$@"
👍 2
w
Hi! So coming back to this - is there a pants built in feature to pass args to the build command? Otherwise looks like I may have to try this shim as suggested.
n
For networking and hosts, support has been added directly now (I think since 2.18). See https://www.pantsbuild.org/v2.19/docs/reference-docker_image#codebuild_networkcode
👍 1
w
Ok, so I got this to work with depot.dev in a rough way. Is there a way to provide a relative path for https://www.pantsbuild.org/2.19/reference/subsystems/docker#executable_search_paths? Or at least variables that can interpolate in the entries.
👍 1
n
I've not needed to set that - what are you hoping to achieve?
w
I’d like the shim to be part of the repo, so setting it relative to the repo location would be good.
right now I have a pre step to copy it to /tmp which is a bit janky
Another note that others may not have noticed - pants seems to test the docker binary with
docker -v
. If the binary doesn’t exit cleanly it appears as “no executable found” error. So I had to fake it in the shim for
depot
.
Putting in my shim script here in case it’s useful to others. I’ve actually moved to build.docker.com since the integration is much simpler, so I’ve stopped looking at depot.dev for now. This script uses env vars to change the expected mode when doing a build, so it can be tricky to use.
👍 1