Home Java javaTutorial How to use Dockerfile to create an image of the java running environment

How to use Dockerfile to create an image of the java running environment

May 05, 2023 pm 01:49 PM
java dockerfile

The current environment used 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 the 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
Copy after login

Create a dockerfile file

[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#vim dockerfile 
#nginx base image
from centos:7.5.1804

label maintaier "mr.luo <mr.luo@dklwj.com>"

run yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
Copy after login

Create a direct docker build script and then execute the script 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 .
Copy after login

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 &#39;mr.luo@dklwj.com&#39;
---> 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
Copy after login

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/
Copy after login

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 <mr.luo@dklwj.com>"

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
Copy after login

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
Copy after login

Add jdk at the end of the profile Environment variables

[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
Copy after login

Create docker build shell script

[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 .
Copy after login

Start making the image

[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 <mr.luo@dklwj.com>"
   ---> 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
Copy after login

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
Copy after login

Check the made image Whether it 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
Copy after login

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
Copy after login

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 <mr.luo@dklwj.com>"

run mkdir /apps
add apache-tomcat-8.5.33.tar.gz /apps
run ln -s /apps/apache-tomcat-8.5.33 /apps/tomcat
Copy after login

Create 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 .
Copy after login

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 <mr.luo@dklwj.com>"
   ---> 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
Copy after login

Use the created image to start a container Check whether it is successfully created, add -p to expose the port when starting, and test it on the 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.
Copy after login

Test with the browser on the client

The above is the detailed content of How to use Dockerfile to create an image of the java running environment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles