58 and start service:: |
58 and start service:: |
59 |
59 |
60 $ sudo service lxc-net start |
60 $ sudo service lxc-net start |
61 $ sudo systemctl restart lxc-net.service |
61 $ sudo systemctl restart lxc-net.service |
62 |
62 |
|
63 Project file layout |
|
64 =================== |
|
65 |
|
66 * ``/usr/share/lxc/templates`` is list of templates. |
|
67 * ``/var/cache/lxc`` is cache for downloaded images. |
|
68 |
|
69 * ``~/.local/share/lxc`` container storage. |
|
70 * ``~/.config/lxc`` container configs. |
|
71 * ``~/.cache/lxc`` is cache for unprivileged downloded images. |
|
72 * ``~/.local/share/lxcsnaps`` is storage for unprivileged snapshots. |
|
73 |
63 Create new container in LXC |
74 Create new container in LXC |
64 =========================== |
75 =========================== |
65 |
76 |
66 Create container from template:: |
77 Create container from template:: |
67 |
78 |
134 |
145 |
135 Fix DNS resolution in container:: |
146 Fix DNS resolution in container:: |
136 |
147 |
137 $ echo nameserver 8.8.8.8 | sudo tee /var/lib/lxc/$NAME/rootfs/etc/resolv.conf |
148 $ echo nameserver 8.8.8.8 | sudo tee /var/lib/lxc/$NAME/rootfs/etc/resolv.conf |
138 |
149 |
|
150 Creating unpriviliged containers |
|
151 ================================ |
|
152 |
|
153 Check if user has assigned ids for namespaces:: |
|
154 |
|
155 $ cat /etc/subuid |
|
156 vagrant:100000:65536 |
|
157 $ cat /etc/subgid |
|
158 vagrant:100000:65536 |
|
159 |
|
160 If not assign ranges with:: |
|
161 |
|
162 sudo usermod -v 100000-165536 -w 100000-165536 vagrant |
|
163 |
|
164 or:: |
|
165 |
|
166 sudo usermod -add-subuids 100000-165536 vagrant |
|
167 sudo usermod -add-subgids 100000-165536 vagrant |
|
168 |
|
169 Create local config:: |
|
170 |
|
171 mkdir -p ~/.config/lxc |
|
172 cat <<EOF >~/.config/lxc/default.conf |
|
173 lxc.idmap = u 0 100000 65536 |
|
174 lxc.idmap = g 0 100000 65536 |
|
175 lxc.network.type = veth |
|
176 lxc.network.link = lxcbr0 |
|
177 EOF |
|
178 echo "$USER veth lxcbr0 2" | sudo tee -a /etc/lxc/lxc-usernet |
|
179 |
139 LXC Container management |
180 LXC Container management |
140 ======================== |
181 ======================== |
141 |
182 |
142 List available containers:: |
183 List available containers:: |
143 |
184 |
144 $ sudo lxc-ls |
185 $ sudo lxc-ls |
|
186 $ sudo lxc-ls -f |
145 $ sudo lxc-ls --fancy |
187 $ sudo lxc-ls --fancy |
146 |
188 |
147 Show details about container:: |
189 Show details about container:: |
148 |
190 |
149 $ sudo lxc-info --name $NAME |
191 $ sudo lxc-info --name $NAME |
174 |
216 |
175 Mark container to start on boot in ``/var/lib/lxc/$NAME/config``:: |
217 Mark container to start on boot in ``/var/lib/lxc/$NAME/config``:: |
176 |
218 |
177 lxc.start.auto = 1 |
219 lxc.start.auto = 1 |
178 |
220 |
|
221 Other autostart params:: |
|
222 |
|
223 lxc.start.delay = 15 # delay in seconds |
|
224 lxc.start.order = 50 # higher value means starts earlier |
|
225 |
|
226 Limit memory usage:: |
|
227 |
|
228 lxc.cgroup.memory.limit_in_bytes = 256M |
|
229 |
|
230 Apply memory limit on fly:: |
|
231 |
|
232 sudo lxc-cgroup -n $NAME memory.limit_in_bytes 100M |
|
233 |
179 Link containers in non standard location to take them in account:: |
234 Link containers in non standard location to take them in account:: |
180 |
235 |
181 $ ln -s /opt/lxc/$NAME /var/lib/lxc/$NAME |
236 $ ln -s /opt/lxc/$NAME /var/lib/lxc/$NAME |
|
237 |
|
238 Show container status/into:: |
|
239 |
|
240 $ sudo lxc-info -n $NAME |
|
241 |
|
242 Print just state:: |
|
243 |
|
244 $ sudo lxc-info -n $NAME -s |
|
245 $ sudo lxc-info -n $NAME --state |
|
246 |
|
247 Print just pid:: |
|
248 |
|
249 $ sudo lxc-info -n $NAME -p |
|
250 $ sudo lxc-info -n $NAME --pid |
|
251 |
|
252 Print just IP addresses:: |
|
253 |
|
254 $ sudo lxc-info -n $NAME -i |
|
255 $ sudo lxc-info -n $NAME --ips |
182 |
256 |
183 Run command in running container:: |
257 Run command in running container:: |
184 |
258 |
185 $ sudo lxc-attach -n $NAME |
259 $ sudo lxc-attach -n $NAME |
186 $ sudo lxc-attach -n $NAME bash |
260 $ sudo lxc-attach -n $NAME bash |
187 $ sudo lxc-attach -n $NAME -- ls -a |
261 $ sudo lxc-attach -n $NAME -- ls -a |
188 $ sudo lxc-attach -n $NAME -- apk list |
262 $ sudo lxc-attach -n $NAME -- apk list |
189 |
263 |
|
264 Open session in console (attaches container tty process to current terminal, prompt for |
|
265 user/password):: |
|
266 |
|
267 $ sudo lxc-console -n $NAME |
|
268 |
|
269 Freeze/unfreeze:: |
|
270 |
|
271 $ sudo lxc-freeze -n $NAME |
|
272 $ sudo lxc-unfreeze -n $NAME |
|
273 |
190 Making snapshot |
274 Making snapshot |
191 =============== |
275 =============== |
192 |
276 |
193 See ``lxc-snapshot(1)``. |
277 See ``lxc-snapshot(1)``. |
194 |
278 |
197 $ sudo lxc-snapshot --list |
281 $ sudo lxc-snapshot --list |
198 $ sudo lxc-snapshot -n $NAME --list |
282 $ sudo lxc-snapshot -n $NAME --list |
199 |
283 |
200 Make a snapshot:: |
284 Make a snapshot:: |
201 |
285 |
202 $ sudo lcx-halt -n $NAME |
286 $ sudo lxc-halt -n $NAME |
203 $ sudo lcx-snapshot -n $NAME |
287 $ sudo lxc-snapshot -n $NAME |
204 |
288 |
205 Restore from snapshot:: |
289 Restore from snapshot:: |
206 |
290 |
207 $ sudo lcx-halt -n $NAME |
291 $ sudo lxc-halt -n $NAME |
208 $ sudo lcx-snapshot -n $NAME -r $SNAPNAME |
292 $ sudo lxc-snapshot -n $NAME -r $SNAPNAME |
209 |
293 |