dockerfile

dockerfile介绍

dockerfile是用来构建docker镜像的文件,命令参数脚本

构建步骤

  1. 编写一个dockerfile文件
  2. docker build构建成为一个镜像
  3. docker run运行镜像
  4. docker push发布镜像(dockerhub、阿里云仓库)

 

 

查看官方是怎么做的

centos官方镜像的dockerfile

  
FROM scratch
ADD centos-8-x86_64.tar.xz /
LABEL org.label-schema.schema-version="1.0"     org.label-schema.name="CentOS Base Image"     org.label-schema.vendor="CentOS"     org.label-schema.license="GPLv2"     org.label-schema.build-date="20201204"
CMD ["/bin/bash"]

很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像

 

dockerfile构建过程

 基础知识:

  1. 每个保留关键字(指令)必须是大写字母
  2. 执行从上到下顺序执行
  3. #表示注释
  4. 每一个指令都会创建提交一个新的镜像层并提交

dockerfile是面向开发的。我们以后要发布项目。做镜像,就需要编写dockerfile文件,这个文件十分简单

docker镜像逐渐成为企业交付的标准,必须掌握

 

步骤:开发、部署、运维

dockerfile:构建文件,定义了一切的步骤,源代码

dockerimage:通过dockerfile构建生成的镜像,最终发布和运行的产品,原来是jar,war

docker容器:容器就是镜像运行起来提供服务的

 

dockerfile指令

指令 描述
FROM 基础镜像,一切从这里开始构建
MAINTAINER

镜像是谁写的,姓名+邮箱

RUN 镜像构建的时候需要运行的命令
ADD 添加内容 
WORKDIR 镜像工作目录
VOLUME 挂载的目录 
EXPOSE 暴露的端口 
RUN

程序运行之后要干的事 

CMD

指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代

ENTRYPOINT

指定这个容器启动的时候要运行的命令,可以追加命令

ONBUILD

当构建一个被继承dockerfile 这个时候就会运行ONBUILD的指令,触发指令

COPY

类似ADD命令,将我们的文件拷贝到镜像中

ENV

构建的时候设置环境变量

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

实战测试

创建一个自己的centos

在home目录下创建dockerfile

[root@iZmu0fa4hv0jwzZ home]# mkdir dockerfile
[root@iZmu0fa4hv0jwzZ home]# cd dockerfile/

编写自己的dockerfile文件

[root@iZmu0fa4hv0jwzZ dockerfile]# vim mydockerfile-centos
[root@iZmu0fa4hv0jwzZ dockerfile]# cat mydockerfile-centos 
FROM centos
MAINTAINER ayanami<12121212@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

运行dockerfile文件

命令 docker build -f dockerfile文件路径 -t 镜像名:[tag]

