Docker05_DockerFile介绍
Docker05_使用 dockerfile 挂载目录在 /home 目录下新建 docker-test-volume 目录,进入该目录在 /home/docker-test-volume 目录下,创建 dockerfile1 文件,写入以下内容:FROM centosVOLUME ["volume01","volume02"]CMD echo "----end----"CMD /bin/bash
·
使用 dockerfile 挂载目录
在 /home 目录下新建 docker-test-volume 目录,进入该目录
在 /home/docker-test-volume 目录下,创建 dockerfile1 文件,写入以下内容:
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
# 根据 dockerfile1 中的脚本创建镜像,镜像名为 docker-blu/centos 版本为1.0
[root@izbp10tup89om84qulgxbsz docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t docker-blu/centos:1.0 .
# 查看生成的镜像
[root@izbp10tup89om84qulgxbsz docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-blu/centos 1.0 65d304aadb42 43 seconds ago 215MB
# 启动自己创建的镜像
[root@izbp10tup89om84qulgxbsz docker-test-volume]# docker run -it 65d304aadb42 /bin/bash
# 查看目录,发现了挂载的目录 volume01 和 volume02
[root@c1a21b8c0d30 /]# ls -l
total 56
lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x 5 root root 360 Nov 18 11:31 dev
drwxr-xr-x 1 root root 4096 Nov 18 11:31 etc
drwxr-xr-x 2 root root 4096 May 11 2019 home
lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------ 2 root root 4096 Aug 9 21:40 lost+found
drwxr-xr-x 2 root root 4096 May 11 2019 media
drwxr-xr-x 2 root root 4096 May 11 2019 mnt
drwxr-xr-x 2 root root 4096 May 11 2019 opt
dr-xr-xr-x 97 root root 0 Nov 18 11:31 proc
dr-xr-x--- 2 root root 4096 Aug 9 21:40 root
drwxr-xr-x 11 root root 4096 Aug 9 21:40 run
lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 May 11 2019 srv
dr-xr-xr-x 13 root root 0 Nov 18 11:31 sys
drwxrwxrwt 7 root root 4096 Aug 9 21:40 tmp
drwxr-xr-x 12 root root 4096 Aug 9 21:40 usr
drwxr-xr-x 20 root root 4096 Aug 9 21:40 var
drwxr-xr-x 2 root root 4096 Nov 18 11:31 volume01
drwxr-xr-x 2 root root 4096 Nov 18 11:31 volume02
# 在 volume01 中创建一个文件
[root@c1a21b8c0d30 /]# cd volume01
[root@c1a21b8c0d30 volume01]# touch a.txt
# 查看容器id
[root@izbp10tup89om84qulgxbsz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1a21b8c0d30 65d304aadb42 "/bin/bash" 7 minutes ago Up 7 minutes festive_cerf
# 查看挂载点信息
[root@izbp10tup89om84qulgxbsz ~]# docker inspect c1a21b8c0d30
"Mounts": [
{
"Type": "volume",
"Name": "3dd6cd544b10b948ae38ce55a2b78c4a4c14945360867317fe8c7886e6141027",
"Source":"/var/lib/docker/volumes/3dd6cd544b10b948ae38ce55a2b78c4a4c14945360867317fe8c7886e6141027/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "61e4336db92d8dc970fbc407aa4bc19c5b059626745ef3e954f82c29af8e1342",
"Source": "/var/lib/docker/volumes/61e4336db92d8dc970fbc407aa4bc19c5b059626745ef3e954f82c29af8e1342/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
# 进入 volume01 挂载的主机目录,发现同步成功!
[root@izbp10tup89om84qulgxbsz ~]# cd /var/lib/docker/volumes/3dd6cd544b10b948ae38ce55a2b78c4a4c14945360867317fe8c7886e6141027/_data
[root@izbp10tup89om84qulgxbsz _data]# ls
a.txt
[root@izbp10tup89om84qulgxbsz _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1a21b8c0d30 65d304aadb42 "/bin/bash" 24 minutes ago Up 24 minutes festive_cerf
# 再启动一个容器,卷继承上一个容器
[root@izbp10tup89om84qulgxbsz _data]# docker run -it --name blu-centos02 --volumes-from c1a21b8c0d30 65d304aadb42 /bin/bash
# 进入 volume01 目录,发现了a.txt
[root@9740c6ea3675 volume01]# ls
a.txt
# 创建 b.txt
[root@9740c6ea3675 volume01]# touch b.txt
# 在之前的容器中发现了b.txt,同步成功!
[root@c1a21b8c0d30 volume01]# ls
a.txt b.txt
实战:使用数据卷容器实现多个MySQL的数据同步
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
docker run -d -p 3311:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7
DockerFile
-
DockerFile 就是一个命令参数脚本,是用来构建docker镜像的文件
-
每个保留关键字(指令)都必须是大写字母
-
# 号表示注释
-
每一个指令都会创建提交一个新的镜像层
DockerFile 的指令
FROM # 指定基础镜像
MAINTAINER # 指定维护者信息(姓名+邮箱)
RUN # 镜像构建时需要运行的命令
ADD # 在镜像中添加内容
WORKDIR # 设置当前工作目录
VOLUME # 设置卷,挂载主机目录
EXPOSE # 指定暴露端口
CMD # 指定容器启动时要运行的命令(只有最后一个CMD指令会生效)
ENTRYPOINT # 指定容器启动时要运行的命令,启动时可以追加命令参数
ONBUILD # 当构建一个被继承 DockerFile 时会执行的指令
COPY # 类似ADD,将文件拷贝至镜像中
ENV # 构建时设置环境变量
使用 DockerFile 创建一个自己的 CentOS 镜像:
[root@blu dockerfile]# vim mydockerfile-centos
FROM centos
MAINTAINER BLU<2711978737@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
[root@blu dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
docker history 命令查看指定镜像的构建过程(以mysql:5.7为例):
[root@blu ~]# docker history mysql:5.7
IMAGE CREATED CREATED BY SIZE COMMENT
c791733bf9b7 4 days ago /bin/sh -c #(nop) CMD ["mysqld"] 0B
<missing> 4 days ago /bin/sh -c #(nop) EXPOSE 3306 33060 0B
<missing> 4 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 4 days ago /bin/sh -c ln -s usr/local/bin/docker-entryp… 34B
<missing> 4 days ago /bin/sh -c #(nop) COPY file:f9202f6b715c0e78… 13.1kB
<missing> 4 days ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0B
<missing> 4 days ago /bin/sh -c { echo mysql-community-server m… 313MB
<missing> 4 days ago /bin/sh -c echo "deb http://repo.mysql.com/a… 55B
<missing> 4 days ago /bin/sh -c #(nop) ENV MYSQL_VERSION=5.7.32-… 0B
<missing> 4 days ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=5.7 0B
<missing> 4 days ago /bin/sh -c set -ex; key='A4A9406876FCBD3C45… 2.61kB
<missing> 4 days ago /bin/sh -c apt-get update && apt-get install… 52.2MB
<missing> 4 days ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0B
<missing> 4 days ago /bin/sh -c set -eux; savedAptMark="$(apt-ma… 4.17MB
<missing> 4 days ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0B
<missing> 4 days ago /bin/sh -c apt-get update && apt-get install… 9.34MB
<missing> 4 days ago /bin/sh -c groupadd -r mysql && useradd -r -… 329kB
<missing> 4 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 4 days ago /bin/sh -c #(nop) ADD file:d2abb0e4e7ac17737… 69.2MB
实战:使用 DockerFile 创建一个 Tomcat 镜像:
FROM centos
MAINTAINER BLU<2711978737@qq.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u271-linux-aarch64.tar.gz /usr/local/
ADD apache-tomcat-9.0.34.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN chmod -R 777 /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_271
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.34
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.34
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.34/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.34/logs/catalina.out
[root@blu tomcat]# docker build -t diytomcat .
[root@blu blu]# docker run -d -p 3001:8080 --name blu-diytomcat -v /usr/blu/tomcat/test/webapps:/usr/local/apache-tomcat-9.0.34/webapps -v /usr/blu/tomcat/test/tomcatlogs:/usr/local/apache-tomcat-9.0.34/logs diytomcat
更多推荐
所有评论(0)