runc

用于根据 OCI 规范生成和运行容器的 CLI 工具。「CLI tool for spawning and running containers according to the OCI specification」

  • Owner: opencontainers/runc
  • Platform: Docker, Linux, Vagrant
  • License:: Apache License 2.0
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

runc

介绍

runc 是一个 CLI 工具,用于根据 OCI 规范生成和运行容器。

发布

runc 依赖并跟踪 runtime-spec 存储库。我们将尝试确保 runc 和 OCI 规范的主要版本保持同步。这意味着 runc 1.0.0 应该实现规范的1.0版本。

您可以在 release 页面上找到 runc 的正式发行版。

当前,以下特性还不能用于生产:

安全

此处概述了报告过程和披露沟通。

安全审核

Cure53 执行了第三方安全审核,您可以在这里查看完整的报告。

构建

runc 当前支持具有各种体系结构支持的 Linux 平台。必须使用 Go 1.13 或更高版本构建。

为了启用 seccomp 支持,您将需要在平台上安装 libseccomp。

例如 CentOS 的 libseccomp-devel 或 Ubuntu 的 libseccomp-dev

#在您的 GOPATH/src 中创建一个 'github.com/opencontainers'
go get github.com/opencontainers/runc
cd $GOPATH/src/github.com/opencontainers/runc
make
sudo make install

您还可以使用 go get 安装到您的 GOPATH,假设您已经在 src 下创建了 github.com 父文件夹:

go get github.com/opencontainers/runc
cd $GOPATH/src/github.com/opencontainers/runc
make
sudo make install

runc 将安装到系统上的 /usr/local/sbin/runc。

构建标签

runc 支持可选的构建标记以编译对各种功能的支持,其中一些功能默认情况下处于启用状态(请参阅顶层 Makefile 中的 BUILDTAGS)。

要更改默认的构建标记,请为make设置BUILDTAGS变量,例如

make BUILDTAGS='seccomp apparmor'

Build Tag Feature Enabled by default Dependency
seccomp Syscall filtering yes libseccomp
selinux selinux process and mount labeling yes
apparmor apparmor profile support yes
nokmem disable kernel memory accounting no

运行测试套件

runc 当前支持通过 Docker 运行其测试套件。要运行套件,只需输入 make test。

make test

还有其他make目标可以在容器外部运行测试,但不建议这样做,因为编写测试时会期望它们可以在任何地方写入和删除。

您可以通过设置 TESTFLAGS 变量来运行特定的测试用例。

# make test TESTFLAGS="-run=SomeTestFunction"

您可以通过设置 TESTPATH 变量来运行特定的集成测试。

# make test TESTPATH="/checkpoint.bats"

您可以通过设置 ROOTLESS_TESTPATH 变量来运行特定的 rootless 集成测试。

# make test ROOTLESS_TESTPATH="/checkpoint.bats"

您可以通过设置CONTAINER_ENGINE_BUILD_FLAGS 和 CONTAINER_ENGINE_RUN_FLAGS 变量来使用容器引擎的标志运行测试。

# make test CONTAINER_ENGINE_BUILD_FLAGS="--build-arg http_proxy=http://yourproxy/" CONTAINER_ENGINE_RUN_FLAGS="-e http_proxy=http://yourproxy/"

依存关系管理

runc 使用 Go Modules 进行依赖项管理。请参阅 Go Modules 以了解如何添加或更新新的依赖项。更新依赖项时,请确保您正在运行 Go 1.14 或更高版本。

# Update vendored dependencies
make vendor
# Verify all dependencies
make verify-dependencies

使用 runc

创建一个 OCI 捆绑包

为了使用 runc,您必须具有 OCI 捆绑包格式的容器。 如果安装了 Docker,则可以使用其 export 方法从现有 Docker 容器中获取根文件系统。

# create the top most bundle directory
mkdir /mycontainer
cd /mycontainer

# create the rootfs directory
mkdir rootfs

# export busybox via Docker into the rootfs directory
docker export $(docker create busybox) | tar -C rootfs -xvf -

