CentOS 7.x create lxc container

From Notes_Wiki
Revision as of 05:36, 23 January 2017 by Saurabh (talk | contribs) (Created page with "<yambe:breadcrumb>CentOS_7.x_lxc|CentOS 7.x lxc</yambe:breadcrumb> =CentOS 7.x create lxc container= ==Create CentOS container== To create CentOS lxc containers use follo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<yambe:breadcrumb>CentOS_7.x_lxc|CentOS 7.x lxc</yambe:breadcrumb>

CentOS 7.x create lxc container

Create CentOS container

To create CentOS lxc containers use following steps:

  1. Checking existing container templates using:
    ls /usr/share/lxc/templates/
  2. Create container using:
    lxc-create -n centos_lxc -t centos
    where, centos_lxc is the container name and centos is name of template.
    If this is first container with chosen template then the command might download the packages first. These packages are cached in '/var/cache/lxc' for future use to avoid re-download. The container itself gets created in '/var/lib/lxc'
  3. Check container root password using:
    cat /var/lib/lxc/centos_lxc/tmp_root_pass
    Note that you would be forced to change root password on first login.
    Optionally reset container root password using:
    chroot /var/lib/lxc/centos_lxc/rootfs passwd
  4. Check existing containers using:
    lxc-ls
  5. Check running containers using:
    lxc-ls --active
  6. Start container in background using:
    lxc-start -n lxc_centos -d
    Do not try to start it in foreground as it would hang and you would have to kill it after password reset using:
    lxc-stop -n lxc_centos -k
  7. Attach console to container using:
    lxc-console -n lxc_centos -t 0
    Disconnect from this console using Ctrl^a+q
  8. To get information on container use:
    lxc-info -n centos_lxc
  9. To stop container use:
    lxc-stop -n lxc_centos
    Note that this takes long time
  10. Note that containers can be created using release and architecture values as:
    lxc-create -n mycentos6 -t download -- -d centos -r 6 -a i386
    For centos6 root password file is not created. Hence reset root password using:
    chroot /var/lib/lxc/mycentos6/rootfs
    before using container
  11. Containers can be cloned using:
    lxc-clone mycentos6 mycentos6-2
    Note that this requires container to be stopped first.
  12. Containers can be destroyed using
    lxc-destroy -n mycentos6


Create debian container

To create debian container use:

  1. Create lxc container
    lxc-create -n mydeb -t debian
    Note that this requires debootstrap package. The first download may take considerable time to build local package cache. If download fails then container creation might fail. In such cases simply run the above command again.
  2. Start container using:
    lxc-start -n mydeb -d
  3. Attach console using:
    lxc-console -n mydeb -t 0
    Login using root:root
  4. Detach from container using 'Ctrl^a'+q. Or shutdown container using:
    shutdown -h now
    and after shutdown console will detach automatically.


Create ubuntu container

To create ubuntu container use:

  1. First ensure following packages are installed:
    yum -y install debootstrap perl
  2. Create necessary symbolic links
    cd /usr/sbin; ln -sf debootstrap qemu-debootstrap
  3. Configure qemu-debootstrap by editing '/usr/sbin/qemu-debootstrap' and replacing following lines:
    DEF_MIRROR=”http://mirrors.kernel.org/ubuntu”
    DEF_HTTPS_MIRROR=”https://mirrors.kernel.org/ubuntu”
  4. Create ubuntu container using:
    lxc-create -n myubuntu -t ubuntu
    The first download may take time to build package cache. If download fails try above command again.
  5. To create container using release and architecture values use:
    lxc-create -n container_name -t container_template -- -r distro_release -a distro_architercture
    For example:
    lxc-create -n mywheezy -t debian -- -r wheezy -a amd64
  6. Start container using:
    lxc-start -n myubuntu -d
  7. Connect to console using:
    lxc-console -n myubuntu -t 0
    Login using ubuntu:ubuntu. You can access root prompt using:
    sudo su -
  8. Exit from container using Ctrl^a+q


<yambe:breadcrumb>CentOS_7.x_lxc|CentOS 7.x lxc</yambe:breadcrumb>