• 深入浅出Dockerfile实战


    https://docs.docker.com/engine/reference/builder/

    Dockerfile的作用

    Docker can build images automatically by reading the instructions from a Dockerfile

    Docker可以通过读取Dockerfile中的指令来自动构建映像。

    Docker build --help

    在这里插入图片描述

    案例

    首先准备一个可以启动的jar包。并在里面编写一个很简单的接口。

    @RestController
    public class TestController {
    
        @GetMapping("hello")
        public String hello() {
            return "dockerfile hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    yml指定为9000端口

    server:
      port: 9000
    
    • 1
    • 2

    将jar包放在Docker环境的服务器上,并在同一个目录创建Dockerfile并编写如下内容

    FROM java:8
    //指定作者名字
    MAINTAINER author
    //挂载点
    VOLUME /tmp
    //将jar包添加到镜像内部并重命名为abcd.jar
    ADD docker-file-0.0.1-SNAPSHOT.jar abcd.jar
    //执行命令
    ENTRYPOINT ["java","-jar","abcd.jar"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在Dockerfile文件目录执行如下命令编译Dockerfile

     docker build -t abcde:1.0 .
    
    • 1

    查询镜像

    docker images 
    
    • 1

    启动容器

    docker run -d -p 宿主机端口:容器内端口 /bin/bash
    
    • 1

    ENTRYPOINT

    ENTRY entry 入口
    POINT point 点

    The exec form, which is the preferred form

    ENTRYPOINT ["executable", "param1", "param2"]
    
    • 1

    The shell form

    ENTRYPOINT command param1 param2
    
    • 1

    官方更加推荐上面这种格式。

    EXPOSE

    作用:暴露TCP或者UDP端口。

    The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
    You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

    the default is TCP。

    指定udp

    EXPOSE 80/udp
    
    • 1

    To expose on both TCP and UDP, include two lines。

    EXPOSE 80/tcp
    EXPOSE 80/udp
    
    • 1
    • 2

    Regardless of the EXPOSE settings, you can override them at runtime by using the -p flag.

    docker run -p 80:80/tcp -p 80:80/udp ...
    
    • 1

    这句话是说Dockerfile指定的EXPOSE 端口都会被 -p 命令覆盖,也就是以运行时设置的为主。

    FROM

    The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.

    指令FROM初始化一个新的构建步骤并对基础镜像设置后续指令。

    FROM [--platform=<platform>] <image> [AS <name>]
    FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
    FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
    
    • 1
    • 2
    • 3

    As such, a valid Dockerfile must start with a FROM instruction

    因此有效的Dockerfile必须以 FROM 指令开始。

  • 相关阅读:
    AI大模型-流式处理 (以百度接口为例)
    回顾10年发展,2022亚马逊云科技re:Invent全球大会即将来袭
    golang 拉取 bitbucket.org 私有库
    大数据运维实战第二十六课 Yarn、HDFS、Kafka 内存调优策略以及性能瓶颈
    git远程操作
    数据结构之队列
    a16z:2024年GenAI市场将爆发
    【Codeforces】 CF79D Password
    手把手教你解决循环依赖,一步一步地来窥探出三级缓存的奥秘
    求一份网页设计结课大作业,要求用到html,css,javascript,的知识
  • 原文地址:https://blog.csdn.net/qq_37151886/article/details/128023243