blob: fb38b3ed13500eaec3d61ba65a08ea9a4f2aaf2b [file] [log] [blame]
John DeNisco06dcd452018-07-26 12:45:10 -04001.. _containerCreation:
2
3.. toctree::
4
5Creating Containers
6___________________
7
8First you should have root privileges:
9
John DeNiscoa14c1662018-08-01 10:38:23 -040010.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040011
John DeNiscoa14c1662018-08-01 10:38:23 -040012 ~$ sudo bash
John DeNisco06dcd452018-07-26 12:45:10 -040013
14Then install packages for containers such as lxc:
15
John DeNiscoa14c1662018-08-01 10:38:23 -040016.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040017
18 # apt-get install bridge-utils lxc
19
20As quoted from the `lxc.conf manpage <https://linuxcontainers.org/it/lxc/manpages/man5/lxc.conf.5.html>`_, "container configuration is held in the config stored in the container's directory.
21A basic configuration is generated at container creation time with the default's recommended for the chosen template as well as extra default keys coming from the default.conf file."
22
23"That *default.conf* file is either located at /etc/lxc/default.conf or for unprivileged containers at ~/.config/lxc/default.conf."
24
25Since we want to ping between two containers, we'll need to **add to this file**.
26
27Look at the contents of *default.conf*, which should initially look like this:
28
John DeNiscoa14c1662018-08-01 10:38:23 -040029.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040030
John DeNiscoa14c1662018-08-01 10:38:23 -040031 # cat /etc/lxc/default.conf
John DeNisco06dcd452018-07-26 12:45:10 -040032 lxc.network.type = veth
33 lxc.network.link = lxcbr0
34 lxc.network.flags = up
35 lxc.network.hwaddr = 00:16:3e:xx:xx:xx
36
37As you can see, by default there is one veth interface.
38
39Now you will *append to this file* so that each container you create will have an interface for a Linux bridge and an unconsumed second interface.
40
41You can do this by piping *echo* output into *tee*, where each line is separated with a newline character *\\n* as shown below. Alternatively, you can manually add to this file with a text editor such as **vi**, but make sure you have root privileges.
42
John DeNiscoa14c1662018-08-01 10:38:23 -040043.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040044
45 # echo -e "lxc.network.name = veth0\nlxc.network.type = veth\nlxc.network.name = veth_link1" | sudo tee -a /etc/lxc/default.conf
46
47Inspect the contents again to verify the file was indeed modified:
48
John DeNiscoa14c1662018-08-01 10:38:23 -040049.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040050
John DeNiscoa14c1662018-08-01 10:38:23 -040051 # cat /etc/lxc/default.conf
John DeNisco06dcd452018-07-26 12:45:10 -040052 lxc.network.type = veth
53 lxc.network.link = lxcbr0
54 lxc.network.flags = up
55 lxc.network.hwaddr = 00:16:3e:xx:xx:xx
56 lxc.network.name = veth0
57 lxc.network.type = veth
58 lxc.network.name = veth_link1
59
60
61After this, we're ready to create the containers.
62
63Creates an Ubuntu Xenial container named "cone".
64
John DeNiscoa14c1662018-08-01 10:38:23 -040065.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040066
67 # lxc-create -t download -n cone -- --dist ubuntu --release xenial --arch amd64 --keyserver hkp://p80.pool.sks-keyservers.net:80
68
69
70If successful, you'll get an output similar to this:
71
72.. code-block:: console
73
74 You just created an Ubuntu xenial amd64 (20180625_07:42) container.
75
76 To enable SSH, run: apt install openssh-server
77 No default root or user password are set by LXC.
78
79
80Make another container "ctwo".
81
John DeNiscoa14c1662018-08-01 10:38:23 -040082.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040083
84 # lxc-create -t download -n ctwo -- --dist ubuntu --release xenial --arch amd64 --keyserver hkp://p80.pool.sks-keyservers.net:80
85
86
87List your containers to verify they exist:
88
89
John DeNiscoa14c1662018-08-01 10:38:23 -040090.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040091
92 # lxc-ls
93 cone ctwo
94
95
96Start the first container:
97
John DeNiscoa14c1662018-08-01 10:38:23 -040098.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -040099
100 # lxc-start --name cone
101
102And verify its running:
103
John DeNiscoa14c1662018-08-01 10:38:23 -0400104.. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -0400105
106 # lxc-ls --fancy
107 NAME STATE AUTOSTART GROUPS IPV4 IPV6
108 cone RUNNING 0 - - -
109 ctwo STOPPED 0 - - -
110
111
112.. note::
113
114 Here are some `lxc container commands <https://help.ubuntu.com/lts/serverguide/lxc.html.en-GB#lxc-basic-usage>`_ you may find useful:
115
116
John DeNiscoa14c1662018-08-01 10:38:23 -0400117 .. code-block:: shell
John DeNisco06dcd452018-07-26 12:45:10 -0400118
119 sudo lxc-ls --fancy
120 sudo lxc-start --name u1 --daemon
121 sudo lxc-info --name u1
122 sudo lxc-stop --name u1
123 sudo lxc-destroy --name u1