其他分享
首页 > 其他分享> > docker存储卷的使用

docker存储卷的使用

作者:互联网

docker存储卷

目录

cow机制

Docker镜像由多个只读层叠加而成,启动容器时,Docker会加载只读镜像层并在镜像栈顶部添加一个读写层。

如果运行中的容器修改了现有的一个已经存在的文件,那么该文件将会从读写层下面的只读层复制到读写层,该文件的只读版本依然存在,只是已经被读写层中该文件的副本所隐藏,这就是“写时复制(COW)”机制。

img

对于这种方式来说,我们去访问一个文件,修改和删除等一类的操作,其效率会非常的低,因为隔着很多层镜像。

而要想绕过这种限制,我们可以通过使用存储卷的机制来实现。

什么是存储卷

存储卷就是将宿主机的本地文件系统中存在的某个目录直接与容器内部的文件系统上的某一目录建立绑定关系。这就意味着,当我们在容器中的这个目录下写入数据时,容器会将其内容直接写入到宿主机上与此容器建立了绑定关系的目录。

img

在宿主机上的这个与容器形成绑定关系的目录被称作存储卷。

为什么要用存储卷

关闭并重启容器,其数据不受影响,但删除Docker容器,则其更改将会全部丢失。

因此Docker存在的问题有:

而要解决这些问题,解决方案就是使用存储卷。

docker存储卷应用,通过存储卷挂载到容器

安装docker,并配置镜像加速器

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

//将国外源修改成国内源
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost yum.repos.d]# dnf -y install docker-ce

//配置镜像加速器
[root@localhost yum.repos.d]# sudo mkdir -p /etc/docker
[root@localhost yum.repos.d]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://pyn8gzi5.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://pyn8gzi5.mirror.aliyuncs.com"]
}
[root@localhost yum.repos.d]# sudo systemctl daemon-reload
[root@localhost yum.repos.d]# sudo systemctl restart docker

//docker开机自启
[root@localhost yum.repos.d]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

基于容器的方式做一个apache编译安装的镜像

拉取centos8镜像,并生成进入容器

