branch: elpa/dockerfile-mode
commit fdebd1eda55af758354b893fc75a615303523919
Author: Robin Campbell Joy <[email protected]>
Commit: Robin Campbell Joy <[email protected]>
Pass --build-args to docker build
Docker allows to set build-time variables by passing them with
"--build-arg" to "docker build". This is useful when building images
behind a firewall in order to pass proxy settings without specifying
them explicitly in the Dockerfile.
---
dockerfile-mode.el | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/dockerfile-mode.el b/dockerfile-mode.el
index b14eae99b8..bbdae27737 100644
--- a/dockerfile-mode.el
+++ b/dockerfile-mode.el
@@ -35,6 +35,14 @@
(defcustom dockerfile-use-sudo nil
"Runs docker builder command with sudo.")
+(defcustom dockerfile-build-args nil
+ "List of --build-arg to pass to docker build.
+
+Each element of the list will be passed as a separate
+ --build-arg to the docker build command."
+ :type '(repeat string)
+ :group 'dockerfile)
+
(defvar dockerfile-font-lock-keywords
`(,(cons (rx (or line-start "onbuild ")
(group (or "from" "maintainer" "run" "cmd" "expose" "env" "arg"
@@ -80,6 +88,11 @@
(unless dockerfile-mode-abbrev-table
(define-abbrev-table 'dockerfile-mode-abbrev-table ()))
+(defun dockerfile-build-arg-string ()
+ "Create a --build-arg string for each element in `dockerfile-build-args'."
+ (mapconcat (lambda (arg) (concat "--build-arg " "\"" arg "\""))
+ dockerfile-build-args " "))
+
;;;###autoload
(defun dockerfile-build-buffer (image-name)
"Build an image based upon the buffer"
@@ -90,7 +103,7 @@
(save-buffer)
(if (stringp image-name)
(async-shell-command
- (format "%sdocker build -t %s -f \"%s\" \"%s\"" (if dockerfile-use-sudo
"sudo " "") image-name (buffer-file-name) (file-name-directory
(buffer-file-name)))
+ (format "%sdocker build -t %s %s -f \"%s\" \"%s\"" (if
dockerfile-use-sudo "sudo " "") image-name (dockerfile-build-arg-string)
(buffer-file-name) (file-name-directory (buffer-file-name)))
"*docker-build-output*")
(print "docker-image-name must be a string, consider surrounding it with
double quotes")))
@@ -104,7 +117,7 @@
(save-buffer)
(if (stringp image-name)
(async-shell-command
- (format "%s docker build --no-cache -t %s -f \"%s\" \"%s\"" (if
dockerfile-use-sudo "sudo" "") image-name (buffer-file-name)
(file-name-directory (buffer-file-name)))
+ (format "%s docker build --no-cache -t %s %s -f \"%s\" \"%s\"" (if
dockerfile-use-sudo "sudo" "") image-name (dockerfile-build-arg-string)
(buffer-file-name) (file-name-directory (buffer-file-name)))
"*docker-build-output*")
(print "docker-image-name must be a string, consider surrounding it with
double quotes")))