docker entry (C): data volume and container DockerFile script, use Ali cloud mirroring warehouse

A. Data volume container

For data retention and persistence, simple to understand, is that container and hosts share a common hard disk, for example, the original tomcat deploy applications, or view the log also need to enter the vessel, will now log folder and deploy the folder out mount to the host, you can view the log without entering the container or deployment project

1. Construction of direct data volume container

#直接把容器内文件夹挂在到主机文件夹上
docker run -it -v /宿主机绝对路径:/容器内目录 镜像名
#在主机根目录下创建MyDockerCentos文件夹,在centos容器中创建MyDockerCentos01文件夹
#挂载成功后,MyDockerCentos01文件夹内容即可共享在MyDockerCentos文件夹内的内容
#添加ro则该文件夹为只读
docker run -it -v /MyDockerCentos:/MyDockerCentos01 or centos

#通过容器元数据查看挂载情况
docker inspect

#在json中可以看到此段文字
...
"Mounts": [
            {
                "Type": "bind",
                "Source": "/MyDockerCentos", #此为主机挂载文件夹路径
                "Destination": "/MyDockerCentos01", #此为容器挂载文件夹路径
                "Mode": "",
                "RW": true, #若为只读,则"RW"为false
                "Propagation": "rprivate"
            }
        ],
...

2. Construction of the container volume data by DockerFile

About DockerFile, specific description below

2.1 write a script to build docker mirror

#在当前跟根目录新建mydockerfile
vim mydockerfile

#写入以下命令
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "create success!"
CMD /bin/bash

#注意:可以理解为
docker run -it -v /host1:/dataVolumeContainer1 -v /host2:/dataVolumeContainer1 centeros /bin/bash

2.2 execution dockerfile

#注意:命令最后有一个“ . ”,请务必带不要忘记
docker build -f /mydocker/mydockerfile -t [新镜像名字] .

#执行完后打印信息
[root@iZwz9ev02los7q1d71e7muZ mydocker]# docker build -f /mydocker/mydockerfile -t centos .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 9f38484d220f #此为文件中指定的镜像模板ID
Step 2/4 : VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
 ---> Running in 2b7657366a63
Removing intermediate container 2b7657366a63
 ---> abec456be739
Step 3/4 : CMD echo "create success!"
 ---> Running in 9e043eaa69dd
Removing intermediate container 9e043eaa69dd
 ---> ff5086933e91
Step 4/4 : CMD /bin/bash
 ---> Running in 046617fa2f7a
Removing intermediate container 046617fa2f7a
 ---> 0245ba1fc082 #根据此id新运行一个容器

#新运行一个容器
docker run -it 0245ba1fc082 /bin/bash

#查看根目录所有文件后可见
[root@2ca987ed7f70 /]# ll
...
drwxr-xr-x  2 root root  4096 Jul 31 15:06 dataVolumeContainer1 #dataVolumeContainer1
drwxr-xr-x  2 root root  4096 Jul 31 15:06 dataVolumeContainer2 #dataVolumeContainer2
...

2.3 Check data volume where

