ONBUILD是 当目前的镜像在基于父镜像的基础上创建的时候,执行了父镜像的onbuild语句,
    FROM 父镜像 而不是 创建父镜像的dockerfile

    1. //编写父镜像
    2. [root@test mydocker]# cat fatherfile
    3. FROM centos
    4. RUN yum install -y curl
    5. ENTRYPOINT ["curl","-s","http://ip.cn"]
    6. ONBUILD RUN echo "father is good"
    7. //创建father名称的镜像
    8. [root@test mydocker]# docker build -f /mydocker/fatherfile -t father .
    9. Sending build context to Docker daemon 6.144kB
    10. Step 1/4 : FROM centos
    11. ---> 300e315adb2f
    12. Step 2/4 : RUN yum install -y curl
    13. ---> Using cache
    14. ---> 5f37af576672
    15. Step 3/4 : ENTRYPOINT ["curl","-s","http://ip.cn"]
    16. ---> Using cache
    17. ---> adf1e62f8da6
    18. Step 4/4 : ONBUILD RUN echo "father is good"
    19. ---> Using cache
    20. ---> fbb09770b5ed
    21. Successfully built fbb09770b5ed
    22. Successfully tagged father:latest
    23. //编写子dockerfile文件继承父镜像
    24. [root@test mydocker]# cat sonfile
    25. //子镜像的dockerfile继承的是创建出来的父镜像,而不是创建父镜像用的dockerfile文件
    26. FROM father
    27. RUN yum install -y curl
    28. ENTRYPOINT ["curl","-s","http://ip.cn"]
    29. ONBUILD RUN echo "father is good"
    30. //创建子镜像
    31. [root@test mydocker]# docker build -f /mydocker/sonfile -t son .
    32. Sending build context to Docker daemon 7.168kB
    33. //继承父镜像
    34. Step 1/4 : FROM father
    35. # Executing 1 build trigger
    36. ---> Running in 7ca9d7f848c9
    37. //执行了创建父镜像的onbuild语句
    38. father is good
    39. Removing intermediate container 7ca9d7f848c9
    40. ---> a480887d7a79
    41. Step 2/4 : RUN yum install -y curl
    42. ---> Running in b57ba10115fc
    43. Last metadata expiration check: 8:12:35 ago on Tue Jan 26 14:51:14 2021.
    44. Package curl-7.61.1-14.el8_3.1.x86_64 is already installed.
    45. Dependencies resolved.
    46. Nothing to do.
    47. Complete!
    48. Removing intermediate container b57ba10115fc
    49. ---> eaa5d942d3a2
    50. Step 3/4 : ENTRYPOINT ["curl","-s","http://ip.cn"]
    51. ---> Running in c5c07c614ee2
    52. Removing intermediate container c5c07c614ee2
    53. ---> 89ef8472381d
    54. Step 4/4 : ONBUILD RUN echo "father is good"
    55. ---> Running in 02fa4990dd3f
    56. Removing intermediate container 02fa4990dd3f
    57. ---> f0d5a065fb1e
    58. Successfully built f0d5a065fb1e
    59. Successfully tagged son:latest
    60. [root@test mydocker]#