Mounting the Docker socket into the controller image is a mistake for two reasons.
- Running Jenkins jobs on the controller is a mistake. Use agents to run Jenkins jobs. The controller isolation section in the “Securing Jenkins” chapter of the documentation describes the security risks of running jobs on the controller
- Mounting the Docker socket in the container image is a mistake. A stackoverflow article describes the security risks of mounting the Docker socket in a container
When I build images, the layers that do not change are cached. I don’t know what configuration you use to build container images, but if the layers are not cached in your configuration, then you need to fix that configuration. Layer caching is a standard part of the docker build process.