#查看该容器元信息
docker inspect 2ca987ed7f70
#可知当未指定文件夹在主机的路径时,则默认创建在/var/lib/docker/volumes目录下
...
"Mounts": [
            {
                "Type": "volume",
                "Name": "008c479311eb38775908647c6f89cebbed368107a418a385ee58b5461341db0b",
                "Source": "/var/lib/docker/volumes/008c479311eb38775908647c6f89cebbed368107a418a385ee58b5461341db0b/_data",
                "Destination": "/dataVolumeContainer2",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "fdbf4a0b6921b7950a214f8bf028cf00edad091870e523bd694a984cc87fac30",
                "Source": "/var/lib/docker/volumes/fdbf4a0b6921b7950a214f8bf028cf00edad091870e523bd694a984cc87fac30/_data",
                "Destination": "/dataVolumeContainer1",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
...

二.DockerFile

Introduction 1. DockerFile

Docker generated script mirrors, similar .bat batch file to build the container when the press will build script execution, if the write-related commands to configure pre-written on the inside of the environment, then after executing the script, you can already get a direct configure the container environment

2. DockerFile instruction

FROM #基础镜像,指定
MAINTAINER #镜像维护者信息
RUN #容器构建时需要运行的命令
EXPOSE #当前容器对外暴露的端口
WORKDIR #主机进入容器后的所在的默认路径位置
ENV #用于在构建镜像的过程中设置环境变量
COPY/ADD #将主机文件拷贝至镜像,若ADD则还会自动解压压缩包和出来URL
VOLUME #容器数据卷,用于数据保存和持久化
CMD #指定容器启动时要运行的命令(脚本中可以有多个,但是只有最后一个生效)
ENTRYPOINT #同CMD,但是不会被docker run之后的参数覆盖
ONBUILD #父镜像被子镜像继承后触发

3. dockerignore

#comment

#代表根目录(上下文环境目录中)中以abc开头的任意直接子目录或者直接子文件将被忽略
#如/abc  abc.txt
/abc*

#代表根目录(上下文环境目录中)中任意直接子目录中以abc开头的任意直接子目录或者直接子文件将被忽略
#如 /file/abc  /file/abc.txt
*/abc*

#代表根目录(上下文环境目录中)中任意两级目录下以abc开头的任意直接子目录或者直接子文件将被忽略
#如 /file1/file2/abc  /file1/file2/abc.txt
*/*/abc*

#排除根目录中的文件和目录,其名称是单字符扩展名temp。例如,/tempa与/tempb被排除在外。
temp?   

#Docker还支持一个**匹配任意数量目录(包括零)的特殊通配符字符串
**/abc*

#以!(感叹号)开头的行可用于对排除项进行例外处理,比如原本包含了README.md这个文件的过滤,但是加了如下一行后
#就不会再过滤README.md,依然会将其提交到守护进程。
!README.md

#异常规则的放置位置会影响行为
*.md
!README*.md
README-secret.md
#README-secret.md 仍然会被忽略
    
*.md
README-secret.md
!README*.md
#README-secret.md 不会被忽略

您甚至可以使用该.dockerignore文件来排除Dockerfile和.dockerignore文件。这些文件仍然发送到守护程序,因为它需要它们来完成它的工作。但是ADD和COPY命令不会将它们复制到图像中。

2.51. DockerFile using a custom image centos

2.5.1.1 write dockerfile

FORM centos

#ENV mypath /temp #设置一个环境变量
#WORKDIR $mypath #设置默认进入容器的路径,指向环境变量所规定的路径

RUN yum install vim -y #安装vim
RUN yum install tree -y #安装tree

EXPOSE 80 #暴露出80端口

CMD echo "success!"

CMD /bin/bash

2.5.1.2 Run the script

docker build -f /mydocker/dockerfile/mydockerfile2 -t mycentos .

#使用命令查看容器历史可见
docker history 22d8bbae3a6f

#顺序加载,倒序执行
IMAGE               CREATED             CREATED BY                                      SIZE   
22d8bbae3a6f        3 hours ago         /bin/sh -c yum install tree -y #安装tree          23MB         
b77e4249fba5        3 hours ago         /bin/sh -c yum install vim -y #安装vim            167MB       
9f38484d220f        4 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]              0B         
<missing>           4 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…     0B         
<missing>           4 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38c…     202MB 

Note: If you can not solve the problem of being given permission, you can enter a folder to run the program after dockerfile

Two five two. Centos with a custom tomcat and jdk

2.5.2.1 Scripting

FROM centos
MAINTAINER huang<[email protected]>
#把主机下的目录tomcat和jdk添加至容器,这里脚本与安装包在同一路径下
ADD apache-tomcat-8.5.43.tar.gz /usr/local
ADD jdk-8u221-linux-i586.tar.gz /usr/local
#安装插件
RUN yum install vim -y
RUN yum install wget -y 
RUN yum install tree -y
RUN yum install bash-completion -y
#设置访问时的容器路径
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置JDK和tomcat环境 
ENV JAVA_HOME /usr/local/jdk1.8.0_221
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.43
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.43
ENV PATH $PATH:$JAVA_HOME/lib:$CATALINA_HOME/lib:$$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat
CMD /usr/local/apache-tomcat-8.5.43/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.43/bin/logs/catalina.out

2.5.2.2 Run the script

docker build -f /mydocker/dockerfile/tomcat -t mytomcat .

#运行成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mytomcat            latest              71e04bed14cd        9 seconds ago       859MB

2.5.2.3 Start container

Note: Configure tomcat and jdk Reference "linux common operation"

#容器暴露端口,名称
docker run -it -p 8001:8080 --name mycat
#创建两个容器数据卷
-v /mydocker/dockersoft/tomcatfile/webapp:/usr/local/apache-tomcat-8.5.43/webapp
-v /mydocker/dockersoft/tomcatfile/logs:/usr/local/apache-tomcat-8.5.43/logs
--privileged=true
#镜像模板
mytomcat
#容器启动
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS           
edd612a7f92f        mytomcat            "/bin/sh -c '/usr/lo…"   24 seconds ago      Up 23 seconds  

PORTS                    NAMES
0.0.0.0:8001->8080/tcp   mycat

III. Use the cloud Ali Warehouse Management Mirror Mirror

Select a mirror image Home Warehouse Management warehouse options, follow the prompts to create a namespace and mirrors warehouse, and bind github account number or other code hosting platform

1. packaged mirror

#把成功运行的容器打包成镜像
docker commit -m="jdk+tomcat+centos" -a="huang" c0733d9e5807 mycentos:1.0
#查看镜像
Docker Registry
#可见新镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            1.0                 fe6434ec860e        6 seconds ago       923MB

2. Log Ali cloud Docker Registry

#用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码
sudo docker login --username=xxxxxxxx registry.cn-shenzhen.aliyuncs.com
xxxpasswordxxx

3. From the Registry pull / push mirroring

#拉取镜像
sudo docker pull registry.cn-shenzhen.aliyuncs.com/xiajibagao/depository01:[镜像版本号]
#推送镜像
sudo docker tag [ImageId] registry.cn-shenzhen.aliyuncs.com/xxxxxx/depository01:[镜像版本号]
sudo docker push registry.cn-shenzhen.aliyuncs.com/xxxxxx/depository01:[镜像版本号]

4. In view mirror Ali cloud

#查看公有镜像
进入阿里云镜像容器管理主界面,选择镜像中心的镜像搜索选项,搜索仓库
eg:xxxxxx/depository01

#查看私有镜像
进入阿里云镜像容器管理主界面,选中自己的仓库,点击镜像版本管理

Guess you like

Origin www.cnblogs.com/Createsequence/p/12344708.html