github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/README.md (about)

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