首页 > 解决方案 > 在 Emacs 中使用 org-mode 的 Docker 编程环境的文学编程设置

问题描述

我整理了一个小例子,说明我如何在 Emacs 中使用 Docker 管理我的开发环境。这个想法是,与其在文件夹中拥有多个相互关联的文件,从而使跟踪过渡更改变得困难,不如拥有一个 org 文件,只需将各个文件缠在一起,甚至在 org 中执行一些源代码块文件以便于使用一些 Emacs 包。下面我尝试描述我的推理,并感谢有关如何简化事情以及替代我的方法的建议。也许尽管它也可以证明对其他以类似工作流程为目标的人有所帮助。我可以改进一些东西,通过遵循最佳实践等?我特别担心本地文件变量,它基于每个文件定义 docker-image 名称,我需要以非常麻烦的方式在多个地方读出它

  # -*- image_name: "test_env"; -*-


    * Environment
      :PROPERTIES:
      :header-args: :results output :mkdirp yes
      :END:

    ** requirements.txt
        #+BEGIN_SRC conf :tangle requirements.txt
        numpy
        #+END_SRC

    ** Start docker
    #+BEGIN_SRC sh :dir "/sudo::"
    sudo service docker start
        #+END_SRC

    ** Dockerfile
    Use [ob-docker-build][1] to build the docker images upon pressing C-c C-c. The image
    name is grabbed from the file-local variable at the top of this file. This file
    can also be tangled to disk.

    #+BEGIN_SRC docker-build :dir "." :tag (symbol-value 'image_name) :tangle Dockerfile
    FROM python:3.8-slim-buster

    ENV VIRTUAL_ENV=/opt/venv
    RUN python3 -m venv $VIRTUAL_ENV
    # Make sure we use the virtualenv:
    ENV PATH="$VIRTUAL_ENV/bin:$PATH"

    COPY requirements.txt .
    RUN pip install --quiet --no-cache-dir -r requirements.txt
    WORKDIR /app
    # just for keeping the container running
    CMD tail -f /dev/null
    #+END_SRC

    ** run_docker.sh
    This script can either be tangled to disk or is executed automatically by
    org-sbe in the python application below

    #+HEADER: :var image_name=(symbol-value 'image_name)
    #+name: run_docker
    #+BEGIN_SRC bash :tangle run_docker.sh :tangle-mode (identity #o755) :results output
    #!/bin/bash

    SBX_DIR=$(realpath $(dirname $0))

    # generate some id for the container
    CONTAINER_NAME=$(uuidgen | md5sum | awk '{ print $1 }' | cut -c -12)

    if [ $# -eq 0 ]; then
        ARGS=$SHELL
    else
        ARGS=$@
    fi

    DOCKER="docker run"

    DOCKER_ARGS=(
        --name "$CONTAINER_NAME"
        -v $HOME:$HOME
        -v "$SBX_DIR":"$SBX_DIR"
        --rm
        --user $(id -u):$(id -g)
        -w $SBX_DIR
        -itd ${image_name}
        $ARGS
    )

    $DOCKER ${DOCKER_ARGS[@]}
    #+END_SRC

    ** python
    Use the [docker-tramp][1] package to attach to a running container. C-c C-c on the
    block below causes the run-docker src-block to be executed. The resulting ID of
    the container is then used here to attach to this running instance

    #+BEGIN_SRC python :dir (concat "/docker:" (org-sbe run_docker) ":") :results output
    print('hello')
    #+END_SRC

这里优先考虑什么?

# -*- foo: "one"; -*-

#+PROPERTY: header-args :var foo=two
#+PROPERTY: foo=three


* first
:PROPERTIES:
:foo: four
:END:
** second
:PROPERTIES:
:foo: five
:END:
#+HEADER: foo=(symbol-value 'foo)
#+BEGIN_SRC bash :var foo=(org-macro--get-property "foo" "")
echo $foo
#+END_SRC

标签: dockeremacsorg-modeliterate-programming

解决方案


回覆:

我特别担心本地文件变量,它基于每个文件定义 docker-image 名称,我需要以非常麻烦的方式在多个地方读出它

您可以在文本和标题参数中定义属性和访问权限:

:PROPERTIES:
:foo:  bar
:END:

You can access the foo property like this: {{{property(foo)}}}.

And you can access it in header args like this:

#+BEGIN_SRC bash :var foo=(org-macro--get-property "foo" "")
echo $foo # prints bar
#+END_SRC

推荐阅读