2015-06-22 10:31:12 +08:00
|
|
|
## runc
|
|
|
|
|
|
|
|
`runc` is a CLI tool for spawning and running containers according to the OCF specification.
|
|
|
|
|
2015-06-27 02:31:00 +08:00
|
|
|
## State of the project
|
|
|
|
|
2015-09-02 00:32:29 +08:00
|
|
|
Currently `runc` is an implementation of the OCI specification. We are currently sprinting
|
2015-12-11 09:11:17 +08:00
|
|
|
to have a v1 of the spec out. So the `runc` config format will be constantly changing until
|
|
|
|
the spec is finalized. However, we encourage you to try out the tool and give feedback.
|
2015-06-27 02:31:00 +08:00
|
|
|
|
2015-07-03 00:59:30 +08:00
|
|
|
### OCF
|
|
|
|
|
2015-09-02 00:32:29 +08:00
|
|
|
How does `runc` integrate with the Open Container Initiative Specification?
|
|
|
|
`runc` depends on the types specified in the
|
|
|
|
[specs](https://github.com/opencontainers/specs) repository. Whenever the
|
|
|
|
specification is updated and ready to be versioned `runc` will update its dependency
|
2015-07-03 00:59:30 +08:00
|
|
|
on the specs repository and support the update spec.
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
### Building:
|
|
|
|
|
2015-08-27 15:50:46 +08:00
|
|
|
At the time of writing, runc only builds on the Linux platform.
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
```bash
|
2015-07-08 19:10:22 +08:00
|
|
|
# create a 'github.com/opencontainers' in your GOPATH/src
|
2015-06-24 12:25:49 +08:00
|
|
|
cd github.com/opencontainers
|
|
|
|
git clone https://github.com/opencontainers/runc
|
|
|
|
cd runc
|
2015-06-22 10:31:12 +08:00
|
|
|
make
|
|
|
|
sudo make install
|
|
|
|
```
|
|
|
|
|
2015-08-22 06:25:26 +08:00
|
|
|
In order to enable seccomp support you will need to install libseccomp on your platform.
|
|
|
|
If you do not with to build `runc` with seccomp support you can add `BUILDTAGS=""` when running make.
|
|
|
|
|
2015-09-12 03:11:24 +08:00
|
|
|
#### Build Tags
|
|
|
|
|
|
|
|
`runc` supports optional build tags for compiling in support for various features.
|
|
|
|
|
|
|
|
|
|
|
|
| Build Tag | Feature | Dependency |
|
|
|
|
|-----------|------------------------------------|-------------|
|
|
|
|
| seccomp | Syscall filtering | libseccomp |
|
|
|
|
| selinux | selinux process and mount labeling | <none> |
|
|
|
|
| apparmor | apparmor profile support | libapparmor |
|
|
|
|
|
2015-09-15 08:30:28 +08:00
|
|
|
### Testing:
|
|
|
|
|
|
|
|
You can run tests for runC by using command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
# make test
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that test cases are run in Docker container, so you need to install
|
|
|
|
`docker` first. And test requires mounting cgroups inside container, it's
|
|
|
|
done by docker now, so you need a docker version newer than 1.8.0-rc2.
|
|
|
|
|
|
|
|
You can also run specific test cases by:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
# make test TESTFLAGS="-run=SomeTestFunction"
|
|
|
|
```
|
2015-09-12 03:11:24 +08:00
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
### Using:
|
|
|
|
|
2015-08-18 09:30:17 +08:00
|
|
|
To run a container, execute `runc start` in the bundle's root directory:
|
|
|
|
```bash
|
|
|
|
runc start
|
|
|
|
/ $ ps
|
|
|
|
PID USER COMMAND
|
|
|
|
1 daemon sh
|
|
|
|
5 daemon sh
|
|
|
|
/ $
|
|
|
|
```
|
2015-06-22 10:31:12 +08:00
|
|
|
|
2015-09-02 00:32:29 +08:00
|
|
|
### OCI Container JSON Format:
|
2015-06-22 10:31:12 +08:00
|
|
|
|
2015-10-27 15:34:38 +08:00
|
|
|
OCI container JSON format is based on OCI [specs](https://github.com/opencontainers/specs).
|
|
|
|
You can generate JSON files by using `runc spec`, it'll generate `config.json`
|
|
|
|
and `runtime.json`. It assumes that the file-system is found in a directory called
|
|
|
|
`rootfs` and there is a user with uid and gid of `0` defined within that file-system.
|
|
|
|
|
|
|
|
Below are sample `config.json` and `runtime.json` configuration files. Note that it
|
|
|
|
could be outdated, please always create base JSON files by `runc spec`.
|
2015-06-24 12:25:49 +08:00
|
|
|
|
2015-09-02 00:32:29 +08:00
|
|
|
`config.json`:
|
|
|
|
```json
|
|
|
|
{
|
2015-10-27 15:34:38 +08:00
|
|
|
"version": "0.2.0",
|
2015-09-02 00:32:29 +08:00
|
|
|
"platform": {
|
|
|
|
"os": "linux",
|
|
|
|
"arch": "amd64"
|
|
|
|
},
|
|
|
|
"process": {
|
|
|
|
"terminal": true,
|
|
|
|
"user": {
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0,
|
|
|
|
"additionalGids": null
|
|
|
|
},
|
|
|
|
"args": [
|
|
|
|
"sh"
|
|
|
|
],
|
|
|
|
"env": [
|
|
|
|
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
|
|
|
"TERM=xterm"
|
|
|
|
],
|
|
|
|
"cwd": ""
|
|
|
|
},
|
|
|
|
"root": {
|
|
|
|
"path": "rootfs",
|
|
|
|
"readonly": true
|
|
|
|
},
|
|
|
|
"hostname": "shell",
|
|
|
|
"mounts": [
|
|
|
|
{
|
|
|
|
"name": "proc",
|
|
|
|
"path": "/proc"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "dev",
|
|
|
|
"path": "/dev"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "devpts",
|
|
|
|
"path": "/dev/pts"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "shm",
|
|
|
|
"path": "/dev/shm"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "mqueue",
|
|
|
|
"path": "/dev/mqueue"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "sysfs",
|
|
|
|
"path": "/sys"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "cgroup",
|
|
|
|
"path": "/sys/fs/cgroup"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"linux": {
|
|
|
|
"capabilities": [
|
2015-09-10 07:26:26 +08:00
|
|
|
"CAP_AUDIT_WRITE",
|
|
|
|
"CAP_KILL",
|
|
|
|
"CAP_NET_BIND_SERVICE"
|
2015-09-16 10:25:38 +08:00
|
|
|
]
|
2015-09-02 00:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
`runtime.json`:
|
2015-06-22 10:31:12 +08:00
|
|
|
```json
|
|
|
|
{
|
2015-09-02 00:32:29 +08:00
|
|
|
"mounts": {
|
2015-10-27 15:34:38 +08:00
|
|
|
"cgroup": {
|
|
|
|
"type": "cgroup",
|
|
|
|
"source": "cgroup",
|
|
|
|
"options": [
|
|
|
|
"nosuid",
|
|
|
|
"noexec",
|
|
|
|
"nodev",
|
|
|
|
"relatime",
|
|
|
|
"ro"
|
|
|
|
]
|
2015-09-02 00:32:29 +08:00
|
|
|
},
|
|
|
|
"dev": {
|
|
|
|
"type": "tmpfs",
|
|
|
|
"source": "tmpfs",
|
|
|
|
"options": [
|
|
|
|
"nosuid",
|
|
|
|
"strictatime",
|
|
|
|
"mode=755",
|
|
|
|
"size=65536k"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"devpts": {
|
|
|
|
"type": "devpts",
|
|
|
|
"source": "devpts",
|
|
|
|
"options": [
|
|
|
|
"nosuid",
|
|
|
|
"noexec",
|
|
|
|
"newinstance",
|
|
|
|
"ptmxmode=0666",
|
|
|
|
"mode=0620",
|
|
|
|
"gid=5"
|
|
|
|
]
|
|
|
|
},
|
2015-10-27 15:34:38 +08:00
|
|
|
"mqueue": {
|
|
|
|
"type": "mqueue",
|
|
|
|
"source": "mqueue",
|
|
|
|
"options": [
|
|
|
|
"nosuid",
|
|
|
|
"noexec",
|
|
|
|
"nodev"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"proc": {
|
|
|
|
"type": "proc",
|
|
|
|
"source": "proc",
|
|
|
|
"options": null
|
|
|
|
},
|
2015-09-02 00:32:29 +08:00
|
|
|
"shm": {
|
|
|
|
"type": "tmpfs",
|
|
|
|
"source": "shm",
|
|
|
|
"options": [
|
|
|
|
"nosuid",
|
|
|
|
"noexec",
|
|
|
|
"nodev",
|
|
|
|
"mode=1777",
|
|
|
|
"size=65536k"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"sysfs": {
|
|
|
|
"type": "sysfs",
|
|
|
|
"source": "sysfs",
|
|
|
|
"options": [
|
|
|
|
"nosuid",
|
|
|
|
"noexec",
|
|
|
|
"nodev"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"hooks": {
|
|
|
|
"prestart": null,
|
2015-10-27 15:34:38 +08:00
|
|
|
"poststart": null,
|
2015-09-02 00:32:29 +08:00
|
|
|
"poststop": null
|
|
|
|
},
|
|
|
|
"linux": {
|
|
|
|
"uidMappings": null,
|
|
|
|
"gidMappings": null,
|
|
|
|
"rlimits": [
|
|
|
|
{
|
2015-09-16 10:25:38 +08:00
|
|
|
"type": "RLIMIT_NOFILE",
|
2015-09-02 00:32:29 +08:00
|
|
|
"hard": 1024,
|
|
|
|
"soft": 1024
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"sysctl": null,
|
|
|
|
"resources": {
|
|
|
|
"disableOOMKiller": false,
|
|
|
|
"memory": {
|
|
|
|
"limit": 0,
|
|
|
|
"reservation": 0,
|
|
|
|
"swap": 0,
|
|
|
|
"kernel": 0,
|
|
|
|
"swappiness": -1
|
|
|
|
},
|
|
|
|
"cpu": {
|
|
|
|
"shares": 0,
|
|
|
|
"quota": 0,
|
|
|
|
"period": 0,
|
|
|
|
"realtimeRuntime": 0,
|
|
|
|
"realtimePeriod": 0,
|
|
|
|
"cpus": "",
|
|
|
|
"mems": ""
|
|
|
|
},
|
2015-09-16 10:25:38 +08:00
|
|
|
"pids": {
|
|
|
|
"limit": 0
|
|
|
|
},
|
2015-09-02 00:32:29 +08:00
|
|
|
"blockIO": {
|
|
|
|
"blkioWeight": 0,
|
2015-10-27 15:34:38 +08:00
|
|
|
"blkioLeafWeight": 0,
|
|
|
|
"blkioWeightDevice": null,
|
|
|
|
"blkioThrottleReadBpsDevice": null,
|
|
|
|
"blkioThrottleWriteBpsDevice": null,
|
|
|
|
"blkioThrottleReadIOPSDevice": null,
|
|
|
|
"blkioThrottleWriteIOPSDevice": null
|
2015-09-02 00:32:29 +08:00
|
|
|
},
|
|
|
|
"hugepageLimits": null,
|
|
|
|
"network": {
|
|
|
|
"classId": "",
|
|
|
|
"priorities": null
|
|
|
|
}
|
|
|
|
},
|
2015-09-16 10:25:38 +08:00
|
|
|
"cgroupsPath": "",
|
2015-09-02 00:32:29 +08:00
|
|
|
"namespaces": [
|
|
|
|
{
|
|
|
|
"type": "pid",
|
|
|
|
"path": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "network",
|
|
|
|
"path": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "ipc",
|
|
|
|
"path": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "uts",
|
|
|
|
"path": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "mount",
|
|
|
|
"path": ""
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"devices": [
|
|
|
|
{
|
|
|
|
"path": "/dev/null",
|
|
|
|
"type": 99,
|
|
|
|
"major": 1,
|
|
|
|
"minor": 3,
|
|
|
|
"permissions": "rwm",
|
|
|
|
"fileMode": 438,
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": "/dev/random",
|
|
|
|
"type": 99,
|
|
|
|
"major": 1,
|
|
|
|
"minor": 8,
|
|
|
|
"permissions": "rwm",
|
|
|
|
"fileMode": 438,
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": "/dev/full",
|
|
|
|
"type": 99,
|
|
|
|
"major": 1,
|
|
|
|
"minor": 7,
|
|
|
|
"permissions": "rwm",
|
|
|
|
"fileMode": 438,
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": "/dev/tty",
|
|
|
|
"type": 99,
|
|
|
|
"major": 5,
|
|
|
|
"minor": 0,
|
|
|
|
"permissions": "rwm",
|
|
|
|
"fileMode": 438,
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": "/dev/zero",
|
|
|
|
"type": 99,
|
|
|
|
"major": 1,
|
|
|
|
"minor": 5,
|
|
|
|
"permissions": "rwm",
|
|
|
|
"fileMode": 438,
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": "/dev/urandom",
|
|
|
|
"type": 99,
|
|
|
|
"major": 1,
|
|
|
|
"minor": 9,
|
|
|
|
"permissions": "rwm",
|
|
|
|
"fileMode": 438,
|
|
|
|
"uid": 0,
|
|
|
|
"gid": 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"apparmorProfile": "",
|
|
|
|
"selinuxProcessLabel": "",
|
|
|
|
"seccomp": {
|
|
|
|
"defaultAction": "SCMP_ACT_ALLOW",
|
2015-10-27 15:34:38 +08:00
|
|
|
"architectures": null,
|
2015-09-02 00:32:29 +08:00
|
|
|
"syscalls": []
|
|
|
|
},
|
|
|
|
"rootfsPropagation": ""
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Examples:
|
|
|
|
|
2015-06-29 15:08:16 +08:00
|
|
|
#### Using a Docker image (requires version 1.3 or later)
|
2015-06-24 12:25:49 +08:00
|
|
|
|
|
|
|
To test using Docker's `busybox` image follow these steps:
|
2015-06-29 08:47:00 +08:00
|
|
|
* Install `docker` and download the `busybox` image: `docker pull busybox`
|
2015-06-24 12:25:49 +08:00
|
|
|
* Create a container from that image and export its contents to a tar file:
|
|
|
|
`docker export $(docker create busybox) > busybox.tar`
|
|
|
|
* Untar the contents to create your filesystem directory:
|
|
|
|
```
|
|
|
|
mkdir rootfs
|
|
|
|
tar -C rootfs -xf busybox.tar
|
|
|
|
```
|
2015-10-27 15:34:38 +08:00
|
|
|
* Create `config.json` and `runtime.json` by using `runc spec`.
|
2015-08-18 09:30:17 +08:00
|
|
|
* Execute `runc start` and you should be placed into a shell where you can run `ps`:
|
2015-06-24 12:25:49 +08:00
|
|
|
```
|
2015-08-18 09:30:17 +08:00
|
|
|
$ runc start
|
2015-06-24 12:25:49 +08:00
|
|
|
/ # ps
|
|
|
|
PID USER COMMAND
|
|
|
|
1 root sh
|
|
|
|
9 root ps
|
|
|
|
```
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
#### Using runc with systemd
|
|
|
|
|
2015-10-28 16:21:39 +08:00
|
|
|
To use runc with systemd, you can create a unit file
|
|
|
|
`/usr/lib/systemd/system/minecraft.service` as below (edit your
|
|
|
|
own Description or WorkingDirectory or service name as you need).
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
```service
|
|
|
|
[Unit]
|
|
|
|
Description=Minecraft Build Server
|
|
|
|
Documentation=http://minecraft.net
|
|
|
|
After=network.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
CPUQuota=200%
|
|
|
|
MemoryLimit=1536M
|
2015-10-28 16:21:39 +08:00
|
|
|
ExecStart=/usr/local/bin/runc start
|
2015-06-22 10:31:12 +08:00
|
|
|
Restart=on-failure
|
|
|
|
WorkingDirectory=/containers/minecraftbuild
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
```
|
2015-10-28 16:21:39 +08:00
|
|
|
|
|
|
|
Make sure you have the bundle's root directory and JSON configs in
|
|
|
|
your WorkingDirectory, then use systemd commands to start the service:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
systemctl daemon-reload
|
|
|
|
systemctl start minecraft.service
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that if you use JSON configs by `runc spec`, you need to modify
|
|
|
|
`config.json` and change `process.terminal` to false so runc won't
|
|
|
|
create tty, because we can't set terminal from the stdin when using
|
|
|
|
systemd service.
|