Proxy network for container inside container

I have a VM that’s hosted on one of our company servers, this VM requires our corporate proxy to access the internet and the proxy is configured system-wide. We use this VM to run a job that builds a docker image and pushes it to the cloud.

For the build job, we use docker sock because our builder ( from HashiCorp) runs inside a docker container and this builder will spin up another container inside the parent container (running on VM).

So, the overview of structure is like this:
VM on corporate server using corporate proxy > Parent container on VM used to run packer build > Nested container in parent container created by Packer

=> When we use the parent container inside this VM, the container is not able to access the internet. However, if I set the proxy config in ‘~/.docker/config.json’ the container is able to access the internet.
=> But the issue now is that, again the nested container inside this current container (created by Packer) is not able to access the internet.
=> We cannot pass the proxy as ENV to the nested container for other reasons. (if we do it, the proxy env vars gets hard coded into the final image to be pushed, this is not desired)

Is there a way to route all traffic from inside the parent container through the outer proxy by default? So that whatever is trying to access the internet from inside the parent container (including the nested container) will use the VM’s proxy by default?

You can pass http_proxy and https_proxy in as a build-arg and use it as ARG instead of ENV. ARGs are ephemeral and will not end in the created image.