[root@iZmu0fa4hv0jwzZ dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .

查看镜像

[root@iZmu0fa4hv0jwzZ dockerfile]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mycentos      0.1       f1661e656e39   3 minutes ago   295MB

测试运行

[root@iZmu0fa4hv0jwzZ dockerfile]# docker run -it mycentos:0.1
[root@dd4a17c47af7 local]# pwd
/usr/local
[root@iZmu0fa4hv0jwzZ dockerfile]# docker run -it mycentos:0.1
[root@dd4a17c47af7 local]# pwd
/usr/local
[root@dd4a17c47af7 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

 

列出本地镜像的变更历史

平时拿到一个镜像,可以研究一下它是怎么做的

docker history 镜像id
[root@iZmu0fa4hv0jwzZ dockerfile]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
mycentos      0.1       f1661e656e39   12 minutes ago   295MB
test/centos   1.0       11bfc295e9b3   8 hours ago      209MB
tomcat02      1.0       c93f6d59c630   23 hours ago     672MB
tomcat        9.0       5505f7218e4d   4 days ago       667MB
nginx         latest    d1a364dc548d   3 weeks ago      133MB
mysql         5.7       2c9028880e58   5 weeks ago      447MB
centos        latest    300e315adb2f   6 months ago     209MB
[root@iZmu0fa4hv0jwzZ dockerfile]# docker history f1661e656e39
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
f1661e656e39   12 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B        
bd1b8aa98ba2   12 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
361c7948f003   12 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
54ebf51ed5ed   12 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
45dfc9afcbe3   12 minutes ago   /bin/sh -c yum -y install net-tools             24.9MB    
4f1c0c7e132a   12 minutes ago   /bin/sh -c yum -y install vim                   60.7MB    
44e99b19d8fa   13 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B        
a137866c37b0   13 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B        
fb306da081ac   13 minutes ago   /bin/sh -c #(nop)  MAINTAINER ayanami<121212…   0B        
300e315adb2f   6 months ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      6 months ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      6 months ago     /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7…   209MB

 

CMD和ENTRYPOINT的区别

测试CMD

在dockerfile目录下新建dockerfile文件

[root@iZmu0fa4hv0jwzZ dockerfile]# vim dockerfile-cmd-test
[root@iZmu0fa4hv0jwzZ dockerfile]# cat dockerfile-cmd-test 
FROM centos
CMD ["ls","-a"]

通过dockerfile文件创建镜像

[root@iZmu0fa4hv0jwzZ dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 15ab54a9f711
Removing intermediate container 15ab54a9f711
 ---> 3f66dbcdb279
Successfully built 3f66dbcdb279
Successfully tagged cmdtest:latest

运行

[root@iZmu0fa4hv0jwzZ dockerfile]# docker run 3f66dbcdb279
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

发现 ls -a 命令生效

如果追加一个 -l命令

[root@iZmu0fa4hv0jwzZ dockerfile]# docker run 3f66dbcdb279 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.

会报错,这是因为cmd的情况下 -l 替换了 CMD["ls","-a"] 命令,-l 不是命令所以报错

 

测试ENTRYPOINT

 新建dockerfile文件

[root@iZmu0fa4hv0jwzZ dockerfile]# cat dockerfile-cmd-entrypoint 
FROM centos
ENTRYPOINT ["ls","-a"]

 通过dockerfile文件创建镜像

[root@iZmu0fa4hv0jwzZ dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 300e315adb2f
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 6bf9a88469cf
Removing intermediate container 6bf9a88469cf
 ---> 0c2a88ea9329
Successfully built 0c2a88ea9329
Successfully tagged entrypoint-test:latest

运行

[root@iZmu0fa4hv0jwzZ dockerfile]# docker run 0c2a88ea9329
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

追加一个 -l 命令

[root@iZmu0fa4hv0jwzZ dockerfile]# docker run 0c2a88ea9329 -l
total 0
drwxr-xr-x   1 root root   6 Jun 22 03:33 .
drwxr-xr-x   1 root root   6 Jun 22 03:33 ..
-rwxr-xr-x   1 root root   0 Jun 22 03:33 .dockerenv
lrwxrwxrwx   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root 340 Jun 22 03:33 dev
drwxr-xr-x   1 root root  66 Jun 22 03:33 etc
drwxr-xr-x   2 root root   6 Nov  3  2020 home
lrwxrwxrwx   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root   6 Dec  4  2020 lost+found
drwxr-xr-x   2 root root   6 Nov  3  2020 media
drwxr-xr-x   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x 115 root root   0 Jun 22 03:33 proc
dr-xr-x---   2 root root 162 Dec  4  2020 root
drwxr-xr-x  11 root root 163 Dec  4  2020 run
lrwxrwxrwx   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x  13 root root   0 Jun 22 03:33 sys
drwxrwxrwt   7 root root 145 Dec  4  2020 tmp
drwxr-xr-x  12 root root 144 Dec  4  2020 usr
drwxr-xr-x  20 root root 262 Dec  4  2020 var

说明ENTRYPOINT的追加命令是直接拼接在ENTRYPOINT命令的后面

 

使用dockerfile创建tomcat镜像

准备镜像文件、tomcat压缩包、jdk压缩包

[root@iZmu0fa4hv0jwzZ mysoftware]# ll
total 152764
-rw-r--r-- 1 root root  11489338 Jun 22 12:20 apache-tomcat-9.0.48.tar.gz
-rw-r--r-- 1 root root 144935989 Jun 22 12:21 jdk-8u291-linux-x64.tar.gz
[root@iZmu0fa4hv0jwzZ mysoftware]# touch readme.txt

编写dockerfile文件

官方命名 Dockerfile,build会自动寻找这个文件,就不需要 -f 指定了

[root@iZmu0fa4hv0jwzZ mysoftware]# vim Dockerfile 
[root@iZmu0fa4hv0jwzZ mysoftware]# cat Dockerfile 
FROM centos
MAINTAINER ayanami<ayanamiblhx@git.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u291-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.48.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

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

EXPOSE 8080

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

 

构建镜像

[root@iZmu0fa4hv0jwzZ mysoftware]# docker build -t diytomcat .

查看镜像

[root@iZmu0fa4hv0jwzZ mysoftware]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
diytomcat         latest    bcfda04673f9   About a minute ago   646MB

运行镜像并指定挂载目录

[root@iZmu0fa4hv0jwzZ mysoftware]# docker run -d -p 9090:8080 --name mytomcat02 -v /home/dockerfile/mysoftware/test:/usr/local/apache-tomcat-9.0.48/webapps/test -v /home/dockerfile/mysoftware/tomcatlogs:/usr/local/apache-tomcat-9.0.48/logs diytomcat

进入容器

[root@iZmu0fa4hv0jwzZ mysoftware]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS              PORTS                                       NAMES
edeba1aa9068   diytomcat   "/bin/sh -c '/usr/lo…"   About a minute ago   Up About a minute   0.0.0.0:9090->8080/tcp, :::9090->8080/tcp   mytomcat02
[root@iZmu0fa4hv0jwzZ mysoftware]# docker exec -it edeba1aa9068 /bin/bash 
[root@edeba1aa9068 local]# ls apache
-tomcat-9.0.48 bin etc games include jdk1.8.0_291 lib lib64 libexec readme.txt sbin share src
[root@edeba1aa9068 local]# cd apache
-tomcat-9.0.48/
[root@edeba1aa9068 apache
-tomcat-9.0.48]# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs temp webapps work

进入主机测试tomcat

[root@iZmu0fa4hv0jwzZ mysoftware]# curl localhost:9090



<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/9.0.48</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />

表明配置成功

 

使用tomcat部署项目

进入主机test目录

[root@iZmu0fa4hv0jwzZ mysoftware]# cd test

创建WEB-INF目录

[root@iZmu0fa4hv0jwzZ test]# mkdir WEB-INF
[root@iZmu0fa4hv0jwzZ test]# cd WEB-INF/
[root@iZmu0fa4hv0jwzZ WEB-INF]# vim web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
</web-app>
[root@iZmu0fa4hv0jwzZ WEB-INF]# cd ..
[root@iZmu0fa4hv0jwzZ test]# vim index.jsp 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  $END$
  </body>
</html>

在浏览器上访问tomcat

 

发现可以访问,说明项目部署成功

 

发布镜像到dockerhub

在dockerhub注册自己的账号

地址:Docker Hub

 在服务器上提交自己的镜像

登录

[root@iZmu0fa4hv0jwzZ mysoftware]# docker login -u 你的用户名
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

更改镜像名

docker tag 镜像id 用户名/镜像名:[tag]

[root@iZmu0fa4hv0jwzZ mysoftware]# docker tag 8e3c4b61a897 ayanamiblhx/diytomcat:1.0

提交自己的镜像

[root@iZmu0fa4hv0jwzZ mysoftware]# docker push ayanamiblhx/diytomcat:1.0
The push refers to repository [docker.io/ayanamiblhx/diytomcat]
838d54b051df: Pushed 
98f252f744ee: Pushed 
d75d09d37a7e: Pushed 
e147dbc79e6e: Pushed 

 

posted @ 2021-06-23 09:45  路过的柚子厨  阅读(113)  评论(0编辑  收藏  举报