Docker-02-Docker basic mount the directory of the host to the directory of the container

Recently, I encountered a problem. What should I do if the disk space of the Docker container I created is not enough? Of course, the directory of the host is attached to the container.

[root@e41a457a3580 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
overlay                   50G   47G  3.6G  93% /
tmpfs                     64M     0   64M   0% /dev
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G   47G  3.6G  93% /jason
shm                       64M     0   64M   0% /dev/shm
tmpfs                     16G     0   16G   0% /proc/asound
tmpfs                     16G     0   16G   0% /proc/acpi
tmpfs                     16G     0   16G   0% /proc/scsi
tmpfs                     16G     0   16G   0% /sys/firmware

In order to alleviate the lack of root directory space, we need to mount the directory of the host computer, such as the / data1/test file to the / data directory of the container. For example, if there is no / data1/test directory on the host computer, the directory will be created automatically.

[root@localhost ~]# docker run -d -p 2325:22 -p 2326:80 -v /data1/test/:/data/ --name test_no_1 centos7 /usr/sbin/sshd -D
3d6bb6e6c4b308fb8fd9b6722d82033aa77a1dc7fdaa09d204962365c9a224fe

Where - p specifies the mapping port. When multiple ports need to be specified, you can use the - p option multiple times

-v specifies the directory mount, where / data1/test represents the directory of the host and / data represents the directory of the container

[root@localhost ~]# ssh 192.168.0.252 -p 2325
root@192.168.0.252's password: 
Last login: Mon May  6 10:45:39 2019

Log in to the container to view disk information

[root@3d6bb6e6c4b3 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
overlay                   50G   47G  3.6G  93% /
tmpfs                     64M     0   64M   0% /dev
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/sdb1                2.8T   29G  2.6T   2% /data
/dev/mapper/centos-root   50G   47G  3.6G  93% /etc/hosts
shm                       64M     0   64M   0% /dev/shm
tmpfs                     16G     0   16G   0% /proc/asound
tmpfs                     16G     0   16G   0% /proc/acpi
tmpfs                     16G     0   16G   0% /proc/scsi
tmpfs                     16G     0   16G   0% /sys/firmware

Create a file on the host

[root@localhost test]# echo "this is the file created on the host" > test.txt
[root@localhost test]# ll
total 4
-rw-r--r-- 1 root root 40 Jul  2 19:53 test.txt

See if there is data in the container

[root@3d6bb6e6c4b3 ~]# ll /data
total 4
-rw-r--r-- 1 root root 40 Jul  2 19:53 test.txt
[root@3d6bb6e6c4b3 ~]# cat /data/test.txt 
//This is a file created on the host

Summary: generally, the mount directory needs to be mounted when creating a container. It cannot be mounted after creation. You can expand the capacity of the container by expanding the capacity. However, it is complex and may have problems in centos7 system. It is not recommended.

Tags: Docker CentOS ssh

Posted on Fri, 01 Nov 2019 13:09:04 -0700 by FMB