Dockerfile的基本介绍和构建过程说明、通过Dockerfile构建具有Java11的rockylinux镜像实战

目录

  • 1. Dockerfile的基本介绍
  • 2. Dockerfile编写的基础知识
  • 3. Docker build Dockerfile的流程
  • 2. 通过Dockerfile构建具有Java11的rockylinux镜像实战
    • 2.1 目标
    • 2.2 Java11的安装包准备
    • 2.3 Dockerfile
    • 2.4 docker build构建镜像
    • 2.5 通过新镜像运行容器并测试

1. Dockerfile的基本介绍

Dockerfile是用来构建Docker镜像的文本文件,是由一条条构建镜像所需的指令和参数构成的脚本

Dockerfile定义了进程需要的一切东西。Dockerfile涉及的内容包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程(当应用进程需要和系统服务或内核进程打交道,这时需要考虑如何设计namespace的权限控制)等

详细介绍参考官网Dockerfile讲解

2. Dockerfile编写的基础知识

  1. 每条保留字指令都必须为大写字母,且后面要跟随至少一个参数
  2. 指令按照从上到下的顺序进行执行
  3. 注释用#表示

3. Docker build Dockerfile的流程

第一次循环:

  1. Docker从基础镜像运行一个容器
  2. 执行一条指令并对容器作出修改
  3. 执行类似docker commit的操作,提交一个新的镜像层
  4. 将原来的容器进行删除

第二次循环:

  1. docker再基于刚提交的镜像运行一个新容器
  2. 执行dockerfile中的下一条指令,操作的方法和前面的一样

第N次循环:

  1. 直到所有的指令都执行完成

2. 通过Dockerfile构建具有Java11的rockylinux镜像实战

2.1 目标

通过Dockerfile,基于rockylinux基础镜像,构建出具有Java11和ifconfig的新镜像

2.2 Java11的安装包准备

[root@docker ~]# 
[root@docker ~]# mkdir java11_rockylinux
[root@docker ~]# 
[root@docker ~]# cd java11_rockylinux/
[root@docker java11_rockylinux]# 
[root@docker java11_rockylinux]# wget https://cdn.azul.com/zulu/bin/zulu11.56.19-ca-jdk11.0.15-linux_x64.tar.gz
[root@docker java11_rockylinux]# 

2.3 Dockerfile

内容如下:

[root@docker java11_rockylinux]# 
[root@docker java11_rockylinux]# cat Dockerfile 
FROM rockylinux
MAINTAINER bulut
 
ENV MY_WORK_DIR /root
WORKDIR $MY_WORK_DIR
 
# 安装ifconfig命令
RUN yum -y install net-tools

#安装java11。其中tar.gz包必须和Dockerfile位于同一目录
ADD zulu11.56.19-ca-jdk11.0.15-linux_x64.tar.gz /root/java11
#配置java环境变量
ENV JAVA_HOME /root/java11/zulu11.56.19-ca-jdk11.0.15-linux_x64
ENV PATH $PATH:$JAVA_HOME/bin
 
EXPOSE 80

CMD /bin/bash


[root@docker java11_rockylinux]#

2.4 docker build构建镜像

[root@docker java11_rockylinux]# 
[root@docker java11_rockylinux]# docker build -t java11_rockylinux:0.1 .
Sending build context to Docker daemon  198.8MB
Step 1/10 : FROM rockylinux
 ---> 210996f98b85
Step 2/10 : MAINTAINER bulut
 ---> Running in c05dbb563066
Removing intermediate container c05dbb563066
 ---> 2f05cc4414d9
Step 3/10 : ENV MY_WORK_DIR /root
 ---> Running in 88d51157e650
Removing intermediate container 88d51157e650
 ---> 25d02f13a1e7
Step 4/10 : WORKDIR $MY_WORK_DIR
 ---> Running in f7aba17e5b61
Removing intermediate container f7aba17e5b61
 ---> fb0b1fbb1a9c
Step 5/10 : RUN yum -y install net-tools
 ---> Running in 6a64c38cb289
Rocky Linux 8 - AppStream                       2.4 MB/s |  10 MB     00:04    
Rocky Linux 8 - BaseOS                          1.8 MB/s | 7.7 MB     00:04    
Rocky Linux 8 - Extras                          9.4 kB/s |  12 kB     00:01    
Dependencies resolved.
================================================================================
 Package         Architecture Version                        Repository    Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.52.20160912git.el8       baseos       321 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 321 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm   514 kB/s | 321 kB     00:00    
--------------------------------------------------------------------------------
Total                                           188 kB/s | 321 kB     00:01     
Rocky Linux 8 - BaseOS                          1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x6D745A60:
 Userid     : "Release Engineering "
 Fingerprint: 7051 C470 A929 F454 CEBE 37B7 15AF 5DAC 6D74 5A60
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                     

Complete!
Removing intermediate container 6a64c38cb289
 ---> 44b044b5d67a
Step 6/10 : ADD zulu11.56.19-ca-jdk11.0.15-linux_x64.tar.gz /root/java11
 ---> f8373799c02c
Step 7/10 : ENV JAVA_HOME /root/java11/zulu11.56.19-ca-jdk11.0.15-linux_x64
 ---> Running in 4918ee062592
Removing intermediate container 4918ee062592
 ---> e0ed0ec11329
Step 8/10 : ENV PATH $PATH:$JAVA_HOME/bin
 ---> Running in 61605799a5e5
Removing intermediate container 61605799a5e5
 ---> e99cbac19847
Step 9/10 : EXPOSE 80
 ---> Running in 5d63fc8cd8fc
Removing intermediate container 5d63fc8cd8fc
 ---> f65fe884a458
Step 10/10 : CMD /bin/bash
 ---> Running in 2da56874d147
Removing intermediate container 2da56874d147
 ---> 7b8d925410ff
Successfully built 7b8d925410ff
Successfully tagged java11_rockylinux:0.1
[root@docker java11_rockylinux]# 

2.5 通过新镜像运行容器并测试

[root@docker java11_rockylinux]# 
[root@docker java11_rockylinux]# docker images java11_rockylinux
REPOSITORY          TAG       IMAGE ID       CREATED              SIZE
java11_rockylinux   0.1       7b8d925410ff   About a minute ago   581MB
[root@docker java11_rockylinux]# 
[root@docker java11_rockylinux]# docker run -it 7b8d925410ff bash
[root@7b576685d9bc ~]# 
[root@7b576685d9bc ~]# ifconfig
eth0: flags=4163  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@7b576685d9bc ~]# 
[root@7b576685d9bc ~]# java -version
openjdk version "11.0.15" 2022-04-19 LTS
OpenJDK Runtime Environment Zulu11.56+19-CA (build 11.0.15+10-LTS)
OpenJDK 64-Bit Server VM Zulu11.56+19-CA (build 11.0.15+10-LTS, mixed mode)
[root@7b576685d9bc ~]# 

你可能感兴趣的:(docker,dockerfile,docker,build,dockerfile编写,rockylinux)