填充根文件系统后,您只需生成捆绑包内 config.json 文件格式的规范即可。 runc 提供了一个 spec 命令来生成基本模板规范,然后可以对其进行编辑。 要查找规范中字段的功能和文档,请参考 specs 存储库。

runc spec

运行容器

假设您具有上一步中的 OCI 捆绑包,则可以两种不同的方式执行容器。

第一种方法是使用便捷命令运行,它将在退出容器后处理容器的创建,启动和删除操作。

# run as root
cd /mycontainer
runc run mycontainerid

如果您使用未修改的“runc spec”模板,则应该在容器内为您提供 sh 会话。

启动容器的第二种方法是使用 specs 生命周期操作。 这使您可以更好地控制容器在运行时如何创建和管理。 这也将在后台启动容器,因此您必须编辑 config.json 来删除此处简单示例的终端设置。 您的 config.json 中的处理字段如下所示,带有 "terminal": false and "args": ["sleep", "5"]。

"process": {
                "terminal": false,
                "user": {
                        "uid": 0,
                        "gid": 0
                },
                "args": [
                        "sleep", "5"
                ],
                "env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "TERM=xterm"
                ],
                "cwd": "/",
                "capabilities": {
                        "bounding": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "effective": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "inheritable": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "permitted": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "ambient": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ]
                },
                "rlimits": [
                        {
                                "type": "RLIMIT_NOFILE",
                                "hard": 1024,
                                "soft": 1024
                        }
                ],
                "noNewPrivileges": true
        },

现在,我们可以在您的 Shell 中进行生命周期操作。

# run as root
cd /mycontainer
runc create mycontainerid

# view the container is created and in the "created" state
runc list

# start the process inside the container
runc start mycontainerid

# after 5 seconds view that the container has exited and is now in the stopped state
runc list

# now delete the container
runc delete mycontainerid

这允许更高级别的系统在创建容器之后和/或在删除容器之前通过设置各种设置来扩展容器创建逻辑。 例如,容器的网络堆栈通常在创建之后但在启动之前进行设置。

Rootless 容器

runc 能够在没有 root 特权的情况下运行容器。 这称为 rootless。 您需要将一些参数传递给 runc 才能运行 rootless 容器。 参见下文,并与以前的版本进行比较。

注意:为了使用此功能,必须在内核中编译并启用“User Namespaces”。根据发行版的不同,有不同的方法来实现这一点:

  • 确认已在内核配置中设置 CONFIG_USER_NS=y (通常在 /proc/config.gz 中找到)
  • Arch/Debian: echo 1 > /proc/sys/kernel/unprivileged_userns_clone
  • RHEL/CentOS 7: echo 28633 > /proc/sys/user/max_user_namespaces

以普通用户身份运行以下命令:

# Same as the first example
mkdir ~/mycontainer
cd ~/mycontainer
mkdir rootfs
docker export $(docker create busybox) | tar -C rootfs -xvf -

# The --rootless parameter instructs runc spec to generate a configuration for a rootless container, which will allow you to run the container as a non-root user.
runc spec --rootless

# The --root parameter tells runc where to store the container state. It must be writable by the user.
runc --root /tmp/runc run mycontainerid

Supervisors

runc 可以与流程管理器和初始化系统一起使用,以确保容器退出时重新启动。 一个示例的 systemd unit 文件看起来像这样。

[Unit]
Description=Start My Container

[Service]
Type=forking
ExecStart=/usr/local/sbin/runc run -d --pid-file /run/mycontainerid.pid mycontainerid
ExecStopPost=/usr/local/sbin/runc delete mycontainerid
WorkingDirectory=/mycontainer
PIDFile=/run/mycontainerid.pid

[Install]
WantedBy=multi-user.target

cgroup v2

参见 ./docs/cgroup-v2.md.

许可

该代码和文档根据 Apache 2.0 许可发布。

(The first version translated by vz on 2020.07.19)

Overview

