How to transfer the files between Linux containers and the host system on Ubuntu 21.04

To Transfer The Files Between Linux Containers And Host System On Ubuntu 21.04

Introduction:

Linux Containers provide OS-level virtualization through a virtual environment containing its own processes and network space, as well as resource isolation through control groups. LXC provides a userspace container object that provides full resource isolation and control for a system or an application based on these new functionalities.

Installation Procedure:

Step 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Launch the Cent OS 8 container by using the below command

root@linuxhelp:~# lxc launch images:centos/8/amd64 test1
Creating test1
Starting test1

Step 3: Launch the Ubuntu 22.04 container by using the below command

root@linuxhelp:~# lxc launch images:ubuntu/22.04/amd64 test2
Creating test2
Starting test2 

Step 4: List the available Linux container by using the below command

root@linuxhelp:~# lxc list
+-------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| NAME  |  STATE  |         IPV4          |                     IPV6                      |   TYPE    | SNAPSHOTS |

+-------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| test1 | RUNNING | 10.241.214.182 (eth0) | fd42:d310:820b:2411:216:3eff:fe21:be8 (eth0)  | CONTAINER | 0         |
+-------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| test2 | RUNNING | 10.241.214.140 (eth0) | fd42:d310:820b:2411:216:3eff:fedc:6f81 (eth0) | CONTAINER | 0         |
+-------+---------+-----------------------+-----------------------------------------------+-----------+-----------+

Step 5: Login to the test1 container’s Shell by using the below command

root@linuxhelp:~# lxc exec test1 bash

Step 6: Check the OS version by using the below command

[root@test1 ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"

Step 7: Exit from the test1 container

[root@test1 ~]# exit
exit

Step 8 Login to the test2 container’s Shell by using the below command

root@linuxhelp:~# lxc exec test2 bash 

Step 9: Check the OS version by using the below command

root@test2:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu Jammy Jellyfish (development branch)
Release:	22.04
Codename:	jammy

Step 10: Create a file named linuxhelp by using the below command

root@test2:~# touch linuxhelp

Step 11: Exit from the test1 container

root@test2:~# exit
exit

Step 12: Pull the file from container to host by using the below command

root@linuxhelp:~# lxc file pull test2/root/linuxhelp /root

Step 13: List the copied file by using the below command

root@linuxhelp:~# ls -la linuxhelp 
-rw-r--r-- 1 root root 0 Dec 31 15:10 linuxhelp

Step 14: Pull the file from host to container by using the below command

root@linuxhelp:~# lxc file push /root/linuxhelp test1/root/

Step 15: Login to the test2 container’s Shell by using the below command

root@linuxhelp:~# lxc exec test1 bash

Step 16: List the copied file by using the below command

[root@test1 ~]# ls -la
total 8
dr-xr-x---  2 root root   9 Dec 31 09:42 .
drwxr-xr-x 18 root root  23 Dec 31 09:35 ..
-rw-------  1 root root  26 Dec 31 09:38 .bash_history
-rw-r--r--  1 root root  18 May 11  2019 .bash_logout
-rw-r--r--  1 root root 176 May 11  2019 .bash_profile
-rw-r--r--  1 root root 176 May 11  2019 .bashrc
-rw-r--r--  1 root root 100 May 11  2019 .cshrc
-rw-r--r--  1 root root 129 May 11  2019 .tcshrc
-rw-r--r--  1 root root   0 Dec 31 09:42 linuxhelp

By this to transfer the files between Linux containers and host system on Ubuntu 21.04 has been completed.

FAQ
Q
How to list only active containers?
A
To list only active containers use command
Q
What is the location of LXC storage containers?
A
LXC storage containers are located under /var/lib/lxc for the root user.
Q
What are the two types of containers?
A
LXC has two types of containers, privileged containers, and unprivileged containers
Q
How to copy a file from the host system to the container?
A
To copy a file from host system to the container by using the following command lxc file push /
Q
How to copy a file from the container to the Host system?
A
To copy a file from the container to the Host system by using the following command lxc file pull /