1lxc(7) lxc(7)
2
3
4
6 lxc - linux containers
7
9 The container technology is actively being pushed into the mainstream
10 Linux kernel. It provides resource management through control groups
11 and resource isolation via namespaces.
12
13 lxc, aims to use these new functionalities to provide a userspace con‐
14 tainer object which provides full resource isolation and resource con‐
15 trol for an applications or a full system.
16
17 lxc is small enough to easily manage a container with simple command
18 lines and complete enough to be used for other purposes.
19
21 The kernel version >= 3.10 shipped with the distros, will work with
22 lxc, this one will have less functionalities but enough to be interest‐
23 ing.
24
25 lxc relies on a set of functionalities provided by the kernel. The
26 helper script lxc-checkconfig will give you information about your ker‐
27 nel configuration, required, and missing features.
28
30 A container is an object isolating some resources of the host, for the
31 application or system running in it.
32
33 The application / system will be launched inside a container specified
34 by a configuration that is either initially created or passed as a
35 parameter of the commands.
36
37 How to run an application in a container
38
39 Before running an application, you should know what are the resources
40 you want to isolate. The default configuration is to isolate PIDs, the
41 sysv IPC and mount points. If you want to run a simple shell inside a
42 container, a basic configuration is needed, especially if you want to
43 share the rootfs. If you want to run an application like sshd, you
44 should provide a new network stack and a new hostname. If you want to
45 avoid conflicts with some files eg. /var/run/httpd.pid, you should
46 remount /var/run with an empty directory. If you want to avoid the con‐
47 flicts in all the cases, you can specify a rootfs for the container.
48 The rootfs can be a directory tree, previously bind mounted with the
49 initial rootfs, so you can still use your distro but with your own /etc
50 and /home
51
52 Here is an example of directory tree for sshd:
53
54
55 [root@lxc sshd]$ tree -d rootfs
56
57 rootfs
58 |-- bin
59 |-- dev
60 | |-- pts
61 | `-- shm
62 | `-- network
63 |-- etc
64 | `-- ssh
65 |-- lib
66 |-- proc
67 |-- root
68 |-- sbin
69 |-- sys
70 |-- usr
71 `-- var
72 |-- empty
73 | `-- sshd
74 |-- lib
75 | `-- empty
76 | `-- sshd
77 `-- run
78 `-- sshd
79
80
81 and the mount points file associated with it:
82
83 [root@lxc sshd]$ cat fstab
84
85 /lib /home/root/sshd/rootfs/lib none ro,bind 0 0
86 /bin /home/root/sshd/rootfs/bin none ro,bind 0 0
87 /usr /home/root/sshd/rootfs/usr none ro,bind 0 0
88 /sbin /home/root/sshd/rootfs/sbin none ro,bind 0 0
89
90
91 How to run a system in a container
92
93 Running a system inside a container is paradoxically easier than run‐
94 ning an application. Why? Because you don't have to care about the
95 resources to be isolated, everything needs to be isolated, the other
96 resources are specified as being isolated but without configuration
97 because the container will set them up. eg. the ipv4 address will be
98 setup by the system container init scripts. Here is an example of the
99 mount points file:
100
101 [root@lxc debian]$ cat fstab
102
103 /dev /home/root/debian/rootfs/dev none bind 0 0
104 /dev/pts /home/root/debian/rootfs/dev/pts none bind 0 0
105
106
107 CONTAINER LIFE CYCLE
108 When the container is created, it contains the configuration informa‐
109 tion. When a process is launched, the container will be starting and
110 running. When the last process running inside the container exits, the
111 container is stopped.
112
113 In case of failure when the container is initialized, it will pass
114 through the aborting state.
115
116 ---------
117 | STOPPED |<---------------
118 --------- |
119 | |
120 start |
121 | |
122 V |
123 ---------- |
124 | STARTING |--error- |
125 ---------- | |
126 | | |
127 V V |
128 --------- ---------- |
129 | RUNNING | | ABORTING | |
130 --------- ---------- |
131 | | |
132 no process | |
133 | | |
134 V | |
135 ---------- | |
136 | STOPPING |<------- |
137 ---------- |
138 | |
139 ---------------------
140
141
142
143 CONFIGURATION
144 The container is configured through a configuration file, the format of
145 the configuration file is described in lxc.conf(5)
146
147 CREATING / DESTROYING CONTAINERS
148 A persistent container object can be created via the lxc-create com‐
149 mand. It takes a container name as parameter and optional configuration
150 file and template. The name is used by the different commands to refer
151 to this container. The lxc-destroy command will destroy the container
152 object.
153
154 lxc-create -n foo
155 lxc-destroy -n foo
156
157
158 VOLATILE CONTAINER
159 It is not mandatory to create a container object before starting it.
160 The container can be directly started with a configuration file as
161 parameter.
162
163 STARTING / STOPPING CONTAINER
164 When the container has been created, it is ready to run an application
165 / system. This is the purpose of the lxc-execute and lxc-start com‐
166 mands. If the container was not created before starting the applica‐
167 tion, the container will use the configuration file passed as parameter
168 to the command, and if there is no such parameter either, then it will
169 use a default isolation. If the application ended, the container will
170 be stopped, but if needed the lxc-stop command can be used to stop the
171 container.
172
173 Running an application inside a container is not exactly the same thing
174 as running a system. For this reason, there are two different commands
175 to run an application into a container:
176
177 lxc-execute -n foo [-f config] /bin/bash
178 lxc-start -n foo [-f config] [/bin/bash]
179
180
181 The lxc-execute command will run the specified command into a container
182 via an intermediate process, lxc-init. This lxc-init after launching
183 the specified command, will wait for its end and all other reparented
184 processes. (to support daemons in the container). In other words, in
185 the container, lxc-init has PID 1 and the first process of the applica‐
186 tion has PID 2.
187
188 The lxc-start command will directly run the specified command in the
189 container. The PID of the first process is 1. If no command is speci‐
190 fied lxc-start will run the command defined in lxc.init.cmd or if not
191 set, /sbin/init .
192
193 To summarize, lxc-execute is for running an application and lxc-start
194 is better suited for running a system.
195
196 If the application is no longer responding, is inaccessible or is not
197 able to finish by itself, a wild lxc-stop command will kill all the
198 processes in the container without pity.
199
200 lxc-stop -n foo -k
201
202
203 CONNECT TO AN AVAILABLE TTY
204 If the container is configured with ttys, it is possible to access it
205 through them. It is up to the container to provide a set of available
206 ttys to be used by the following command. When the tty is lost, it is
207 possible to reconnect to it without login again.
208
209 lxc-console -n foo -t 3
210
211
212 FREEZE / UNFREEZE CONTAINER
213 Sometime, it is useful to stop all the processes belonging to a con‐
214 tainer, eg. for job scheduling. The commands:
215
216 lxc-freeze -n foo
217
218
219 will put all the processes in an uninteruptible state and
220
221 lxc-unfreeze -n foo
222
223
224 will resume them.
225
226 This feature is enabled if the freezer cgroup v1 controller is enabled
227 in the kernel.
228
229 GETTING INFORMATION ABOUT CONTAINER
230 When there are a lot of containers, it is hard to follow what has been
231 created or destroyed, what is running or what are the PIDs running in a
232 specific container. For this reason, the following commands may be use‐
233 ful:
234
235 lxc-ls -f
236 lxc-info -n foo
237
238
239 lxc-ls lists containers.
240
241 lxc-info gives information for a specific container.
242
243 Here is an example on how the combination of these commands allows one
244 to list all the containers and retrieve their state.
245
246 for i in $(lxc-ls -1); do
247 lxc-info -n $i
248 done
249
250
251 MONITORING CONTAINER
252 It is sometime useful to track the states of a container, for example
253 to monitor it or just to wait for a specific state in a script.
254
255 lxc-monitor command will monitor one or several containers. The parame‐
256 ter of this command accepts a regular expression for example:
257
258 lxc-monitor -n "foo|bar"
259
260
261 will monitor the states of containers named 'foo' and 'bar', and:
262
263 lxc-monitor -n ".*"
264
265
266 will monitor all the containers.
267
268 For a container 'foo' starting, doing some work and exiting, the output
269 will be in the form:
270
271 'foo' changed state to [STARTING]
272 'foo' changed state to [RUNNING]
273 'foo' changed state to [STOPPING]
274 'foo' changed state to [STOPPED]
275
276
277 lxc-wait command will wait for a specific state change and exit. This
278 is useful for scripting to synchronize the launch of a container or the
279 end. The parameter is an ORed combination of different states. The fol‐
280 lowing example shows how to wait for a container if it successfully
281 started as a daemon.
282
283
284 # launch lxc-wait in background
285 lxc-wait -n foo -s STOPPED &
286 LXC_WAIT_PID=$!
287
288 # this command goes in background
289 lxc-execute -n foo mydaemon &
290
291 # block until the lxc-wait exits
292 # and lxc-wait exits when the container
293 # is STOPPED
294 wait $LXC_WAIT_PID
295 echo "'foo' is finished"
296
297
298
299 CGROUP SETTINGS FOR CONTAINERS
300 The container is tied with the control groups, when a container is
301 started a control group is created and associated with it. The control
302 group properties can be read and modified when the container is running
303 by using the lxc-cgroup command.
304
305 lxc-cgroup command is used to set or get a control group subsystem
306 which is associated with a container. The subsystem name is handled by
307 the user, the command won't do any syntax checking on the subsystem
308 name, if the subsystem name does not exists, the command will fail.
309
310 lxc-cgroup -n foo cpuset.cpus
311
312
313 will display the content of this subsystem.
314
315 lxc-cgroup -n foo cpu.shares 512
316
317
318 will set the subsystem to the specified value.
319
321 lxc(7), lxc-create(1), lxc-copy(1), lxc-destroy(1), lxc-start(1), lxc-
322 stop(1), lxc-execute(1), lxc-console(1), lxc-monitor(1), lxc-wait(1),
323 lxc-cgroup(1), lxc-ls(1), lxc-info(1), lxc-freeze(1), lxc-unfreeze(1),
324 lxc-attach(1), lxc.conf(5)
325
327 Daniel Lezcano <daniel.lezcano@free.fr>
328
329 Christian Brauner <christian.brauner@ubuntu.com>
330
331 Serge Hallyn <serge@hallyn.com>
332
333 Stéphane Graber <stgraber@ubuntu.com>
334
335
336
337Version 3.2.1 2020-01-29 lxc(7)