当前位置: 移动技术网 > IT编程>开发语言>Java > 如何基于Dockerfile构建tomcat镜像

如何基于Dockerfile构建tomcat镜像

2020年08月29日  | 移动技术网IT编程  | 我要评论
dockerfile 是用来构建docker镜像的文件!命令参数脚本!构建步骤: 编写一个dockerfile 文件 docker build 构建成一个镜像 docker run 运行镜像

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

构建步骤:

  • 编写一个dockerfile 文件
  • docker build 构建成一个镜像
  • docker run 运行镜像
  • docker push 发布镜像(dockerhub、阿里云镜像仓库)
# 以contes为例 查看 dockerhub 上 contes 的 dockerfile

from scratch
add centos-7-x86_64-docker.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="20200809" \
  org.opencontainers.image.title="centos base image" \
  org.opencontainers.image.vendor="centos" \
  org.opencontainers.image.licenses="gpl-2.0-only" \
  org.opencontainers.image.created="2020-08-09 00:00:00+01:00"

cmd ["/bin/bash"]

dockerfile构建过程

基础知识:

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

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

docker镜像逐渐成为企业交付的标准!

dockerfile:构建镜像,定义了一切的步骤,源代码;

dockerimages:通过dockerfile构建生成的一个镜像,这个镜像就是我们最终发布和运行的产品!

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

dockerfile的指令

from			# 基础镜像
maintainer		# 镜像是谁写的	姓名 + 邮箱
run				# 镜像构建的时候需要运行的命令
add				# 步骤	eg:tomcat镜像---》放入tomcat的压缩包!添加内容
workdir			# 镜像的工作目录
volume			# 挂载的目录位置
expost			# 暴露端口配置
cmd				# 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
entrypoint		# 指定这个容器启动的时候要运行的命令,可以追加命令
onbuild			# 当构建一个被继承的 dockerfile ,这个时候就会运行 onbuild 的指令,触发指令
copy			# 类似add,将我们的文件拷贝到镜像中
env 			# 构建的时候设置环境变量

实战测试

docker hub 中 99% 的镜像都是从 from scratch 这个基础镜像过来的 ,然后配置我们需要的软件和配置来进行构建

创建一个自己的centos

# 1、编写dockerfile的文件
from centos
# 作者信息
maintainer yinxiaodong<m15010969094@163.com>
# 配置环境变量 
env mypath /user/local
# 指定镜像的工作目录
workdir $mypath 
 
run yum install -y vim
run yum install -y net-tools
# 暴露80端口 
expose 80
 
cmd echo $mypath
cmd echo "---------end--------"
cmd /bin/bash


