runc README

The project's README from github.com/opencontainers/runc. As one markdown file: README.md.

runc

Go Reference CII Best Practices gha/validate gha/ci

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. See the header of go.mod for the minimally required Go version.

Pre-Requisites

Utilities and Libraries

In addition to Go, building runc requires multiple utilities and libraries to be installed on your system.

On Ubuntu/Debian, you can install the required dependencies with:

apt update && apt install -y make gcc linux-libc-dev libseccomp-dev pkg-config git

On CentOS/Fedora, you can install the required dependencies with:

yum install -y make gcc kernel-headers libseccomp-devel pkg-config git

On Alpine Linux, you can install the required dependencies with:

apk --update add bash make gcc libseccomp-dev musl-dev linux-headers git

The following dependencies are optional:

libpathrs

libpathrs is a Rust library runc can optionally use for path safety. As mentioned in the build tag section, its use is controlled with the libpathrs build tag. runc currently requires at least libpathrs 0.2.5 in order to function properly.

At time of writing, very few distributions have libpathrs packages and so it is usually necessary to build and install it locally. For detailed installation instructions, see the upstream documentation, but for development builds the following instructions should be sufficient:

libpathrs requires Rust 1.63+ (which is available on almost any distribution, including Debian oldstable and enterprise distributions like RHEL or SLES). Assuming you already have cargo installed (as well as other libpathrs dependencies like clang and lld), the following steps are all that are really necessary to install libpathrs:

LIBPATHRS_VERSION=0.2.5
curl -o - -sSL https://github.com/cyphar/libpathrs/releases/download/v${LIBPATHRS_VERSION}/libpathrs-${LIBPATHRS_VERSION}.tar.xz | tar xvfJ -
cd libpathrs-${LIBPATHRS_VERSION}/
make release
sudo ./install.sh --prefix=/usr/local
sudo ldconfig

As part of our CI, we make use of a custom installation script for libpathrs which may be useful as a reference for folks with more complicated needs. With script/build-libpathrs.sh the installation of libpathrs becomes as simple as:

sudo ./script/build-libpathrs.sh "$LIBPATHRS_VERSION" /usr/local
sudo ldconfig

However, please note that this installation script is completely unsupported and is not really intended for general use (it includes some workarounds for issues in our CI which will no longer be necessary once libpathrs has distribution packages we can use).

Build

# 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.

Version string customization

You can see the runc version by running runc --version. You can append a custom string to the version using the EXTRA_VERSION make variable when building, e.g.:

make EXTRA_VERSION="+build-1"

Bear in mind to include some separator for readability.

Build Tags

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

The following build tags are currently recognized:

Build Tag Feature Set by Default Dependencies
seccomp Syscall filtering using libseccomp. yes libseccomp
libpathrs Use libpathrs for path safety. yes libpathrs
runc_nocriu Disables runc checkpoint/restore. no criu

To add or remove build tags from the default set, use the RUNC_BUILDTAGS make or shell variable. Tags prefixed with - are removed from the default set; others are added. For example:

# Add runc_nocriu and remove seccomp tag.
make RUNC_BUILDTAGS="runc_nocriu -seccomp"

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

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/"

Go 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:

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.

runc (opencontainers/runc)

Searching Database…

13.5K ·GitHub
这是什么东西?runc 是一个命令行工具,用于在 Linux 上按照 OCI(开放容器倡议)规范创建和运行容器。
它是 Docker 的替代品,还是 Docker 底层依赖的东西?它是更底下的那一层:一个面向底层、并非给最终用户使用的工具,主要被 Docker、Podman 这类更高层的容器软件调用。除非你有特殊需求,否则直接用那些软件就好。
怎么安装它并跑起我的第一个容器?用 make 构建(需要 Go),或者从 releases 页面下载已签名的版本;二进制文件会安装到 /usr/local/sbin/runc。然后创建一个 OCI bundle——也就是一个根文件系统加上由 runc spec 生成的 config.json——再用 runc run 启动它。
构建它需要我的系统上有哪些东西?需要 Go,再加上一组系统库,在 Debian、Fedora 或 Alpine 上用一条包管理器命令就能装好。libseccomp(系统调用过滤)和 libpathrs(一个 Rust 写的路径安全库)是可选的,通过 build tags 开启。
它能像 Docker Desktop 那样在 macOS 或 Windows 上用吗?不能——runc 只支持 Linux,所以运行它的机器必须有 Linux 内核。
它能在没有 root 权限的情况下运行容器吗?可以——runc 支持 rootless 容器,前提是你的内核编译并启用了 user namespaces(CONFIG_USER_NS=y)。
容器退出后,怎么让它继续保持运行?使用进程守护程序或 init 系统:runc 可以配合它们工作(README 里有一个 systemd unit 文件的示例),这样容器退出后就会被自动重启。
我对容器的生命周期有多大的控制权?除了 runc run 这个便捷命令之外,你还可以直接执行各个生命周期操作——create、start、delete——并在步骤之间插入自己的设置,比如在 create 之后、start 之前配置网络。
这个代码库做过安全审计吗?做过——Cure53 对它做了一次第三方安全审计,完整报告的链接在 README 里。
What are similar projects?containerd, gvisor, distribution, podman, buildkit, moby, grype, arcbox
Size
25.2 MB
Stars
13.5K
Age
11y
Commits · 90 Days