Dockerfiel介绍

dockerfile是用来构建镜像的文件,名参数脚本。
构建步骤
1.编写一个dockerfile文件
2.docker build 构建成为一个镜像
3.docker run 运行镜像
4.docker push 发布镜像 (DockerHub、阿里镜像仓库)
image.png
image.png

Dockerfile构建过程

基础知识
1.每个保留关键字(指令)都必须是大写字母
2.执行从上到下顺序执行
3.#表示注释
4.每一个指令都会创建提交一个新镜像,并提交
image.png
\
Dockerfile是面向开发的,以后要发布项目,做镜像们就需要写dockerfile文件,这文件十分简单。
Dockerfile:构建文件,定义了一切步骤,源代码
Dockerimages:通过Dockerfile构建生成的镜像,最终发布和运行的产品
Docker容器:容器就是镜像运行起来提供服务的。

DockerFile指令

FROM            #基础镜像,一切从这里开始构建
MAINTAINER      #镜像是谁写的,姓名+邮箱
RUN             #镜像构建的时候需要运行的命令
ADD             #步骤:Tomcat镜像,这个tomcat压缩包!添加内容
WORKDIR         #镜像工作目录
VOLUME          #挂载的目录
EXPOSE          #暴露端口配置
CMD             #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代    ["ls","-a"]
ENTRYPOINT      #指定这个容器启动的时候要运行的命令,可以追加命令                       ["ls","-a"]
ONBUILD         #当构建一个被继承DockerFile 这个时候就会运行ONBUILD 的指令,出发指令
COPY            #类似ADD,将我们的文件拷贝到镜像中
ENV             #构建的时候设置环境变量

image.png
实操一下
Docker Hub中99%镜像都是从这个基础镜像过来的FROM scratch,然后配置需要的软件和需求来进行构建
image.png

创建一个自己的centos

1.#编写一个Dockerfile的文件
[root@localhost dockerfile]# cat mydockerfile-centos
FROM centos
MAINTAINER xaingguo<liu_9826110@163.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

2.通过这个文件构建镜像
命令docker bulid -f dockerfile文件 -t 镜像名:[tag] .
docker build -f mydockerfile-centos -t mycentos:0.1 .

Successfully built 5a6c2a12fada
Successfully tagged mycentos:0.1
3.测试运行

对比:之前原生的centos
image.png
增加之后的镜像
image.png
可以列出本地镜像的变更历史
image.png

实操搞一下:Tomcat镜像

1.准备Tomcat压缩包,jdk压缩包 <https://pan.baidu.com/s/1wTfy9ZW-tWWM6c61yQWPxQ>;
提取码:xrq4
image.png
2.编写Dockerfile

FROM centos:7
MAINTAINER nature<liu9826110@163.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u11-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.22.tar.gz /usr/local

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_11
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.22/bin/logs/catalina.out

3.构建镜像

[root@xiangguo tomcat]# docker build -t diytomcat .
[root@xiangguo tomcat]# docker images  

image.png
4.启动镜像

[root@xiangguo tomcat]# docker run -d -p 9090:8080 --name xgtomcat -v /home/nature/build/tomcat/test:/usr/local/apache-tomcat-9.0.22/webapps/test -v /home/nature/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.22/logs diytomcat

5访问测试
image.png
6.发布项目(由于做卷挂载,可以直接在本地编写项目,发布)
index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>首页</title>
<style>
    *{
    padding:0;
    margin:0;
    font-family:"微软雅黑";
}
.header{
    height:72px;
    background:#458fce ;
}
.header .logo{
    color:#fff ;
    line-height:70px;
    font-size:30px;
    margin-left:20px;
    display:inline-block;
    text-align:center;

}
a {
    color: #fff ;
    text-decoration: none ;
}
.header .login{
    float:right;
    color:#fff ;
    line-height:72px;
    margin-right:2px;
    display:inline-block;
}
.banner{
    height:380px;
    overflow:hidden;
    background: #ddd;
}
</style>
</head>
<body>
    <div class="header">
        <div class="logo">web实践</div>
        <div class ="login">
            <a rel="nofollow" href ="javascript:void(0)">登录</a>
            <span>|</span> 
            <a rel="nofollow" href ="javascript:void(0)">故事</a>
        </div>
    </div>
</body>
</html>

image.png

发布自己的镜像

DockerHub
地址:<https://hub.docker.com>;

[root@xiangguo ~]# docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username

登陆成功后,提交镜像

[root@xiangguo ~]# docker login -u liuzhaoxuan
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@xiangguo ~]# docker push liuzhaoxuan/diytomcat:1.0
The push refers to repository [docker.io/liuzhaoxuan/diytomcat]
An image does not exist locally with the tag: liuzhaoxuan/diytomcat
[root@xiangguo ~]# docker tag eb50604912df lzx/diytomcat:1.0
[root@xiangguo ~]# docker iomages
docker: 'iomages' is not a docker command.
See 'docker --help'
[root@xiangguo ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED        SIZE
diytomcat       latest    eb50604912df   8 hours ago    727MB
lzx/diytomcat   1.0       eb50604912df   8 hours ago    727MB
mysql           latest    5c62e459e087   8 weeks ago    556MB
cmdtest         latest    dc266f7bb898   2 months ago   204MB
nginx           latest    f0b8a9a54136   3 months ago   133MB
tomcat          latest    c0e850d7b9bb   3 months ago   667MB
hello-world     latest    d1165f221234   5 months ago   13.3kB
centos          7         8652b9f0cb4c   9 months ago   204MB
[root@xiangguo ~]# docker push lzx/diytomcat:1.0
The push refers to repository [docker.io/lzx/diytomcat]
962fe093e7b9: Preparing
168475953090: Preparing
cc841424475a: Preparing
d3b8ca1656d5: Preparing
0ac7206ca0c3: Preparing
174f56854903: Waiting
denied: requested access to the resource is denied
但是会拒绝

image.png

[root@xiangguo ~]# docker tag eb50604912df liuzhaoxuan/lzxtomcat:1.0
[root@xiangguo ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED        SIZE
diytomcat               latest    eb50604912df   22 hours ago   727MB
liuzhaoxuan/lzxtomcat   1.0       eb50604912df   22 hours ago   727MB
mysql                   latest    5c62e459e087   8 weeks ago    556MB
cmdtest                 latest    dc266f7bb898   2 months ago   204MB
nginx                   latest    f0b8a9a54136   3 months ago   133MB
tomcat                  latest    c0e850d7b9bb   3 months ago   667MB
hello-world             latest    d1165f221234   5 months ago   13.3kB
centos                  7         8652b9f0cb4c   9 months ago   204MB

[root@xiangguo ~]# docker push liuzhaoxuan/lzxtomcat:1.0
The push refers to repository [docker.io/liuzhaoxuan/lzxtomcat]
962fe093e7b9: Pushed
168475953090: Pushing [===>                                               ]   12.5MB/183.8MB
cc841424475a: Pushing [====================>                              ]   6.31MB/15.49MB
d3b8ca1656d5: Pushing [>                                                  ]  4.368MB/324MB
0ac7206ca0c3: Pushed
174f56854903: Pushing [==>                                                ]  11.99MB/203.9MB
这样就正在上传。

小结

image.png