Running emacs in a docker container

2019-03-15 00:24发布

I'm using a Mac and want to run emacs in my docker container. Is there a preferred way to solve this? In my flow I get stuc because the DISPLAY/TERM aren't set

> docker exec -it c6a7a76db84c bash
> sudo apt-get install emacs
...
> oot@c6a7a76db84c:/var/log/apache2# emacs
Please set the environment variable DISPLAY or TERM (see `tset').
> oot@c6a7a76db84c:/var/log/apache2# tset
tset: unknown terminal type unknown
Terminal type?

What should I use for tset? Or is there a preferred way to run emacs in a docker container?

标签: emacs docker
3条回答
聊天终结者
2楼-- · 2019-03-15 01:07

In order to edit files inside a container, it's generally best to use volumes and the editor on the host, as Bryan suggests. In fact, you should never edit a file that's not in a volume as your changes will be lost when the container is removed.

However, it's quite possible and useful to run editors and even GUI applications inside containers. In order to run the command line version of emacs, all I had to do was:

$ docker run -it debian /bin/bash
root@02bd877c1052:/# apt-get update && apt-get install -y emacs23-nox
root@02bd877c1052:/# emacs

I've tested this with boot2docker and docker running natively on Linux. I think your problem was because emacs was trying to launch the X version of emacs, which won't work by default as there isn't an XServer running.

If you want to run a GUI application inside a container, you have a few choices:

  • Use VNC or similar
  • Use ssh -x to do X forwarding (requires an XServer such as xquartz)
  • Mount the xsocket inside the container (again requires an XServer and I'm not sure how it will work with xquartz, also there are some security issues with exposing the xsocket)

Please don't believe that containers shouldn't be used for interactive applications. There are many reasons to do so, including security. The subuser project uses Docker to run interactive applications and goes into detail about why this can be a good idea.

查看更多
老娘就宠你
3楼-- · 2019-03-15 01:15

As Adrian Mouat mentioned in his comment, just do:

export TERM=xterm

and you'll be able to run emacs (note, though, that you'll always have to double-enter C-p to move to previous line, because Docker).

查看更多
趁早两清
4楼-- · 2019-03-15 01:25

Running an editor inside the container isn't really a good idea. Containers work well for services, not interactive applications. If you try it, all the input and output is being multiplexed over an http channel from the process (emacs) to the Docker CLI via the Docker daemon. Also if the files you are editing are within the container's layered filesystem then that adds extra overhead too.

As Docker say, volumes are a better option:

you can also mount a directory from your own host into a container.

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

This will mount the local directory, /src/webapp, into the container as the /opt/webapp directory. This is very useful for testing, for example we can mount our source code inside the container and see our application at work as we change the source code.

Note that as from Docker 1.3 the -v switch will also work from the outer Mac.

查看更多
登录 后发表回答