十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要讲解了“Docker怎么构建JAVA环境镜像”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Docker怎么构建JAVA环境镜像”吧!
目前创新互联已为超过千家的企业提供了网站建设、域名、虚拟空间、成都网站托管、企业网站设计、富裕网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
1\ 需要提前下载软件包jdk和tomcat
[root@docker-master ~]# vim Dockerfile-Java FROM centos:6 MAINTAINER gujiwork ADD jdk-8u151-linux-x64.tar.gz /usr/local ENV JAVA_HOME /usr/local/jdk1.8.0_151 ADD apache-tomcat-7.0.82.tar.gz /usr/local WORKDIR /usr/local/apache-tomcat-7.0.82 ENTRYPOINT ["bin/catalina.sh","run"] #暴露8080端口 EXPOSE 8080
2\执行build之后便开始构建成镜像
[root@docker-master ~]# docker build -t tomcat:v1 -f ./Dockerfile-Java . Sending build context to Docker daemon 3.045GB Step 1/8 : FROM centos:6 6: Pulling from library/centos 9bfcefca2b8d: Pull complete Digest: sha256:573923afbed3faf8000ad6eb333ebeb1d46e753d3ae23a6f59b528c7e1531747 Status: Downloaded newer image for centos:6 ---> 0cbf37812bff Step 2/8 : MAINTAINER gujiwork ---> Running in 2205a6b1c70d ---> 807741f8ef42 Removing intermediate container 2205a6b1c70d Step 3/8 : ADD jdk-8u151-linux-x64.tar.gz /usr/local ---> 617e8b4fa97d Step 4/8 : ENV JAVA_HOME /usr/local/jdk1.8.0_151 ---> Running in deded76bd1f4 ---> bfe5b16b5dfb Removing intermediate container deded76bd1f4 Step 5/8 : ADD apache-tomcat-7.0.82.tar.gz /usr/local ---> ad01015a58f5 Step 6/8 : WORKDIR /usr/local/tomcat ---> 70c90ddeb127 Removing intermediate container 385c8cafef5b Step 7/8 : ENTRYPOINT bin/catalina.sh run ---> Running in fb186dfb8068 ---> 0c9299e34658 Removing intermediate container fb186dfb8068 Step 8/8 : EXPOSE 8080 ---> Running in 44161cdc051b ---> b0ca287e65ba Removing intermediate container 44161cdc051b Successfully built b0ca287e65ba Successfully tagged tomcat:v1
3\可通过docker images查看构建的镜像
[root@docker-master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat v1 b0ca287e65ba 2 minutes ago 592MB nginx latest 568c4670fa80 2 weeks ago 109MB centos 6 0cbf37812bff 2 months ago 194MB centos latest 3fa822599e10 12 months ago 204MB
4\ 启动docker tomcat容器,使用宿主机IP访问页面进行测试.
[root@docker-master ~]# docker run -itd --name web01 -p 80:8080 tomcat:v1 2c4d214c8ce9aef03409d7e02a43ed10247480de22c3b9d9c31b4e8d73eca45f [root@docker-master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2c4d214c8ce9 tomcat:v1 "bin/catalina.sh run" 29 seconds ago Up 27 seconds 0.0.0.0:80->8080/tcp web01 # 查看本机网卡IP地址 [root@docker-master ~]# ifconfig ens33 ens33: flags=4163mtu 1500 inet 172.18.18.10 netmask 255.255.255.0 broadcast 172.18.18.255 inet6 fe80::20c:29ff:fe80:de01 prefixlen 64 scopeid 0x20 ether 00:0c:29:80:de:01 txqueuelen 1000 (Ethernet) RX packets 55747 bytes 72954144 (69.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 30192 bytes 2373785 (2.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
5\配置开启SSH远程登录,方便测试及开发进行相关的排错查看
[root@docker-master ~]# vim Dockerfile-Java-ssh FROM centos:6 MAINTAINER gujiwork ENV JAVA_HOME /usr/local/jdk1.8.0_151 ENV ROOT_PASSWORD 123456 ADD jdk-8u151-linux-x64.tar.gz /usr/local ADD apache-tomcat-7.0.82.tar.gz /usr/local COPY ./start.sh /usr/local/start.sh RUN yum install -y openssh-server && \ echo $ROOT_PASSWORD |passwd --stdin root && \ ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && \ ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \ echo 'UseDNS no' >> /etc/ssh/sshd_config && \ sed -i -e '/pam_loginuid.so/d' /etc/pam.d/sshd && \ chmod +x /usr/local/start.sh && \ rm -rf /usr/local/apache-tomcat-7.0.82.tar.gz WORKDIR /usr/local/apache-tomcat-7.0.82 ENTRYPOINT ["/usr/local/start.sh","run"] #暴露8080和22端口 EXPOSE 8080 EXPOSE 22
6\start.sh脚本,用于启动容器内的多个服务
[root@docker-master ~]# vim start.sh #!/bin/bash TomcatDir="/usr/local/apache-tomcat-7.0.82" cd $TomcatDir ; ./bin/startup.sh /usr/sbin/sshd -D tail -f /var/log/*
7\重新构建镜像,tag标记为tomcat:v2
[root@docker-master ~]# docker build -t tomcat:v2 -f Dockerfile-Java-ssh . Sending build context to Docker daemon 3.045GB Step 1/15 : FROM centos:6 ---> 0cbf37812bff Step 2/15 : MAINTAINER gujiwork ---> Using cache ---> 807741f8ef42 Step 3/15 : ENV ROOT_PASSWORD 123456 ---> Running in b01f3ce7ae37 ---> 47ec926ccf9c Removing intermediate container b01f3ce7ae37 Step 4/15 : ADD jdk-8u151-linux-x64.tar.gz /usr/local ---> 33bb1bfe21ce Step 5/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_151 ---> Running in 53d4f20f8a3e ---> 4589c805fe03 Removing intermediate container 53d4f20f8a3e Step 6/15 : ADD apache-tomcat-7.0.82.tar.gz /usr/local ---> 2b9b81efe22c Step 7/15 : WORKDIR /usr/local/apache-tomcat-7.0.82 ---> 4e0132cba433 Removing intermediate container d3629c03c293 Step 8/15 : ENTRYPOINT bin/catalina.sh run ---> Running in 3149b8be9f3e ---> 4a0d49525705 Removing intermediate container 3149b8be9f3e Step 9/15 : RUN yum install -y openssh-server ---> Running in 54d1c22da719 Loaded plugins: fastestmirror, ovl Setting up Install Process http://mirrors.njupt.edu.cn/centos/6.10/updates/x86_64/repodata/e38a3ccfa43be7ce9c19045799166dc3424cba6ca0f49b19d4c1b48707e7597f-primary.sqlite.bz2: [Errno 14] PYCURL ERROR 7 - "couldn't connect tohost" Trying other mirror. Resolving Dependencies --> Running transaction check ---> Package openssh-server.x86_64 0:5.3p1-123.el6_9 will be installed --> Processing Dependency: openssh = 5.3p1-123.el6_9 for package: openssh-server-5.3p1-123.el6_9.x86_64 --> Processing Dependency: /sbin/service for package: openssh-server-5.3p1-123.el6_9.x86_64 --> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-5.3p1-123.el6_9.x86_64 --> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-5.3p1-123.el6_9.x86_64 --> Running transaction check ---> Package fipscheck-lib.x86_64 0:1.2.0-7.el6 will be installed --> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.2.0-7.el6.x86_64 ---> Package initscripts.x86_64 0:9.03.61-1.el6.centos will be installed --> Processing Dependency: util-linux-ng >= 2.16 for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: upstart >= 0.6.5-11 for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: udev >= 125-1 for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: sysvinit-tools >= 2.87-6 for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: plymouth for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: /sbin/pidof for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: /sbin/ip for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: /sbin/blkid for package: initscripts-9.03.61-1.el6.centos.x86_64 --> Processing Dependency: /sbin/arping for package: initscripts-9.03.61-1.el6.centos.x86_64 ---> Package openssh.x86_64 0:5.3p1-123.el6_9 will be installed ---> Package tcp_wrappers-libs.x86_64 0:7.6-58.el6 will be installed --> Running transaction check ---> Package fipscheck.x86_64 0:1.2.0-7.el6 will be installed ---> Package iproute.x86_64 0:2.6.32-57.el6 will be installed --> Processing Dependency: iptables >= 1.4.5 for package: iproute-2.6.32-57.el6.x86_64 --> Processing Dependency: libxtables.so.4()(64bit) for package: iproute-2.6.32-57.el6.x86_64 ---> Package iputils.x86_64 0:20071127-24.el6 will be installed ---> Package plymouth.x86_64 0:0.8.3-29.el6.centos will be installed --> Processing Dependency: system-logos for package: plymouth-0.8.3-29.el6.centos.x86_64 --> Processing Dependency: libdrm_radeon.so.1()(64bit) for package: plymouth-0.8.3-29.el6.centos.x86_64 --> Processing Dependency: libdrm_nouveau.so.1()(64bit) for package: plymouth-0.8.3-29.el6.centos.x86_64 --> Processing Dependency: libdrm_intel.so.1()(64bit) for package: plymouth-0.8.3-29.el6.centos.x86_64 --> Processing Dependency: libdrm.so.2()(64bit) for package: plymouth-0.8.3-29.el6.centos.x86_64 ---> Package sysvinit-tools.x86_64 0:2.87-6.dsf.el6 will be installed ---> Package udev.x86_64 0:147-2.73.el6_8.2 will be installed --> Processing Dependency: hwdata for package: udev-147-2.73.el6_8.2.x86_64 ---> Package upstart.x86_64 0:0.6.5-17.el6 will be installed ---> Package util-linux-ng.x86_64 0:2.17.2-12.28.el6_9.2 will be installed --> Running transaction check ---> Package hwdata.noarch 0:0.233-20.1.el6 will be installed ---> Package iptables.x86_64 0:1.4.7-19.el6 will be installed --> Processing Dependency: policycoreutils for package: iptables-1.4.7-19.el6.x86_64 ---> Package libdrm.x86_64 0:2.4.65-2.el6 will be installed --> Processing Dependency: libpciaccess.so.0()(64bit) for package: libdrm-2.4.65-2.el6.x86_64 ---> Package redhat-logos.noarch 0:60.0.14-12.el6.centos will be installed --> Running transaction check ---> Package libpciaccess.x86_64 0:0.13.4-1.el6 will be installed ---> Package policycoreutils.x86_64 0:2.0.83-30.1.el6_8 will be installed --> Processing Dependency: libdbus-glib-1.so.2()(64bit) for package: policycoreutils-2.0.83-30.1.el6_8.x86_64 --> Running transaction check ---> Package dbus-glib.x86_64 0:0.86-6.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: openssh-server x86_64 5.3p1-123.el6_9 base 329 k Installing for dependencies: dbus-glib x86_64 0.86-6.el6 base 170 k fipscheck x86_64 1.2.0-7.el6 base 14 k fipscheck-lib x86_64 1.2.0-7.el6 base 8.3 k hwdata noarch 0.233-20.1.el6 base 1.4 M initscripts x86_64 9.03.61-1.el6.centos base 949 k iproute x86_64 2.6.32-57.el6 base 386 k iptables x86_64 1.4.7-19.el6 base 255 k iputils x86_64 20071127-24.el6 base 121 k libdrm x86_64 2.4.65-2.el6 base 136 k libpciaccess x86_64 0.13.4-1.el6 base 24 k openssh x86_64 5.3p1-123.el6_9 base 277 k plymouth x86_64 0.8.3-29.el6.centos base 89 k policycoreutils x86_64 2.0.83-30.1.el6_8 base 663 k redhat-logos noarch 60.0.14-12.el6.centos base 15 M sysvinit-tools x86_64 2.87-6.dsf.el6 base 60 k tcp_wrappers-libs x86_64 7.6-58.el6 base 62 k udev x86_64 147-2.73.el6_8.2 base 358 k upstart x86_64 0.6.5-17.el6 base 177 k util-linux-ng x86_64 2.17.2-12.28.el6_9.2 base 1.6 M Transaction Summary ================================================================================ Install 20 Package(s) Total download size: 21 M Installed size: 42 M Downloading Packages: -------------------------------------------------------------------------------- Total 7.3 MB/s | 21 MB 00:02 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Importing GPG key 0xC105B9DE: Userid : CentOS-6 Key (CentOS 6 Official Signing Key)Package: centos-release-6-10.el6.centos.12.3.x86_64 (@CentOS/6.10) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : sysvinit-tools-2.87-6.dsf.el6.x86_64 1/20 Installing : hwdata-0.233-20.1.el6.noarch 2/20 Installing : libpciaccess-0.13.4-1.el6.x86_64 3/20 Installing : fipscheck-lib-1.2.0-7.el6.x86_64 4/20 Installing : fipscheck-1.2.0-7.el6.x86_64 5/20 Installing : dbus-glib-0.86-6.el6.x86_64 6/20 Installing : redhat-logos-60.0.14-12.el6.centos.noarch 7/20 Installing : upstart-0.6.5-17.el6.x86_64 8/20 Installing : policycoreutils-2.0.83-30.1.el6_8.x86_64 9/20 Installing : iptables-1.4.7-19.el6.x86_64 10/20 Installing : iproute-2.6.32-57.el6.x86_64 11/20 Installing : libdrm-2.4.65-2.el6.x86_64 12/20 Installing : iputils-20071127-24.el6.x86_64 13/20 Installing : plymouth-0.8.3-29.el6.centos.x86_64 14/20 Installing : util-linux-ng-2.17.2-12.28.el6_9.2.x86_64 15/20 install-info: No such file or directory for /usr/share/info/ipc.info Installing : initscripts-9.03.61-1.el6.centos.x86_64 16/20 Installing : udev-147-2.73.el6_8.2.x86_64 17/20 Installing : openssh-5.3p1-123.el6_9.x86_64 18/20 Installing : tcp_wrappers-libs-7.6-58.el6.x86_64 19/20 Installing : openssh-server-5.3p1-123.el6_9.x86_64 20/20 Verifying : plymouth-0.8.3-29.el6.centos.x86_64 1/20 Verifying : tcp_wrappers-libs-7.6-58.el6.x86_64 2/20 Verifying : policycoreutils-2.0.83-30.1.el6_8.x86_64 3/20 Verifying : fipscheck-1.2.0-7.el6.x86_64 4/20 Verifying : upstart-0.6.5-17.el6.x86_64 5/20 Verifying : redhat-logos-60.0.14-12.el6.centos.noarch 6/20 Verifying : udev-147-2.73.el6_8.2.x86_64 7/20 Verifying : initscripts-9.03.61-1.el6.centos.x86_64 8/20 Verifying : iptables-1.4.7-19.el6.x86_64 9/20 Verifying : openssh-5.3p1-123.el6_9.x86_64 10/20 Verifying : hwdata-0.233-20.1.el6.noarch 11/20 Verifying : sysvinit-tools-2.87-6.dsf.el6.x86_64 12/20 Verifying : util-linux-ng-2.17.2-12.28.el6_9.2.x86_64 13/20 Verifying : iputils-20071127-24.el6.x86_64 14/20 Verifying : libdrm-2.4.65-2.el6.x86_64 15/20 Verifying : dbus-glib-0.86-6.el6.x86_64 16/20 Verifying : libpciaccess-0.13.4-1.el6.x86_64 17/20 Verifying : openssh-server-5.3p1-123.el6_9.x86_64 18/20 Verifying : iproute-2.6.32-57.el6.x86_64 19/20 Verifying : fipscheck-lib-1.2.0-7.el6.x86_64 20/20 Installed: openssh-server.x86_64 0:5.3p1-123.el6_9 Dependency Installed: dbus-glib.x86_64 0:0.86-6.el6 fipscheck.x86_64 0:1.2.0-7.el6 fipscheck-lib.x86_64 0:1.2.0-7.el6 hwdata.noarch 0:0.233-20.1.el6 initscripts.x86_64 0:9.03.61-1.el6.centos iproute.x86_64 0:2.6.32-57.el6 iptables.x86_64 0:1.4.7-19.el6 iputils.x86_64 0:20071127-24.el6 libdrm.x86_64 0:2.4.65-2.el6 libpciaccess.x86_64 0:0.13.4-1.el6 openssh.x86_64 0:5.3p1-123.el6_9 plymouth.x86_64 0:0.8.3-29.el6.centos policycoreutils.x86_64 0:2.0.83-30.1.el6_8 redhat-logos.noarch 0:60.0.14-12.el6.centos sysvinit-tools.x86_64 0:2.87-6.dsf.el6 tcp_wrappers-libs.x86_64 0:7.6-58.el6 udev.x86_64 0:147-2.73.el6_8.2 upstart.x86_64 0:0.6.5-17.el6 util-linux-ng.x86_64 0:2.17.2-12.28.el6_9.2 Complete! ---> c488b880f48b Removing intermediate container 54d1c22da719 Step 10/15 : RUN echo $ROOT_PASSWORD |passwd --stdin root ---> Running in 9906618c76ea Changing password for user root. passwd: all authentication tokens updated successfully. ---> 473b44862b8b Removing intermediate container 9906618c76ea Step 11/15 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key ---> Running in 1ff47b661b7d Generating public/private dsa key pair. Your identification has been saved in /etc/ssh/ssh_host_dsa_key. Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub. The key fingerprint is: c7:17:da:ca:fc:9c:b4:d6:45:fb:db:b0:93:a3:23:75 root@1ff47b661b7d The key's randomart image is: +--[ DSA 1024]----+ | | | | | . | | . o . .| | S + o ..| | + o. E..| | +..o.o.| | .+oo=oo| | o*o.+o| +-----------------+ Enter passphrase (empty for no passphrase): Enter same passphrase again: ---> 956b3bf9ecc1 Removing intermediate container 1ff47b661b7d Step 12/15 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key ---> Running in 1a1c8be34244 Generating public/private rsa key pair. Your identification has been saved in /etc/ssh/ssh_host_rsa_key. Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub. The key fingerprint is: fd:9c:57:31:62:06:0e:df:8d:af:60:c8:d7:75:77:f3 root@1a1c8be34244 The key's randomart image is: +--[ RSA 2048]----+ | . . | | + o o | | o * *+| | ... + + O| | So.+ . .E| | oo.... | | +.. | | . | | | +-----------------+ Enter passphrase (empty for no passphrase): Enter same passphrase again: ---> dd3bcb4ab9ae Removing intermediate container 1a1c8be34244 Step 13/15 : CMD /usr/bin/sshd -D ---> Running in 09c2eca527ce ---> 02941cde6997 Removing intermediate container 09c2eca527ce Step 14/15 : EXPOSE 8080 ---> Running in f1f09cd58b26 ---> c6cbbb630d02 Removing intermediate container f1f09cd58b26 Step 15/15 : EXPOSE 22 ---> Running in 98ca940bb8c2 ---> 1f569892a3d7 Removing intermediate container 98ca940bb8c2 Successfully built 1f569892a3d7 Successfully tagged tomcat:v2
8\启动容器 -p 表示映射到宿主机的固定端口, -P为随机端口
[root@docker-master ~]# docker run -itd --name web02 -p 80:8080 -p 2222:22 tomcat:v2 9cc734888d1f87bbbad4536c05dc1a62bae4f12ae8d88560067d1d89039f2ca1
9\ 测试远程连接是否可以登陆
10\此时便可以通过宿主机映射的2222端口进行连接了.
感谢各位的阅读,以上就是“Docker怎么构建JAVA环境镜像”的内容了,经过本文的学习后,相信大家对Docker怎么构建JAVA环境镜像这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!