aboutsummaryrefslogtreecommitdiffstats
path: root/docs/usecases/containerCreation.rst
blob: fb38b3ed13500eaec3d61ba65a08ea9a4f2aaf2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
.. _containerCreation:

.. toctree::

Creating Containers
___________________

First you should have root privileges:

.. code-block:: shell

  ~$ sudo bash

Then install packages for containers such as lxc:

.. code-block:: shell

  # apt-get install bridge-utils lxc

As 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.
A 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."

"That *default.conf* file is either located at /etc/lxc/default.conf or for unprivileged containers at ~/.config/lxc/default.conf."

Since we want to ping between two containers, we'll need to **add to this file**.

Look at the contents of *default.conf*, which should initially look like this:

.. code-block:: shell
    
    # cat /etc/lxc/default.conf
    lxc.network.type = veth
    lxc.network.link = lxcbr0
    lxc.network.flags = up
    lxc.network.hwaddr = 00:16:3e:xx:xx:xx

As you can see, by default there is one veth interface.

Now 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.

You 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. 

.. code-block:: shell

    # echo -e "lxc.network.name = veth0\nlxc.network.type = veth\nlxc.network.name = veth_link1"  | sudo tee -a /etc/lxc/default.conf

Inspect the contents again to verify the file was indeed modified:

.. code-block:: shell

    # cat /etc/lxc/default.conf
    lxc.network.type = veth
    lxc.network.link = lxcbr0
    lxc.network.flags = up
    lxc.network.hwaddr = 00:16:3e:xx:xx:xx
    lxc.network.name = veth0
    lxc.network.type = veth
    lxc.network.name = veth_link1


After this, we're ready to create the containers.

Creates an Ubuntu Xenial container named "cone".

.. code-block:: shell

      # lxc-create -t download -n cone -- --dist ubuntu --release xenial --arch amd64 --keyserver hkp://p80.pool.sks-keyservers.net:80


If successful, you'll get an output similar to this:

.. code-block:: console
    
    You just created an Ubuntu xenial amd64 (20180625_07:42) container.

    To enable SSH, run: apt install openssh-server
    No default root or user password are set by LXC.


Make another container "ctwo".

.. code-block:: shell

     # lxc-create -t download -n ctwo -- --dist ubuntu --release xenial --arch amd64 --keyserver hkp://p80.pool.sks-keyservers.net:80


List your containers to verify they exist:


.. code-block:: shell

     # lxc-ls
     cone ctwo


Start the first container:

.. code-block:: shell
    
    # lxc-start --name cone

And verify its running:

.. code-block:: shell
    
    # lxc-ls --fancy
    NAME STATE   AUTOSTART GROUPS IPV4 IPV6 
    cone RUNNING 0         -      -    -    
    ctwo STOPPED 0         -      -    -  


.. note::

    Here are some `lxc container commands <https://help.ubuntu.com/lts/serverguide/lxc.html.en-GB#lxc-basic-usage>`_ you may find useful:


    .. code-block:: shell

          sudo lxc-ls --fancy
          sudo lxc-start --name u1 --daemon
          sudo lxc-info --name u1
          sudo lxc-stop --name u1
          sudo lxc-destroy --name u1