通过docker使用emacs

我习惯使用emacs来编辑文件,但unraid上没有,通过自带的vim/vi编辑几个文件还可以,多了就很不习惯了。 于是,就想通过docker来运行emacs。同时,也解决了不同主机系统上的emacs版本不一致,总有这个或 那个的小问题。

构建相关脚本

dockerfile文件

FROM alpine:edge

ADD install.alpine /install.sh
RUN /install.sh

安装软件的脚本

#!/bin/sh
set -x

op=${1:-all}

apk add --no-cache apk-tools-static
wget -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub

function install_app () {
    sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
    echo 'https://mirrors.ustc.edu.cn/alpine/edge/testing' >> /etc/apk/repositories
    apk add --no-cache bash \
        sudo \
        dbus-x11 \
        git \
        curl \
        emacs-x11 \
        openssh-client \
        librime \
        wget \
        gcc \
        libc-dev \
        librime-dev \
        diffutils \
        wqy-zenhei \
        gzip \
        fontconfig \
        font-noto-emoji \
        opencc \
        unzip \
        tar \
        make \
        ttf-dejavu \
  ttf-font-awesome \

}

function install_source_code() {
    set  -euo pipefail
    I1FS=$'\n\t'
    mkdir -p /tmp/adodefont
    cd /tmp/adodefont
    wget -q --show-progress -O source-code-pro.zip https://github.com/adobe-fonts/source-code-pro/archive/2.030R-ro/1.050R-it.zip
    unzip -q source-code-pro.zip -d source-code-pro
    mkdir -p ~/.fonts
    cp -v source-code-pro/*/OTF/*.otf ~/.fonts/
    fc-cache -f
    rm -rf source-code-pro{,.zip}
}

function clean_all() {
    rm -rf /var/cache/* /tmp/* /var/log/* ~/.cache && mkdir -p /var/cache/apk
}

function all() {
    install_app
    #install_source_code
    clean_all
}

$op

第一次启动后,需要手动运行 /install.sh install_source_code来安装source_code字体,不然spacemacs 会报告缺失字体。

docker-compose.yml

version: "3"
services:
  emacs:
  build:
    context: .
    dockerfile: alpine.Dockerfile
    #image: alpine_emacs:28.2
    #restart: always
    container_name: emacs
    entrypoint: /root/entry.sh
    environment:
    - DISPLAY=$DISPLAY
    - LANG=zh-CN.UTF-8
    network_mode: "host"
    working_dir: "/root"
    privileged: true
    command: "bash"
    volumes:
    - /tmp/.X11-unix:/tmp/.X11-unix
    - ./root:/root:z
    - /mnt/:/mnt:z

上面是我的unraid上的启动脚本,修改了entrypoint,主要是想添加一些自定义的变量配置,entry.sh内容如下:

#!/bin/bash
. /root/.bashrc
tail -f /dev/null

主要就是让容器启动后保持运行状态,这样下次就可以通过docker exec来直接运行emacs了

启动脚本

以unraid为例,我在/boot/config中创建了一个mybashprofile的文件, 然后在~/.bash_profile中添加一行代码:

test -f /boot/config/mybashprofile && source /boot/config/mybashprofile

tips: /boot/config/go中添加下面的代码后,每次登录进unraid系统后,mybashprofile中的相关定义会自动生效

echo "test -f /boot/config/mybashprofile && source /boot/config/mybashprofile" >> /root/.bash_profile

其他系统不用这么麻烦,直接在.bash_rc或者.bash_profile中添加一个source mybashprofile就可以了。

mybashprofile文件的内容:

function emacs {
    local current_dir="$PWD"
    local check=`docker ps | grep emacs`
    if [ "X"${check} == "X" ]
    then
        docker compose -f /mnt/user/appdata/emacs/docker-compose.yml up -d
    fi
    docker exec -it emacs /root/cmd.sh "$current_dir" emacs "${@}"
}

然后,就可以直接输入emacs之后,自动启动emacs了。并且,在容器中自动切换到和容器外部相同的目录中。如果emacs容器没有启动,则自动启动容器,然后再执行emacs。这样使用起来方便了很多。

cmd.sh的内容如下

#!/bin/bash
. /root/.bashrc
cd $1
shift
echo $@
$@
发布日期:
分类:技术

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据