How to use Dockerfile to create a mirror of the Java runtime environment

How to use Dockerfile to create a mirror of the Java runtime environment

The current environment is:

  • Centos 7.5
  • docker-ce 18.06.1-ce

1. First use the basic image of centos7.5.1804 to install some environments required for operation

Create a corresponding file directory in the /app directory

[root@node2 /app/]# mkdir dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -pv
[root@node2 /app]# cd dockerfile/system/centos/
[root@node2 /app/dockerfile/system/centos]# mkdir centos-7.5-base
[root@node2 /app/dockerfile/system/centos]# cd centos-7.5-base

Create a Dockerfile

[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#vim Dockerfile 
#Nginx Base Image
FROM centos:7.5.1804

LABEL maintaier "mr.luo <[email protected]>"

RUN yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop

Create a docker build script and execute it directly

[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#vim build-command.sh 
#!/bin/bash
docker build -t 172.20.7.50/baseimages/centos-base:7.5.1804 .

Execute the script to create a new image

[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#bash build-command.sh 
Sending build context to Docker daemon 3.072kB
Step 1/3: FROM centos:7.5.1804
---> 76d6bc25b8a5
Step 2/3: LABEL maintaier '[email protected]'
---> Using cache
---> 05ccd970d71d
Step 3/3 : RUN yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
---> Using cache
---> 73d683a54877
Successfully built 73d683a54877
Successfully tagged 172.20.7.50/baseimages/centos-base:7.5.1804

2. Use the prepared basic environment image to create a jdk image

Exit from centos-7.5-base and create a jdk1.8 directory in the current directory.

[root@node2 /app/dockerfile/system/centos/centos-7.5-base]# cd ..
[root@node2 /app/dockerfile/system/centos]# mkdir jdk1.8
[root@node2 /app/dockerfile/system/centos]# cd jdk1.8/

Create Dockerfile

[root@node2 /app/dockerfile/system/centos/jdk1.8]#vim Dockerfile 
FROM 172.20.7.50/baseimages/centos-base:7.5.1804
LABEL maintainer "mr.luo <[email protected]>"

ADD jdk-8u162-linux-x64.tar.gz /usr/local/src/
RUN ln -s /usr/local/src/jdk1.8.0_162/ /usr/local/jdk

ADD profile /etc/profile

ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin

RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

Upload the jdk package to the current directory:

Copy the /etc/profile file to the current directory

[root@node2 /app/dockerfile/system/centos/jdk1.8]#cp profile /etc/profile

Add the JDK environment variable at the end of the profile

[root@node2 /app/dockerfile/system/centos/jdk1.8]#vim profile 
export JAVA_HOME=/usr/local/jdk
export TOMCAT_HOME=/apps/tomcat
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

Create a shell script for docker build

[root@node2 /app/dockerfile/system/centos/jdk1.8]#vim build-command.sh 
#!/bin/bash
#
docker build -t 172.20.7.50/baseimages/centos7.5-jdk:8.162 .

Start making images

[root@node2 /app/dockerfile/system/centos/jdk1.8]#bash build-command.sh 
  Sending build context to Docker daemon 189.8MB
  Step 1/10: FROM 172.20.7.50/baseimages/centos-base:7.5.1804
   ---> 73d683a54877
  Step 2/10: LABEL maintainer "mr.luo <[email protected]>"
   ---> Running in 65604dd1f392
  Removing intermediate container 65604dd1f392
   ---> c4720603ce38
  Step 3/10 : ADD jdk-8u162-linux-x64.tar.gz /usr/local/src/
   ---> bc98adffe1b4
  Step 4/10 : RUN ln -s /usr/local/src/jdk1.8.0_162/ /usr/local/jdk
   ---> Running in df5a6f67f9fd
  Removing intermediate container df5a6f67f9fd
   ---> 0ae1af0416c6
  Step 5/10 : ADD profile /etc/profile
   ---> eee23a69c0c8
  Step 6/10 : ENV JAVA_HOME /usr/local/jdk
   ---> Running in edbef8563e83
  Removing intermediate container edbef8563e83
   ---> 5f783f642054
  Step 7/10 : ENV JRE_HOME $JAVA_HOME/jre
   ---> Running in fa0e5f08e732
  Removing intermediate container fa0e5f08e732
   ---> 28a4d31463d4
  Step 8/10 : ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
   ---> Running in 3c4ebb04ac62
  Removing intermediate container 3c4ebb04ac62
   ---> 245f2dd82d52
  Step 9/10 : ENV PATH $PATH:$JAVA_HOME/bin
   ---> Running in 4f5e6093f0a9
  Removing intermediate container 4f5e6093f0a9
   ---> 5be0e6261eea
  Step 10/10 : RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
   ---> Running in 52d8cb8463a8
  Removing intermediate container 52d8cb8463a8
   ---> 9fb867ae8c39
  Successfully built 9fb867ae8c39
  Successfully tagged 172.20.7.50/baseimages/centos7.5-jdk:8.162

View the files in the current directory

[root@node2 /app/dockerfile/system/centos/jdk1.8]#ls
build-command.sh Dockerfile jdk-8u162-linux-x64.tar.gz profile

Check whether the prepared image can be used normally

[root@node2 /app/dockerfile/system/centos/jdk1.8]#docker run -it --rm 172.20.7.50/baseimages/centos7.5-jdk:8.162 bash
[root@919844b164dc /]# java -version 
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
[root@919844b164dc /]# date
Thu Nov 22 21:17:49 CST 2018
[root@919844b164dc /]# exit
exit

3. Make a Tomcat image

Enter the previously built /app/dockerfile/web/tomcat and create a tomcat-base directory

[root@node2 ~]# cd /app/dockerfile/web/tomcat 
[root@node2 /app/dockerfile/web/tomcat]#mkdir tomcat-base

Create Dockerfile

[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#vim Dockerfile 
FROM 172.20.7.50/baseimages/centos7.5-jdk:8.162

LABEL maintainer "mr.luo <[email protected]>"

RUN mkdir /apps
ADD apache-tomcat-8.5.33.tar.gz /apps
RUN ln -s /apps/apache-tomcat-8.5.33 /apps/tomcat

Create a docker build script

[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#vim build-command.sh 
#!/bin/bash

docker build -t 172.20.7.50/baseimages/centos-tomcat:8.5.33 .

Execute to create the image file:

[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#bash build-command.sh 
  Sending build context to Docker daemon 9.625MB
  Step 1/5: FROM 172.20.7.50/baseimages/centos7.5-jdk:8.162
   ---> 9fb867ae8c39
  Step 2/5: LABEL maintainer "mr.luo <[email protected]>"
   ---> Running in 9ce6fc4d2850
  Removing intermediate container 9ce6fc4d2850
   ---> b68755061b28
  Step 3/5 : RUN mkdir /apps
   ---> Running in b483c6b127f2
  Removing intermediate container b483c6b127f2
   ---> 605c1a048a5f
  Step 4/5 : ADD apache-tomcat-8.5.33.tar.gz /apps
   ---> 3c44f96ed41c
  Step 5/5 : RUN ln -s /apps/apache-tomcat-8.5.33 /apps/tomcat
   ---> Running in 4c1aa39a6c92
  Removing intermediate container 4c1aa39a6c92
   ---> 9b3bc4f58cc9
  Successfully built 9b3bc4f58cc9
  Successfully tagged 172.20.7.50/baseimages/centos-tomcat:8.5.33

Use the created image to start a container to check whether it is successfully created. Add -p to expose the port when starting it and test it on a physical machine

[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#docker run -it -p 8802:8080 172.20.7.50/baseimages/centos-tomcat:8.5.33 bash  
[root@917b2c2262a3 /]# cd /apps/
[root@917b2c2262a3 apps]# ll
total 0
drwxr-xr-x 9 root root 220 Nov 22 22:08 apache-tomcat-8.5.33
lrwxrwxrwx 1 root root 26 Nov 22 22:08 tomcat -> /apps/apache-tomcat-8.5.33
[root@917b2c2262a3 apps]# ./tomcat/bin/catalina.sh start
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.

Test with a browser on the client side

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile
  • How to write the best Dockerfile
  • Detailed explanation of COPY and ADD commands in Dockerfile
  • A detailed introduction to the Dockerfile image building file and related commands in Docker
  • Detailed explanation of using Dockerfile to build MySQL image and implement data initialization and permission setting
  • Detailed explanation of the specific use of the ENV instruction in Dockerfile
  • Dockerfile usage examples
  • Dockerfile to create the official Tomcat image and detailed explanation of image usage
  • Summary of common commands in Dockerfile
  • How to deploy SpringBoot project using Dockerfile
  • How to create your own image using Dockerfile
  • Introduction to Dockerfile instructions ADD and COPY
  • Detailed explanation of multi-stage (multi-stage build) in Dockerfile
  • Docker Basics: Detailed Explanation of Dockerfile Commands
  • How to deploy nodejs service using Dockerfile
  • Dockerfile instructions explained
  • A brief introduction to the Dockerfile instruction VOLUME
  • Dockerfile simple introduction

<<:  Five ways to traverse JavaScript arrays

>>:  The difference between MySQL execute, executeUpdate and executeQuery

Recommend

How to use & and nohup in the background of Linux

When we work in a terminal or console, we may not...

The difference between GB2312, GBK and UTF-8 in web page encoding

First of all, we need to understand that GB2312, ...

Full steps to create a high-performance index in MySQL

Table of contents 1. Index Basics 1. Types of Ind...

Detailed example of locating and optimizing slow query sql in MySQL

Table of contents 1. How to locate and optimize s...

Detailed explanation of DOM style setting in four react components

1. Inline styles To add inline styles to the virt...

Vue makes div height draggable

This article shares the specific code of Vue to r...

React implementation example using Amap (react-amap)

The PC version of React was refactored to use Ama...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...

In-depth interpretation of /etc/fstab file in Linux system

Preface [root@localhost ~]# cat /etc/fstab # # /e...

Several implementation methods of the tab bar (recommended)

Tabs: Category + Description Tag bar: Category =&...

Example of how to implement keepalived+nginx high availability

1. Introduction to keepalived Keepalived was orig...

MySQL database master-slave replication and read-write separation

Table of contents 1. Master-slave replication Mas...

How to optimize MySQL query speed

In the previous chapters, we introduced how to ch...

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...