2015-03-30 13:31:41 +08:00
|
|
|
## nsinit
|
|
|
|
|
|
|
|
`nsinit` is a cli application which demonstrates the use of libcontainer.
|
|
|
|
It is able to spawn new containers or join existing containers.
|
|
|
|
|
|
|
|
### How to build?
|
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
First add the `libcontainer/vendor` into your GOPATH. It's because libcontainer
|
|
|
|
vendors all its dependencies, so it can be built predictably.
|
2015-03-30 13:31:41 +08:00
|
|
|
|
|
|
|
```
|
|
|
|
export GOPATH=$GOPATH:/your/path/to/libcontainer/vendor
|
|
|
|
```
|
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
Then get into the nsinit folder and get the imported file. Use `make` command
|
|
|
|
to make the nsinit binary.
|
2015-03-30 13:31:41 +08:00
|
|
|
|
|
|
|
```
|
|
|
|
cd libcontainer/nsinit
|
|
|
|
go get
|
|
|
|
make
|
|
|
|
```
|
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
We have finished compiling the nsinit package, but a root filesystem must be
|
|
|
|
provided for use along with a container configuration file.
|
2015-03-30 13:31:41 +08:00
|
|
|
|
|
|
|
Choose a proper place to run your container. For example we use `/busybox`.
|
|
|
|
|
|
|
|
```
|
|
|
|
mkdir /busybox
|
|
|
|
curl -sSL 'https://github.com/jpetazzo/docker-busybox/raw/buildroot-2014.11/rootfs.tar' | tar -xC /busybox
|
|
|
|
```
|
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
Then you may need to write a configuration file named `container.json` in the
|
|
|
|
`/busybox` folder. Environment, networking, and different capabilities for
|
|
|
|
the container are specified in this file. The configuration is used for each
|
|
|
|
process executed inside the container.
|
|
|
|
|
|
|
|
See the `sample_configs` folder for examples of what the container configuration
|
|
|
|
should look like.
|
2015-03-30 13:31:41 +08:00
|
|
|
|
|
|
|
```
|
|
|
|
cp libcontainer/sample_configs/minimal.json /busybox/container.json
|
|
|
|
cd /busybox
|
|
|
|
```
|
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
You can customize `container.json` per your needs. After that, nsinit is
|
|
|
|
ready to work.
|
|
|
|
|
|
|
|
To execute `/bin/bash` in the current directory as a container just run the
|
|
|
|
following **as root**:
|
|
|
|
|
2015-03-30 13:31:41 +08:00
|
|
|
```bash
|
2015-04-01 13:50:48 +08:00
|
|
|
nsinit exec --tty --config container.json /bin/bash
|
2015-03-30 13:31:41 +08:00
|
|
|
```
|
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
If you wish to spawn another process inside the container while your current
|
|
|
|
bash session is running, run the same command again to get another bash shell
|
|
|
|
(or change the command). If the original process (PID 1) dies, all other
|
|
|
|
processes spawned inside the container will be killed and the namespace will
|
|
|
|
be removed.
|
2015-03-30 13:31:41 +08:00
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
You can identify if a process is running in a container by looking to see if
|
|
|
|
`state.json` is in the root of the directory.
|
2015-03-30 13:31:41 +08:00
|
|
|
|
2015-04-01 13:50:48 +08:00
|
|
|
You may also specify an alternate root directory from where the `container.json`
|
|
|
|
file is read and where the `state.json` file will be saved.
|