[root@localhost ~]# docker pull centos:8
8: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:8
docker.io/library/centos:8
[root@localhost ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       8         5d0da3dc9764   10 months ago   231MB
[root@localhost ~]# docker run -it --name httpd centos:8 /bin/bash
[root@2f2e94aca069 /]# 

在容器中配置阿里云的yum源和epel源

[root@2f2e94aca069 /]# cd /etc/yum.repos.d/
[root@2f2e94aca069 yum.repos.d]# rm -rf *

//配置yum源
[root@2f2e94aca069 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@2f2e94aca069 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

//配置epel源
[root@2f2e94aca069 yum.repos.d]# dnf install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@2f2e94aca069 yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@2f2e94aca069 yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@2f2e94aca069 yum.repos.d]# ls
CentOS-Base.repo   epel-testing-modular.repo  epel.repo
epel-modular.repo  epel-testing.repo

打开一个新的终端,下载apache的软件包复制到容器中

[root@localhost ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz http://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
[root@localhost ~]# docker cp apr-1.7.0.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp apr-util-1.6.1.tar.gz httpd:/usr/src/
[root@localhost ~]# docker cp httpd-2.4.54.tar.gz httpd:/usr/src/

去容器中查看是否复制成功

[root@2f2e94aca069 yum.repos.d]# cd /usr/src/
[root@2f2e94aca069 src]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  debug  httpd-2.4.54.tar.gz  kernels

下载相关依赖包,创建一个apache用户

[root@2f2e94aca069 src]# dnf -y install wget openssl-devel pcre-devel expat-devel libtool libxml2-devel gcc gcc-c++ make
[root@2f2e94aca069 src]# useradd -r -M -s /sbin/nologin apache
[root@2f2e94aca069 src]# id apache
uid=998(apache) gid=996(apache) groups=996(apache)

解压并编译安装apache相关软件包

//解压并编译apr
[root@2f2e94aca069 src]# tar xf apr-1.7.0.tar.gz 
[root@2f2e94aca069 src]# cd apr-1.7.0
[root@2f2e94aca069 apr-1.7.0]# vi configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    #$RM "$cfgfile"                  //注释这一行
[root@2f2e94aca069 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@2f2e94aca069 apr-1.7.0]# make && make install

//解压并编译apr-util
[root@2f2e94aca069 apr-1.7.0]# cd ..
[root@2f2e94aca069 src]# tar xf apr-util-1.6.1.tar.gz 
[root@2f2e94aca069 src]# cd apr-util-1.6.1
[root@2f2e94aca069 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@2f2e94aca069 apr-util-1.6.1]# make && make install

//解压并编译httpd
[root@2f2e94aca069 apr-util-1.6.1]# cd ..
[root@2f2e94aca069 src]# tar xf httpd-2.4.54.tar.gz 
[root@2f2e94aca069 src]# cd httpd-2.4.54
[root@2f2e94aca069 httpd-2.4.54]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-rpm=prefork
[root@2f2e94aca069 httpd-2.4.54]# make && make install

配置环境变量

[root@2f2e94aca069 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh
[root@2f2e94aca069 ~]# source /etc/profile.d/apache.sh

//创建软连接
[root@2f2e94aca069 ~]# ln -s /usr/local/apache/include/ /usr/include/apache

//vi进入后,/ServerName,找到下面此行取消注释
[root@2f2e94aca069 ~]# vi /usr/local/apache/conf/httpd.conf
#ServerName www.example.com:80

//启动80端口
[root@2f2e94aca069 ~]# apachectl start
[root@2f2e94aca069 ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*    

制作apache镜像,并上传

//制作镜像
[root@localhost ~]# docker commit -p -c 'CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]'  2f2e94aca069 a3171344634/httpd:v0.1
sha256:169b852685dcd09b08e051a18a31dde19ccb8dbba5a7f3ba342d4452bddc2267
[root@localhost ~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
a3171344634/httpd   v0.1      169b852685dc   18 seconds ago   763MB
centos              8         5d0da3dc9764   10 months ago    231MB

//上传镜像
[root@localhost ~]# docker login   //登录
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: a3171344634
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# docker push a3171344634/httpd:v0.1
The push refers to repository [docker.io/a3171344634/httpd]
3176bbf5fec3: Pushed 
74ddd0ec08fa: Mounted from library/centos 
v0.1: digest: sha256:418e4e4d903fe75959e23ad693261d42ba9ad5bc220113ff906cea5d8c99ca9c size: 742
[root@localhost ~]# 

image-20220811094033739

测试镜像,部署站点

//启动一个容器,指定容器目录的挂载点
[root@localhost ~]# docker run -d --name web --restart always -p 80:80 -v /httpd_html:/usr/local/apache/htdocs a3171344634/httpd:v0.1
51bb9399bfff6c9e62bab0ff1a8e20db016f3d8d141f7f5894b80416115cac1c
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                               NAMES
51bb9399bfff   a3171344634/httpd:v0.1   "/usr/local/apache/b…"   4 seconds ago   Up 3 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web

//把‘贪吃蛇游戏’源码上传到目录
[root@localhost ~]# cd /httpd_html/
[root@localhost httpd_html]# ls
SnakeHTMLgames_780cbae4.zip
[root@localhost httpd_html]# unzip SnakeHTMLgames_780cbae4.zip 
[root@localhost httpd_html]# rm -f SnakeHTMLgames_780cbae4.zip //删除源码包
[root@localhost httpd_html]# mv snake.html index.html    //设置主页面
[root@localhost httpd_html]# ls
'#U6700#U65b0Asp.Net#U6e90#U7801#U4e0b#U8f7d.url'   Snake.js     index.html
 51Aspx#U6e90#U7801#U5fc5#U8bfb.txt                 Stage.js
 Node.js                                           'from .gif'

浏览器测试访问
image

标签:httpd,存储,apr,2f2e94aca069,使用,docker,root,localhost
来源: https://www.cnblogs.com/Clannaddada/p/16575067.html