4

I want to add a Tramp method for opening files from Docker containers. Here's what I've got so far (but it doesn't work):

(add-to-list 'tramp-methods
             '("docker"
               (tramp-login-program "docker")
               (tramp-login-args
                (("run" "-ti" "%h" "bash")))))
(setq tramp-debug-buffer t)
(setq tramp-verbose 10)

The output this produces:

Tramp: Opening connection for test-newapi using docker...failed
Tramp: Opening connection for test-newapi using docker...failed
Tramp: Opening connection for test-newapi using docker...failed
Tramp: Opening connection for test-newapi using docker...failed
Tramp: Opening connection for test-newapi using docker...failed
byte-code: Host `test' looks like a remote host, `docker' can only use the local host

When trying to open /docker:test:/.

Question

How can I see what exactly Tramp was doing (what command did it feed to Docker?)

PS

I found this, but it gives the same result.

PPS

Nevermind, it had to be (tramp-login-args (("run" "-ti") ("%h") ("bash"))). But I'd still want to know how to debug a problem like this.

2 Answers 2

3

Easiest options:

  1. helm-tramp
    Tramp helm interface for ssh server and docker
  2. docker-tramp
    *docker-tramp.el offers a TRAMP method for Docker containers.

Both are available on MELPA and can be easily installed with:

M-x, package-install, and type name_of_the_package RET

Note: The Spacemacs Emacs distribution have the Docker layer which provides syntax highlight and build functionality to Dockerfiles as well as TRAMP access to running docker containers.
It also includes basic Docker container and image management provided by docker.el.


Also,

From the Emacs Wiki page, Tramp and Docker integration

Manual install:

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))

Your questions:

Nevermind, it had to be (tramp-login-args (("run" "-ti") ("%h") ("bash"))). But I'd still want to know how to debug a problem like this.

  • According to Emacs Wiki it has to be:

    (tramp-login-args (("exec" "-it") ("%h") ("/bin/bash")))
    
2
  • exec and run Docker commands do different things. exec executes the command in the container that is already running (and takes the id of the container as an argument), but run starts a container and takes as an argument the name of the image. Unfortunately, I cannot check Tramp+Docker right now, but at the time I wrote this question I also wrote to Tramp mailing list and there were some bugs / problems with that setup. Though it was over half a year ago, so, maybe they are fixed already.
    – wvxvw
    Jun 13, 2017 at 7:57
  • As I could understand from the Wiki page, Tramp just connect to a running instance of a container. That's why they chose exec instead of run. Jun 13, 2017 at 13:23
1

I don't know how to integrate docker with Tramp, but you seem to have it solved.

When you have enabled Tramp traces, there is a Tramp debug buffer. Enable at least trace level 6. Then you can search through the debug buffer with (6), this shows you all commands Tramp has sent, and the responses.

3
  • Yes, I saw that, but it doesn't print the command it used to try to connect (even at level 10). Actually, I'd be even happy with just knowing what function actually does it, so I could put a breakpoint in it.
    – wvxvw
    Jan 11, 2016 at 11:58
  • When there is nothing in the traces, Tramp hasn't sent a command yet. You can apply (setq debug-on-error t) in order to see where the error message comes from. Jan 11, 2016 at 12:45
  • Thanks, it actually wasn't an error, but I could get around that with debug-on-message set to that exact message.
    – wvxvw
    Jan 11, 2016 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.