Name With Owneropencontainers/runc
Primary LanguageGo
Program languageMakefile (Language Count: 6)
PlatformDocker, Linux, Vagrant
License:Apache License 2.0
Release Count47
Last Release Namev1.2.0-rc.1 (Posted on 2024-04-03 21:43:42)
First Release Namev0.0.1 (Posted on 2015-07-16 11:18:35)
Created At2015-06-05 23:30:45
Pushed At2024-05-07 21:10:24
Last Commit At
Stargazers Count11.4k
Watchers Count383
Fork Count2k
Commits Count6.9k
Has Issues Enabled
Issues Count1181
Issue Open Count275
Pull Requests Count2229
Pull Requests Open Count106
Pull Requests Close Count686
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

runc

Go Report Card
Go Reference
CII Best Practices
gha/validate
gha/ci
CirrusCI

Introduction

runc is a CLI tool for spawning and running containers on Linux according to the OCI specification.

Releases

You can find official releases of runc on the release page.

All releases are signed by one of the keys listed in the runc.keyring file in the root of this repository.

Security

The reporting process and disclosure communications are outlined here.

Security Audit

A third party security audit was performed by Cure53, you can see the full report here.

Building

runc only supports Linux. It must be built with Go version 1.19 or higher.

In order to enable seccomp support you will need to install libseccomp on your platform.

e.g. libseccomp-devel for CentOS, or libseccomp-dev for Ubuntu

# create a 'github.com/opencontainers' in your GOPATH/src
cd github.com/opencontainers
git clone https://github.com/opencontainers/runc
cd runc

make
sudo make install

You can also use go get to install to your GOPATH, assuming that you have a github.com parent folder already created under src:

go get github.com/opencontainers/runc
cd $GOPATH/src/github.com/opencontainers/runc
make
sudo make install

runc will be installed to /usr/local/sbin/runc on your system.

Build Tags

runc supports optional build tags for compiling support of various features,
with some of them enabled by default (see BUILDTAGS in top-level Makefile).

To change build tags from the default, set the BUILDTAGS variable for make,
e.g. to disable seccomp:

make BUILDTAGS=""
Build Tag Feature Enabled by Default Dependencies
seccomp Syscall filtering using libseccomp. yes libseccomp
!runc_nodmz Reduce memory usage for CVE-2019-5736 protection by using a small C binary, see memfd-bind for more details. runc_nodmz disables this feature and causes runc to use a different protection mechanism which will further increases memory usage temporarily during container startup. This feature can also be disabled at runtime by setting the RUNC_DMZ=legacy environment variable. yes
runc_dmz_selinux_nocompat Disables a SELinux DMZ workaround (new distros should set this). See dmz README for details. no

The following build tags were used earlier, but are now obsoleted:

  • nokmem (since runc v1.0.0-rc94 kernel memory settings are ignored)
  • apparmor (since runc v1.0.0-rc93 the feature is always enabled)
  • selinux (since runc v1.0.0-rc93 the feature is always enabled)

Running the test suite

runc currently supports running its test suite via Docker.
To run the suite just type make test.

make test

There are additional make targets for running the tests outside of a container but this is not recommended as the tests are written with the expectation that they can write and remove anywhere.

You can run a specific test case by setting the TESTFLAGS variable.

# make test TESTFLAGS="-run=SomeTestFunction"

You can run a specific integration test by setting the TESTPATH variable.

# make test TESTPATH="/checkpoint.bats"

You can run a specific rootless integration test by setting the ROOTLESS_TESTPATH variable.

# make test ROOTLESS_TESTPATH="/checkpoint.bats"

You can run a test using your container engine's flags by setting CONTAINER_ENGINE_BUILD_FLAGS and CONTAINER_ENGINE_RUN_FLAGS variables.

# make test CONTAINER_ENGINE_BUILD_FLAGS="--build-arg http_proxy=http://yourproxy/" CONTAINER_ENGINE_RUN_FLAGS="-e http_proxy=http://yourproxy/"

Dependencies Management

runc uses Go Modules for dependencies management.
Please refer to Go Modules for how to add or update
new dependencies.

# Update vendored dependencies
make vendor
# Verify all dependencies
make verify-dependencies

Using runc

Please note that runc is a low level tool not designed with an end user
in mind. It is mostly employed by other higher level container software.

Therefore, unless there is some specific use case that prevents the use
of tools like Docker or Podman, it is not recommended to use runc directly.

If you still want to use runc, here's how.

