...

Text file src/github.com/opencontainers/runc/README.md

Documentation: github.com/opencontainers/runc

     1# runc
     2
     3[![Go Report Card](https://goreportcard.com/badge/github.com/opencontainers/runc)](https://goreportcard.com/report/github.com/opencontainers/runc)
     4[![Go Reference](https://pkg.go.dev/badge/github.com/opencontainers/runc.svg)](https://pkg.go.dev/github.com/opencontainers/runc)
     5[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/588/badge)](https://bestpractices.coreinfrastructure.org/projects/588)
     6[![gha/validate](https://github.com/opencontainers/runc/workflows/validate/badge.svg)](https://github.com/opencontainers/runc/actions?query=workflow%3Avalidate)
     7[![gha/ci](https://github.com/opencontainers/runc/workflows/ci/badge.svg)](https://github.com/opencontainers/runc/actions?query=workflow%3Aci)
     8[![CirrusCI](https://api.cirrus-ci.com/github/opencontainers/runc.svg)](https://cirrus-ci.com/github/opencontainers/runc)
     9
    10## Introduction
    11
    12`runc` is a CLI tool for spawning and running containers on Linux according to the OCI specification.
    13
    14## Releases
    15
    16You can find official releases of `runc` on the [release](https://github.com/opencontainers/runc/releases) page.
    17
    18All releases are signed by one of the keys listed in the [`runc.keyring` file in the root of this repository](runc.keyring).
    19
    20## Security
    21
    22The reporting process and disclosure communications are outlined [here](https://github.com/opencontainers/org/blob/master/SECURITY.md).
    23
    24### Security Audit
    25A third party security audit was performed by Cure53, you can see the full report [here](https://github.com/opencontainers/runc/blob/master/docs/Security-Audit.pdf).
    26
    27## Building
    28
    29`runc` only supports Linux. It must be built with Go version 1.17 or higher.
    30
    31In order to enable seccomp support you will need to install `libseccomp` on your platform.
    32> e.g. `libseccomp-devel` for CentOS, or `libseccomp-dev` for Ubuntu
    33
    34```bash
    35# create a 'github.com/opencontainers' in your GOPATH/src
    36cd github.com/opencontainers
    37git clone https://github.com/opencontainers/runc
    38cd runc
    39
    40make
    41sudo make install
    42```
    43
    44You can also use `go get` to install to your `GOPATH`, assuming that you have a `github.com` parent folder already created under `src`:
    45
    46```bash
    47go get github.com/opencontainers/runc
    48cd $GOPATH/src/github.com/opencontainers/runc
    49make
    50sudo make install
    51```
    52
    53`runc` will be installed to `/usr/local/sbin/runc` on your system.
    54
    55
    56#### Build Tags
    57
    58`runc` supports optional build tags for compiling support of various features,
    59with some of them enabled by default (see `BUILDTAGS` in top-level `Makefile`).
    60
    61To change build tags from the default, set the `BUILDTAGS` variable for make,
    62e.g. to disable seccomp:
    63
    64```bash
    65make BUILDTAGS=""
    66```
    67
    68| Build Tag | Feature                            | Enabled by default | Dependency |
    69|-----------|------------------------------------|--------------------|------------|
    70| seccomp   | Syscall filtering                  | yes                | libseccomp |
    71
    72The following build tags were used earlier, but are now obsoleted:
    73 - **nokmem** (since runc v1.0.0-rc94 kernel memory settings are ignored)
    74 - **apparmor** (since runc v1.0.0-rc93 the feature is always enabled)
    75 - **selinux**  (since runc v1.0.0-rc93 the feature is always enabled)
    76
    77### Running the test suite
    78
    79`runc` currently supports running its test suite via Docker.
    80To run the suite just type `make test`.
    81
    82```bash
    83make test
    84```
    85
    86There 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.
    87
    88You can run a specific test case by setting the `TESTFLAGS` variable.
    89
    90```bash
    91# make test TESTFLAGS="-run=SomeTestFunction"
    92```
    93
    94You can run a specific integration test by setting the `TESTPATH` variable.
    95
    96```bash
    97# make test TESTPATH="/checkpoint.bats"
    98```
    99
   100You can run a specific rootless integration test by setting the `ROOTLESS_TESTPATH` variable.
   101
   102```bash
   103# make test ROOTLESS_TESTPATH="/checkpoint.bats"
   104```
   105
   106You can run a test using your container engine's flags by setting `CONTAINER_ENGINE_BUILD_FLAGS` and `CONTAINER_ENGINE_RUN_FLAGS` variables.
   107
   108```bash
   109# make test CONTAINER_ENGINE_BUILD_FLAGS="--build-arg http_proxy=http://yourproxy/" CONTAINER_ENGINE_RUN_FLAGS="-e http_proxy=http://yourproxy/"
   110```
   111
   112### Dependencies Management
   113
   114`runc` uses [Go Modules](https://github.com/golang/go/wiki/Modules) for dependencies management.
   115Please refer to [Go Modules](https://github.com/golang/go/wiki/Modules) for how to add or update
   116new dependencies.
   117
   118```
   119# Update vendored dependencies
   120make vendor
   121# Verify all dependencies
   122make verify-dependencies
   123```
   124
   125## Using runc
   126
   127Please note that runc is a low level tool not designed with an end user
   128in mind. It is mostly employed by other higher level container software.
   129
   130Therefore, unless there is some specific use case that prevents the use
   131of tools like Docker or Podman, it is not recommended to use runc directly.
   132
   133If you still want to use runc, here's how.
   134
   135### Creating an OCI Bundle
   136
   137In order to use runc you must have your container in the format of an OCI bundle.
   138If you have Docker installed you can use its `export` method to acquire a root filesystem from an existing Docker container.
   139
   140```bash
   141# create the top most bundle directory
   142mkdir /mycontainer
   143cd /mycontainer
   144
   145# create the rootfs directory
   146mkdir rootfs
   147
   148# export busybox via Docker into the rootfs directory
   149docker export $(docker create busybox) | tar -C rootfs -xvf -
   150```
   151
   152After a root filesystem is populated you just generate a spec in the format of a `config.json` file inside your bundle.
   153`runc` provides a `spec` command to generate a base template spec that you are then able to edit.
   154To find features and documentation for fields in the spec please refer to the [specs](https://github.com/opencontainers/runtime-spec) repository.
   155
   156```bash
   157runc spec
   158```
   159
   160### Running Containers
   161
   162Assuming you have an OCI bundle from the previous step you can execute the container in two different ways.
   163
   164The first way is to use the convenience command `run` that will handle creating, starting, and deleting the container after it exits.
   165
   166```bash
   167# run as root
   168cd /mycontainer
   169runc run mycontainerid
   170```
   171
   172If you used the unmodified `runc spec` template this should give you a `sh` session inside the container.
   173
   174The second way to start a container is using the specs lifecycle operations.
   175This gives you more power over how the container is created and managed while it is running.
   176This will also launch the container in the background so you will have to edit
   177the `config.json` to remove the `terminal` setting for the simple examples
   178below (see more details about [runc terminal handling](docs/terminals.md)).
   179Your process field in the `config.json` should look like this below with `"terminal": false` and `"args": ["sleep", "5"]`.
   180
   181
   182```json
   183        "process": {
   184                "terminal": false,
   185                "user": {
   186                        "uid": 0,
   187                        "gid": 0
   188                },
   189                "args": [
   190                        "sleep", "5"
   191                ],
   192                "env": [
   193                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
   194                        "TERM=xterm"
   195                ],
   196                "cwd": "/",
   197                "capabilities": {
   198                        "bounding": [
   199                                "CAP_AUDIT_WRITE",
   200                                "CAP_KILL",
   201                                "CAP_NET_BIND_SERVICE"
   202                        ],
   203                        "effective": [
   204                                "CAP_AUDIT_WRITE",
   205                                "CAP_KILL",
   206                                "CAP_NET_BIND_SERVICE"
   207                        ],
   208                        "inheritable": [
   209                                "CAP_AUDIT_WRITE",
   210                                "CAP_KILL",
   211                                "CAP_NET_BIND_SERVICE"
   212                        ],
   213                        "permitted": [
   214                                "CAP_AUDIT_WRITE",
   215                                "CAP_KILL",
   216                                "CAP_NET_BIND_SERVICE"
   217                        ],
   218                        "ambient": [
   219                                "CAP_AUDIT_WRITE",
   220                                "CAP_KILL",
   221                                "CAP_NET_BIND_SERVICE"
   222                        ]
   223                },
   224                "rlimits": [
   225                        {
   226                                "type": "RLIMIT_NOFILE",
   227                                "hard": 1024,
   228                                "soft": 1024
   229                        }
   230                ],
   231                "noNewPrivileges": true
   232        },
   233```
   234
   235Now we can go through the lifecycle operations in your shell.
   236
   237
   238```bash
   239# run as root
   240cd /mycontainer
   241runc create mycontainerid
   242
   243# view the container is created and in the "created" state
   244runc list
   245
   246# start the process inside the container
   247runc start mycontainerid
   248
   249# after 5 seconds view that the container has exited and is now in the stopped state
   250runc list
   251
   252# now delete the container
   253runc delete mycontainerid
   254```
   255
   256This 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`.
   257
   258#### Rootless containers
   259`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.
   260
   261**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:
   262- Confirm `CONFIG_USER_NS=y` is set in your kernel configuration (normally found in `/proc/config.gz`)
   263- Arch/Debian: `echo 1 > /proc/sys/kernel/unprivileged_userns_clone`
   264- RHEL/CentOS 7: `echo 28633 > /proc/sys/user/max_user_namespaces`
   265
   266Run the following commands as an ordinary user:
   267```bash
   268# Same as the first example
   269mkdir ~/mycontainer
   270cd ~/mycontainer
   271mkdir rootfs
   272docker export $(docker create busybox) | tar -C rootfs -xvf -
   273
   274# 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.
   275runc spec --rootless
   276
   277# The --root parameter tells runc where to store the container state. It must be writable by the user.
   278runc --root /tmp/runc run mycontainerid
   279```
   280
   281#### Supervisors
   282
   283`runc` can be used with process supervisors and init systems to ensure that containers are restarted when they exit.
   284An example systemd unit file looks something like this.
   285
   286```systemd
   287[Unit]
   288Description=Start My Container
   289
   290[Service]
   291Type=forking
   292ExecStart=/usr/local/sbin/runc run -d --pid-file /run/mycontainerid.pid mycontainerid
   293ExecStopPost=/usr/local/sbin/runc delete mycontainerid
   294WorkingDirectory=/mycontainer
   295PIDFile=/run/mycontainerid.pid
   296
   297[Install]
   298WantedBy=multi-user.target
   299```
   300
   301## More documentation
   302
   303* [cgroup v2](./docs/cgroup-v2.md)
   304* [Checkpoint and restore](./docs/checkpoint-restore.md)
   305* [systemd cgroup driver](./docs/systemd.md)
   306* [Terminals and standard IO](./docs/terminals.md)
   307* [Experimental features](./docs/experimental.md)
   308
   309## License
   310
   311The code and docs are released under the [Apache 2.0 license](LICENSE).

View as plain text