This page is about a TRAMP integration that allows navigating docker’s container. To see other packages related to docker, see Docker.
Install the package at https://github.com/emacs-pe/docker-tramp.el
NOTE: if running Emacs built from the sources (or v29.1), as of Nov-2022 (probably earlier) there’s a native support for Docker via tramp (tramp-container.el) so the above package is not required.
In order to open files inside Docker containers with tramp, add the following to your .emacs
:
;; Open files in Docker containers like so: /docker:drunk_bardeen:/etc/passwd (push (cons "docker" '((tramp-login-program "docker") (tramp-login-args (("exec" "-it") ("%h") ("/bin/bash"))) (tramp-remote-shell "/bin/sh") (tramp-remote-shell-args ("-i") ("-c")))) tramp-methods) (defadvice tramp-completion-handle-file-name-all-completions (around dotemacs-completion-docker activate) "(tramp-completion-handle-file-name-all-completions \"\" \"/docker:\" returns a list of active Docker container names, followed by colons." (if (equal (ad-get-arg 1) "/docker:") (let* ((dockernames-raw (shell-command-to-string "docker ps | perl -we 'use strict; $_ = <>; m/^(.*)NAMES/ or die; my $offset = length($1); while(<>) {substr($_, 0, $offset, q()); chomp; for(split m/\\W+/) {print qq($_:\n)} }'")) (dockernames (cl-remove-if-not #'(lambda (dockerline) (string-match ":$" dockerline)) (split-string dockernames-raw "\n")))) (setq ad-return-value dockernames)) ad-do-it))
My Docker container names often have underscores and your Perl split is breaking up the name into multiples. Here is simpler version with awk:
(defadvice tramp-completion-handle-file-name-all-completions (around dotemacs-completion-docker activate) "(tramp-completion-handle-file-name-all-completions \"\" \"/docker:\" returns a list of active Docker container names, followed by colons." (if (equal (ad-get-arg 1) "/docker:") (let* ((dockernames-raw (shell-command-to-string "docker ps | awk '$NF != \"NAMES\" { print $NF \":\" }'")) (dockernames (cl-remove-if-not #'(lambda (dockerline) (string-match ":$" dockerline)) (split-string dockernames-raw "\n")))) (setq ad-return-value dockernames)) ad-do-it))
Docker has a --format option, which may give the names (including the trailing colon) directly
docker ps --format '{{.Names}}:'