美文网首页
Dockerfile构建Centos镜像和Springboot应

Dockerfile构建Centos镜像和Springboot应

作者: overflowedstack | 来源:发表于2021-03-13 12:34 被阅读0次
  1. 什么是Dockerfile
    Dockerfile就是用来构建docker镜像的构建文件,是一个脚本。
    构建步骤:
    1)编写一个dockerfile文件
    2)docker build构建成为一个镜像
    3)docker run运行镜像
    4)docker push发布镜像到dockerhub或者阿里云镜像仓库

  2. 查看一个官方镜像的dockerfile,例如centos
    https://github.com/CentOS/sig-cloud-instance-images/blob/ccd17799397027acf9ee6d660e75b8fce4c852e8/docker/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"]

上面就是centos官方镜像的dockerfile。很多官方镜像都是基础包,很多功能都没有。
我们可以自己搭建自己的镜像。

  1. Dockerfile构建
    基础知识:
    每个指令都全大写
    执行顺序从上到下
    符号#表示注释
    每一个指令都会创建提交一个新的镜像层,并提交。

dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件。docker镜像逐渐成为企业交付的标准。

Dockerfile:构建文件,定义了一切的步骤,源代码
Docker images:通过dockerfile构建生成的镜像
Docker 容器:镜像运行起来,成为服务器

  1. Dockerfile的指令
CMD         #指定这个容器启动的时候要运行的命令,只有最后一个可生效,可被替代
ENTRYPOINT  #指定这个容器启动的时候要运行的命令,可以追加命令
ENV         #构建的时候设置环境变量
dockerfile指令
  1. 构建自己的centos
    5.1 编写dockerfile文件
    Docker hub中绝大部分的镜像都是从这个基础镜像过来的FROM scratch,然后配置需要的软件和配置来构建。
FROM centos
MAINTAINER 666@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

5.2 通过dockerfile构建镜像

$ docker build -f mydockerfile -t mycentos:0.1 ./
Sending build context to Docker daemon 2.048 kB
Step 1/10 : FROM centos
 ---> 300e315adb2f
Step 2/10 : MAINTAINER 666@qq.com
 ---> Running in 045cb5791ecf
 ---> 1e340b8251e1
......
Successfully built 5bc14d5566b0

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            0.1                 5bc14d5566b0        8 seconds ago       291 MB
centos              latest              300e315adb2f        3 months ago        209 MB

5.3 运行镜像

$ docker run -it 5bc14d5566b0
[root@9fcb1418876c local]# pwd
/usr/local
[root@9fcb1418876c local]# which ifconfig
/usr/sbin/ifconfig
[root@9fcb1418876c local]# which vim
/usr/bin/vim

5.4 通过docker history查看本地进行的更改历史

$ docker history 5bc14d5566b0
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
5bc14d5566b0        12 hours ago        /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/b...   0 B                 
9af872d3ea9e        12 hours ago        /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "ec...   0 B                 
80bee366026b        12 hours ago        /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "ec...   0 B                 
cd7ce39f582b        12 hours ago        /bin/sh -c #(nop)  EXPOSE 80/tcp                0 B                 
1e63a4ec0c86        12 hours ago        /bin/sh -c yum -y install net-tools             23.3 MB             
e399e74cc924        12 hours ago        /bin/sh -c yum -y install vim                   58 MB               
799a505d2fc7        12 hours ago        /bin/sh -c #(nop) WORKDIR /usr/local            0 B                 
07074579c9e9        12 hours ago        /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0 B                 
1e340b8251e1        12 hours ago        /bin/sh -c #(nop)  MAINTAINER 666@qq.com        0 B                 
300e315adb2f        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
<missing>           3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema....   0 B                 
<missing>           3 months ago        /bin/sh -c #(nop) ADD file:bd7a2aed6ede423...   209 MB   
  1. 构建springboot应用
    6.1 准备一个打包好的springboot应用程序
    笔者手头准备了一个springboot的helloworld jar包demo-0.0.1-SNAPSHOT.jar,启动后通过localhost:8080/hello可以调用其服务。
    6.2 编写dockerfile构建应用
FROM openjdk:8-jre #基于哪个镜像进行构建
WORKDIR /app  #定义进入容器时默认位置,接下来后序操作工作位置
ADD demo-0.0.1-SNAPSHOT.jar app.jar #将外面的jar包复制到工作目录,并修改名为app.jar
EXPOSE 8080  #让当前容器暴露哪个端口,因为项目使用端口是8080
ENTRYPOINT ["java","-jar"] #启动容器固定命令
CMD ["app.jar"]  #执行jar名称

6.3 执行构建命令,构建镜像

$ docker build -f springbootdockerfile -t myhelloworld:01 .
Sending build context to Docker daemon 21.59 MB
Step 1/6 : FROM openjdk:8-jre
8-jre: Pulling from library/openjdk
e22122b926a1: Pull complete 
f29e09ae8373: Pull complete 
e319e3daef68: Pull complete 
3c6c60bfcb21: Pull complete 
a5ee52e88d8b: Pull complete 
7fd26b251c91: Pull complete 
Digest: sha256:48ed02ddca38195e2aea17ef31318238b81276a10878ce37ea78f55e2e94304c
Status: Downloaded newer image for openjdk:8-jre
 ---> 9f5e0576aa8f
Step 2/6 : WORKDIR /app
 ---> bbab0512086e
Removing intermediate container 1a1c7c7b9f53
Step 3/6 : ADD demo-0.0.1-SNAPSHOT.jar app.jar
 ---> 6912ebc1fda1
Removing intermediate container 6f9a1440544b
Step 4/6 : EXPOSE 8080
 ---> Running in 7406ff58d18e
 ---> c89382449991
Removing intermediate container 7406ff58d18e
Step 5/6 : ENTRYPOINT java -jar
 ---> Running in 1ee589b828d3
 ---> 4ec8e993dbc1
Removing intermediate container 1ee589b828d3
Step 6/6 : CMD app.jar
 ---> Running in 0cb60e544403
 ---> d62674c28103
Removing intermediate container 0cb60e544403
Successfully built d62674c28103

6.4 运行镜像

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
openjdk             8-jre               9f5e0576aa8f        6 hours ago         268 MB
myhelloworld        01                  d62674c28103        19 hours ago        289 MB

$ docker run -d -p 8080:8080 --name helloworld myhelloworld:01
692b6de0f304d4500a4229b2f80cdf6d4649802252522b19c804cd21187e8a04

6.5 测试服务,Hello World!

$ curl "localhost:8080/hello"
Hello World!

相关文章

网友评论

      本文标题:Dockerfile构建Centos镜像和Springboot应

      本文链接:https://www.haomeiwen.com/subject/ogmgqltx.html