CentOS 7.x create lxc container

From Notes_Wiki

Home > CentOS > CentOS 7.x > Virtualization > Lxc > 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. Optionally change DNS to 4.2.2.2, 8.8.8.8 that work everywhere to avoid issue mentioned at CentOS 7.x lxc container creation fails due to host resolution
  3. 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'
  4. 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
  5. Check existing containers using:
    lxc-ls
  6. Check running containers using:
    lxc-ls --active
  7. 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
  8. Attach console to container using:
    lxc-console -n lxc_centos -t 0
    Disconnect from this console using Ctrl^a+q
  9. To get information on container use:
    lxc-info -n centos_lxc
  10. To stop container use:
    lxc-stop -n lxc_centos
    Note that this takes long time
  11. 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
  12. Containers can be cloned using:
    lxc-clone mycentos6 mycentos6-2
    Note that this requires container to be stopped first.
  13. Containers can be destroyed using
    lxc-destroy -n mycentos6


Create debian container

To create debian container use:

  1. Optionally change DNS to 4.2.2.2, 8.8.8.8 that work everywhere to avoid issue mentioned at CentOS 7.x lxc container creation fails due to host resolution
  2. 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.
  3. Start container using:
    lxc-start -n mydeb -d
  4. Attach console using:
    lxc-console -n mydeb -t 0
    Login using root:root
  5. 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. Optionally change DNS to 4.2.2.2, 8.8.8.8 that work everywhere to avoid issue mentioned at CentOS 7.x lxc container creation fails due to host resolution
  5. 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.
  6. 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
  7. Start container using:
    lxc-start -n myubuntu -d
  8. Connect to console using:
    lxc-console -n myubuntu -t 0
    Login using ubuntu:ubuntu. You can access root prompt using:
    sudo su -
  9. Exit from container using Ctrl^a+q


Home > CentOS > CentOS 7.x > Virtualization > Lxc > CentOS 7.x create lxc container