docker - 在 Emacs 中使用 org-mode 的 Docker 编程环境的文学编程设置

标签 docker emacs org-mode literate-programming

我整理了一个小示例,说明如何在 Emacs 中使用 Docker 管理我的开发环境。这个想法是,与其将多个文件放在相互关联的文件夹中,从而难以跟踪过渡更改,不如拥有一个组织文件,只是将各个文件纠缠在一起,甚至在组织内执行一些源代码块为方便使用某些 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

最佳答案

回复:

I am particularly concerned about the local-file variable that defines the docker-image name on a per-file basis and which I need to read out in several places in a quite cumbersome way

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

: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

关于docker - 在 Emacs 中使用 org-mode 的 Docker 编程环境的文学编程设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61247999/

相关文章:

windows - 如何在使用 Windows 主机时挂载 Docker 卷?

docker - 使用 Google AI Platform Notebook 实例,如何从我的本地计算机通过 ssh 连接到 jupyterlab 容器?

emacs - 如何从 emacs kill-ring 中删除顶部条目 (pop)?

r - 如何在不终止 R 进程的情况下终止 ESS 中的 Shiny 应用程序

html - Emacs 组织模式 : specify raw area for HTML exporter

emacs - 如何在 Org 模式下使用 SyncTeX?

docker - gcloud docker 推送可靠性

text - 如何用elisp处理一系列文件?

emacs - 在 Emacs 中,如何从文件开头搜索?

bash - 访问运行脚本的 docker 容器外部的 bash 脚本变量