# 2、通过文件构建镜像
# 命令 docker build -f dockerfile文件路径	-t 镜像名:版本号 .
[root@localhost dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
sending build context to docker daemon 2.048kb
step 1/9 : from centos
 ---> 0d120b6ccaa8
step 2/9 : maintainer yinxiaodong<m15010969094@163.com>
 ---> running in f79b12ffb083
removing intermediate container f79b12ffb083
 ---> 8ee53d3f7a65
step 3/9 : env mypath /user/local
 ---> running in 659a2c96d5f7
removing intermediate container 659a2c96d5f7
 ---> 59ab131ef44c
step 4/9 : workdir $mypath
 ---> running in 0b4b8f9c65bb
removing intermediate container 0b4b8f9c65bb
 ---> 408b06671488
step 5/9 : run yum install -y net-tools
 ---> running in 151f81148a87
centos-8 - appstream              118 kb/s | 5.8 mb   00:50  
centos-8 - base                 218 kb/s | 2.2 mb   00:10  
centos-8 - extras                1.8 kb/s | 7.3 kb   00:04  
dependencies resolved.
================================================================================
 package     architecture version            repository  size
================================================================================
installing:
 net-tools    x86_64    2.0-0.51.20160912git.el8    baseos    323 k

transaction summary
================================================================================
install 1 package

total download size: 323 k
installed size: 1.0 m
downloading packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm  219 kb/s | 323 kb   00:01  
--------------------------------------------------------------------------------
total                      66 kb/s | 323 kb   00:04   
warning: /var/cache/dnf/baseos-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm: header v3 rsa/sha256 signature, key id 8483c65d: nokey
centos-8 - base                 162 kb/s | 1.6 kb   00:00  
importing gpg key 0x8483c65d:
 userid   : "centos (centos official signing key) <security@centos.org>"
 fingerprint: 99db 70fa e1d7 ce22 7fb6 4882 05b5 55b3 8483 c65d
 from    : /etc/pki/rpm-gpg/rpm-gpg-key-centosofficial
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.51.20160912git.el8.x86_64       1/1 
 running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64       1/1 
 verifying    : net-tools-2.0-0.51.20160912git.el8.x86_64       1/1 

installed:
 net-tools-2.0-0.51.20160912git.el8.x86_64                   

complete!
removing intermediate container 151f81148a87
 ---> c0a0546c9b2a
step 6/9 : expose 80
 ---> running in 42000936515d
removing intermediate container 42000936515d
 ---> fe68114ecf3f
step 7/9 : cmd echo $mypath
 ---> running in c393fc53a354
removing intermediate container c393fc53a354
 ---> a6924276bf90
step 8/9 : cmd echo "---------end--------"
 ---> running in 5994de56f0a1
removing intermediate container 5994de56f0a1
 ---> a8ba0ebb3770
step 9/9 : cmd /bin/bash
 ---> running in d1fa2d436363
removing intermediate container d1fa2d436363
 ---> 41bb76be4884
successfully built 41bb76be4884
successfully tagged mycentos:0.1
[root@localhost dockerfile]# docker images
repository      tag         image id      created       size
mycentos       0.1         41bb76be4884    7 seconds ago    246mb
centos        1.0         d840628c30a9    6 hours ago     215mb
centos        latest       0d120b6ccaa8    2 days ago     215mb

# 启动 mycentos 使用 ifconfig 命令查看ip信息
[root@localhost dockerfile]# docker run -it mycentos:0.1
[root@494c2bc72263 local]# ifconfig
eth0: flags=4163<up,broadcast,running,multicast> mtu 1500
    inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
    ether 02:42:ac:11:00:03 txqueuelen 0 (ethernet)
    rx packets 6 bytes 516 (516.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<up,loopback,running> 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
# 查看所在目录    /user/local 正是我们在dockerfile中指定的目录 
[root@494c2bc72263 local]# pwd
/user/local

查看镜像的构建历史记录

# 命令 docker history 镜像id

[root@localhost ~]# docker history 41bb76be4884
image        created       created by                   size        comment
41bb76be4884    16 minutes ago   /bin/sh -c #(nop) cmd ["/bin/sh" "-c" "/bin…  0b         
a8ba0ebb3770    16 minutes ago   /bin/sh -c #(nop) cmd ["/bin/sh" "-c" "echo…  0b         
a6924276bf90    16 minutes ago   /bin/sh -c #(nop) cmd ["/bin/sh" "-c" "echo…  0b         
fe68114ecf3f    16 minutes ago   /bin/sh -c #(nop) expose 80          0b         
c0a0546c9b2a    16 minutes ago   /bin/sh -c yum install -y net-tools       31.3mb       
408b06671488    17 minutes ago   /bin/sh -c #(nop) workdir /user/local      0b         
59ab131ef44c    17 minutes ago   /bin/sh -c #(nop) env mypath=/user/local    0b         
8ee53d3f7a65    17 minutes ago   /bin/sh -c #(nop) maintainer yinxiaodong<m1…  0b         
0d120b6ccaa8    2 days ago     /bin/sh -c #(nop) cmd ["/bin/bash"]      0b         
<missing>      2 days ago     /bin/sh -c #(nop) label org.label-schema.sc…  0b         
<missing>      2 days ago     /bin/sh -c #(nop) add file:538afc0c5c964ce0d…  215mb  

我们平时拿到一个镜像,可以研究一下它是怎么做的---->docker history 镜像id

cmd 和 entrypoint 的区别

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

# 测试 cmd
# 编写dockerfile的文件
[root@localhost dockerfile]# cat dockerfile-cmd-test 
from centos
cmd ["ls","-a"]
# 根据 dockerfile 构建镜像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.1 .
sending build context to docker daemon 3.072kb
step 1/2 : from centos
 ---> 0d120b6ccaa8
step 2/2 : cmd ["ls","-a"]
 ---> running in b3f8ba72222b
removing intermediate container b3f8ba72222b
 ---> 561e47f88730
successfully built 561e47f88730
successfully tagged testcmd:0.1	# 构建成功
# 查看镜像
[root@localhost dockerfile]# docker images
repository      tag         image id      created       size
testcmd        0.1         561e47f88730    6 seconds ago    215mb
centos        latest       0d120b6ccaa8    2 days ago     215mb
# 启动镜像 发现ls -a命令生效
[root@localhost dockerfile]# docker run -it testcmd:0.1
.  .dockerenv	dev home lib64    media opt  root sbin	sys usr
.. bin		etc lib  lost+found mnt  proc run  srv	tmp var

# 启动命令中 追加一个 -l, 我们期望的是 ls -a -l,但是 报错,这里将 ls -a 替换成了 -l,最终的命令是 -l 所以报错。
[root@localhost dockerfile]# docker run -it 561e47f88730 -l
docker: error response from daemon: oci runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $path": unknown.
# 将启动命令替换成 docker run -it testcmd:0.1 ls -al, 成功打印详细信息;
[root@localhost dockerfile]# docker run -it testcmd:0.1 ls -al
total 0
drwxr-xr-x.  1 root root  6 aug 13 08:20 .
drwxr-xr-x.  1 root root  6 aug 13 08:20 ..
-rwxr-xr-x.  1 root root  0 aug 13 08:20 .dockerenv
lrwxrwxrwx.  1 root root  7 may 11 2019 bin -> usr/bin
drwxr-xr-x.  5 root root 360 aug 13 08:20 dev
drwxr-xr-x.  1 root root 66 aug 13 08:20 etc
drwxr-xr-x.  2 root root  6 may 11 2019 home
lrwxrwxrwx.  1 root root  7 may 11 2019 lib -> usr/lib
lrwxrwxrwx.  1 root root  9 may 11 2019 lib64 -> usr/lib64
drwx------.  2 root root  6 aug 9 21:40 lost+found
drwxr-xr-x.  2 root root  6 may 11 2019 media
drwxr-xr-x.  2 root root  6 may 11 2019 mnt
drwxr-xr-x.  2 root root  6 may 11 2019 opt
dr-xr-xr-x. 123 root root  0 aug 13 08:20 proc
dr-xr-x---.  2 root root 162 aug 9 21:40 root
drwxr-xr-x. 11 root root 163 aug 9 21:40 run
lrwxrwxrwx.  1 root root  8 may 11 2019 sbin -> usr/sbin
drwxr-xr-x.  2 root root  6 may 11 2019 srv
dr-xr-xr-x. 13 root root  0 aug 11 09:58 sys
drwxrwxrwt.  7 root root 145 aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 aug 9 21:40 var



# 测试二:现在我们把 dockerfile 中 cmd 替换成 entrypoint 后重新构建,运行 看看结果

[root@localhost dockerfile]# cat dockerfile-cmd-test 
from centos
entrypoint ["ls","-a"]
# 重新构建镜像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.2 .
sending build context to docker daemon 3.072kb
step 1/2 : from centos
 ---> 0d120b6ccaa8
step 2/2 : entrypoint ["ls","-a"]
 ---> running in c634ca09fabe
removing intermediate container c634ca09fabe
 ---> 52d295395f08
successfully built 52d295395f08
successfully tagged testcmd:0.2
# 查看镜像
[root@localhost dockerfile]# docker images
repository      tag         image id      created       size
testcmd        0.2         52d295395f08    7 seconds ago    215mb
testcmd        0.1         561e47f88730    12 minutes ago   215mb
centos        latest       0d120b6ccaa8    2 days ago     215mb
# 运行testcmd:0.2镜像 并追加 -l ,发现 打印出了详细信息	
[root@localhost dockerfile]# docker run -it testcmd:0.2 -l
total 0
drwxr-xr-x.  1 root root  6 aug 13 08:17 .
drwxr-xr-x.  1 root root  6 aug 13 08:17 ..
-rwxr-xr-x.  1 root root  0 aug 13 08:17 .dockerenv
lrwxrwxrwx.  1 root root  7 may 11 2019 bin -> usr/bin
drwxr-xr-x.  5 root root 360 aug 13 08:17 dev
drwxr-xr-x.  1 root root 66 aug 13 08:17 etc
drwxr-xr-x.  2 root root  6 may 11 2019 home
lrwxrwxrwx.  1 root root  7 may 11 2019 lib -> usr/lib
lrwxrwxrwx.  1 root root  9 may 11 2019 lib64 -> usr/lib64
drwx------.  2 root root  6 aug 9 21:40 lost+found
drwxr-xr-x.  2 root root  6 may 11 2019 media
drwxr-xr-x.  2 root root  6 may 11 2019 mnt
drwxr-xr-x.  2 root root  6 may 11 2019 opt
dr-xr-xr-x. 121 root root  0 aug 13 08:17 proc
dr-xr-x---.  2 root root 162 aug 9 21:40 root
drwxr-xr-x. 11 root root 163 aug 9 21:40 run
lrwxrwxrwx.  1 root root  8 may 11 2019 sbin -> usr/sbin
drwxr-xr-x.  2 root root  6 may 11 2019 srv
dr-xr-xr-x. 13 root root  0 aug 11 09:58 sys
drwxrwxrwt.  7 root root 145 aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 aug 9 21:40 var

dockerfile中很多命令都十分相似,我们需要了解他的区别,最好的学习就是对比进行测试查看效果!

实战:tomcat镜像

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

[root@localhost tomcat]# ls
apache-tomcat-8.5.43.tar.gz dockerfile jdk-8u211-linux-x64.tar.gz read.txt

2、编写dockerfile文件, 官方命名dockerfile,build会自动寻找这个文件,不需要 -f 指定!

# 引入基础
from centos
# 作者信息
maintainer yinxiaodoong<m15010969094@163.com>

# copy文件read.txt到容器的 /usr/local/read.txt 目录下
copy read.txt /usr/local/read.txt

# 添加jdk tomcat ---> 自动解压
add jdk-8u211-linux-x64.tar.gz /usr/local/
add apache-tomcat-8.5.43.tar.gz /usr/local/

# 安装 vim 命令
run yum install -y vim


# 配置 mypath
env mypath /usr/local/
# 指定 进入容器默认路径 /user/local/ 
workdir $mypath


# 配置jdk环境变量
env java_home /usr/local/jdk1.8.0_211
env classpath $java_home/lib/dt.jat:$java_home/lib/tools.jar

# 配置tomcat环境变量
env catalina_home /usr/local/apache-tomcat-8.5.43
env catalina_bash /usr/local/apache-tomcat-8.5.43

env path $path:$java_home/bin:$catalina_home/lib:$catalina_home/bin


# 暴露8080端口
expose 8080


# /usr/local/tomcat/webapps# 
# 启动容器时 启动tomcat
cmd /usr/local/apache-tomcat-8.5.43/bin/startup.sh && tail -f /usr/local/apache-tomcat-8.5.43/logs/catalina.out 

# 构建tomcat镜像
[root@localhost tomcat]# docker build -t mytomcat 
[root@localhost tomcat]# docker images
repository     tag         image id      created       size
mytomcat      latest       a9707559afa1    8 seconds ago    693mb
centos       latest       0d120b6ccaa8    4 days ago     215mb


[root@localhost tomcat]# docker run -d -p 8081:8080 --name mytomcat--01 -v /home/yinxiaodong/build/tomcat/test:/usr/local/apache-tomcat-8.5.43/webapps/test -v /home/yinxiaodong/build/tomcat/logs:/usr/local/apache-tomcat-8.5.43/logs mytomcat

发布自己的镜像

发布到 dockerhub

地址   注册自己的账号

确定自己的账号可以登录

在我们的服务器上提交

[root@localhost logs]# docker login --help

usage:	docker login [options] [server]

log in to a docker registry.
if no server is specified, the default is defined by the daemon.

options:
 -p, --password string  password
   --password-stdin  take the password from stdin
 -u, --username string  username
[root@localhost logs]# 
[root@localhost logs]# docker login -u xxx -p xxx
warning! using --password via the cli is insecure. use --password-stdin.
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

登录完毕后就可以提交镜像了

[root@localhost logs]# docker push xxx/mytomcat:1.0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网