Creating an OCI Bundle

In order to use runc you must have your container in the format of an OCI bundle.
If you have Docker installed you can use its export method to acquire a root filesystem from an existing Docker container.

# create the top most bundle directory
mkdir /mycontainer
cd /mycontainer

# create the rootfs directory
mkdir rootfs

# export busybox via Docker into the rootfs directory
docker export $(docker create busybox) | tar -C rootfs -xvf -

After a root filesystem is populated you just generate a spec in the format of a config.json file inside your bundle.
runc provides a spec command to generate a base template spec that you are then able to edit.
To find features and documentation for fields in the spec please refer to the specs repository.

runc spec

Running Containers

Assuming you have an OCI bundle from the previous step you can execute the container in two different ways.

The first way is to use the convenience command run that will handle creating, starting, and deleting the container after it exits.

# run as root
cd /mycontainer
runc run mycontainerid

If you used the unmodified runc spec template this should give you a sh session inside the container.

The second way to start a container is using the specs lifecycle operations.
This gives you more power over how the container is created and managed while it is running.
This will also launch the container in the background so you will have to edit
the config.json to remove the terminal setting for the simple examples
below (see more details about runc terminal handling).
Your process field in the config.json should look like this below with "terminal": false and "args": ["sleep", "5"].

        "process": {
                "terminal": false,
                "user": {
                        "uid": 0,
                        "gid": 0
                },
                "args": [
                        "sleep", "5"
                ],
                "env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "TERM=xterm"
                ],
                "cwd": "/",
                "capabilities": {
                        "bounding": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "effective": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "inheritable": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "permitted": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ],
                        "ambient": [
                                "CAP_AUDIT_WRITE",
                                "CAP_KILL",
                                "CAP_NET_BIND_SERVICE"
                        ]
                },
                "rlimits": [
                        {
                                "type": "RLIMIT_NOFILE",
                                "hard": 1024,
                                "soft": 1024
                        }
                ],
                "noNewPrivileges": true
        },

Now we can go through the lifecycle operations in your shell.

# run as root
cd /mycontainer
runc create mycontainerid

# view the container is created and in the "created" state
runc list

# start the process inside the container
runc start mycontainerid

# after 5 seconds view that the container has exited and is now in the stopped state
runc list

# now delete the container
runc delete mycontainerid

This allows higher level systems to augment the containers creation logic with setup of various settings after the container is created and/or before it is deleted. For example, the container's network stack is commonly set up after create but before start.

Rootless containers

runc has the ability to run containers without root privileges. This is called rootless. You need to pass some parameters to runc in order to run rootless containers. See below and compare with the previous version.

Note: In order to use this feature, "User Namespaces" must be compiled and enabled in your kernel. There are various ways to do this depending on your distribution:

  • Confirm CONFIG_USER_NS=y is set in your kernel configuration (normally found in /proc/config.gz)
  • Arch/Debian: echo 1 > /proc/sys/kernel/unprivileged_userns_clone
  • RHEL/CentOS 7: echo 28633 > /proc/sys/user/max_user_namespaces

Run the following commands as an ordinary user:

# Same as the first example
mkdir ~/mycontainer
cd ~/mycontainer
mkdir rootfs
docker export $(docker create busybox) | tar -C rootfs -xvf -

# The --rootless parameter instructs runc spec to generate a configuration for a rootless container, which will allow you to run the container as a non-root user.
runc spec --rootless

# The --root parameter tells runc where to store the container state. It must be writable by the user.
runc --root /tmp/runc run mycontainerid

Supervisors

runc can be used with process supervisors and init systems to ensure that containers are restarted when they exit.
An example systemd unit file looks something like this.

[Unit]
Description=Start My Container

[Service]
Type=forking
ExecStart=/usr/local/sbin/runc run -d --pid-file /run/mycontainerid.pid mycontainerid
ExecStopPost=/usr/local/sbin/runc delete mycontainerid
WorkingDirectory=/mycontainer
PIDFile=/run/mycontainerid.pid

[Install]
WantedBy=multi-user.target

More documentation

License

The code and docs are released under the Apache 2.0 license.

To the top