How to build a tomcat image based on Dockerfile

How to build a tomcat image based on Dockerfile

Dockerfile is a file used to build a docker image! Command parameter script!

Build steps:

  • Write a Dockerfile
  • Docker build builds a mirror
  • docker run runs the image
  • docker push publishes images (DockerHub, Alibaba Cloud Image Repository)
# Take contes as an example to view the dockerfile of contes on dockerhub

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 build process

Basics:

  • Each reserved keyword (command) must be capitalized.
  • Execution order is from top to bottom
  • Indicates comments
  • Each instruction creates and submits a new image layer.

Dockerfile is development-oriented. If we want to publish projects and make images in the future, we need to write a Dockerfile file. This file is very simple!

Docker images are gradually becoming the standard for enterprise delivery!

DockerFile: builds the image, defines all the steps and source code;

DockerImages: An image generated by building a DockerFile. This image is the product we finally release and run!

Docker container: A container is an image that runs to provide services!

DockerFile instructions

FROM # Base image MAINTAINER # Who wrote the image? Name + Email RUN # Commands to be run when building the image ADD # Steps eg: tomcat image---》Put in the compressed package of tomcat! Add contentWORKDIR # The working directory of the imageVOLUME # The mounted directory locationEXPOST # Expose port configurationCMD # Specify the command to run when this container starts. Only the last one will take effect and can be replacedENTRYPOINT # Specify the command to run when this container starts. You can append commandsONBUILD # When building an inherited DockerFile, the ONBUILD instruction will be run, triggering the instructionCOPY # Similar to ADD, copy our files to the imageENV # Set environment variables when building

Field test

99% of the images in Docker Hub are based on the FROM scratch base image, and then configured with the software and configuration we need to build

Create your own centos

# 1. Write the DockerFile file FROM centos
# Author information MAINTAINER yinxiaodong<m15010969094@163.com>
# Configure environment variable ENV MYPATH /user/local
# Specify the working directory of the image WORKDIR $MYPATH 
 
RUN yum install -y vim
RUN yum install -y net-tools
# Expose port 80 EXPOSE 80
 
CMD echo $MYPATH
CMD echo "---------end--------"
CMD /bin/bash


# 2. Build an image through files# Command docker build -f dockerfile file path -t image name: version number.
[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

# Start mycentos and use ifconfig command to view IP information [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
# Check the directory /user/local which is exactly the directory we specified in dockerfile [root@494c2bc72263 local]# pwd
/user/local

View the build history of an image

# Command docker history image 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

When we get an image, we can study how it is made---->docker history image id

The difference between CMD and ENTRYPOINT

CMD #Specify the command to run when this container starts. Only the last one will take effect and can be replaced. ENTRYPOINT #Specify the command to run when this container starts. You can append commands. #Test CMD
# Write DockerFile file [root@localhost dockerfile]# cat dockerfile-cmd-test 
FROM centos
CMD ["ls","-a"]
# Build image based on 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 # Build successfully# View the image [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
# Start the image and find that the ls -a command is effective [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

# Append a -l to the startup command. We expect ls -a -l, but an error is reported. Here, ls -a is replaced with -l. The final command is -l, so an error is reported.
[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.
# Replace the startup command with docker run -it testcmd:0.1 ls -al, and print detailed information successfully;
[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



# Test 2: Now we replace CMD in DockerFile with ENTRYPOINT and rebuild it, and run it to see the result [root@localhost dockerfile]# cat dockerfile-cmd-test 
FROM centos
ENTRYPOINT ["ls","-a"]
# Rebuild the image [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
# View the image [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
# Run testcmd:0.2 image and append -l, and find that detailed information is printed	
[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

Many commands in DockerFile are very similar. We need to understand their differences. The best way to learn is to compare and test them to see the results!

Actual combat: Tomcat image

1. Prepare the image file tomcat compressed package jdk compressed package

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

2. Write the Dockerfile file, officially named Dockerfile, build will automatically find this file, no -f specification is required!

# Import the basic FROM centos
# Author information MAINTAINER yinxiaodoong<m15010969094@163.com>

# copy the file read.txt to the /usr/local/read.txt directory of the container COPY read.txt /usr/local/read.txt

# Add jdk tomcat ---> Automatically decompress ADD jdk-8u211-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.43.tar.gz /usr/local/

# Install vim command RUN yum install -y vim


#Configure MYPATH
ENV MYPATH /usr/local/
#Specify the default path to enter the container /user/local/ 
WORKDIR $MYPATH


# Configure the jdk environment variable ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV CLASSPATH $JAVA_HOME/lib/dt.jat:$JAVA_HOME/lib/tools.jar

# Configure the tomcat environment variable 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


# Expose port 8080 EXPOSE 8080


# /usr/local/tomcat/webapps# 
# Start tomcat when starting the container
CMD /usr/local/apache-tomcat-8.5.43/bin/startup.sh && tail -f /usr/local/apache-tomcat-8.5.43/logs/catalina.out 

# Build tomcat image [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

Publish your own image

Publish to DockerHub

Address: https://hub.docker.com/ Register your own account

Make sure your account can log in

Submit on our server

[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

After logging in, you can submit the image.

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

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:
  • Dockerfile file writing and image building command analysis
  • Implementation of building custom images with Dockerfile
  • Build a Docker image using Dockerfile
  • Steps to build a Docker image using Dockerfile
  • How to use Dockerfile to build images in Docker
  • Example of using Dockerfile to build an nginx image
  • How to use Dockerfile to build images
  • Sample code for building a docker image using the dockerfile instruction

<<:  Solution to running out of MySQL's auto-increment ID (primary key)

>>:  Native js implements shopping cart logic and functions

Recommend

Four modes of Oracle opening and closing

>1 Start the database In the cmd command windo...

Linux debugging tools that developers and operators must look at [Recommended]

System performance expert Brendan D. Gregg update...

Several ways of running in the background of Linux (summary)

1. nohup Run the program in a way that ignores th...

Border-radius IE8 compatible processing method

According to canisue (http://caniuse.com/#search=...

How to load third-party component libraries on demand in Vue3

Preface Take Element Plus as an example to config...

MySQL uses covering index to avoid table return and optimize query

Preface Before talking about covering index, we m...

Will this SQL writing method really cause the index to fail?

Preface There are often some articles on the Inte...

In IIS 7.5, HTML supports the include function like SHTML (add module mapping)

When I first started, I found a lot of errors. In...

React Hooks Detailed Explanation

Table of contents What are hooks? Class Component...

Understanding of haslaylout and bfc parsing

1. haslayout and bfc are IE-specific and standard ...

Two simple ways to remove text watermarks from web pages

<br /> When we browse certain websites and s...

WeChat applet custom tabBar step record

Table of contents 1. Introduction 2. Customize ta...

A brief discussion on JavaScript scope

Table of contents 1. Scope 1. Global scope 2. Loc...

Explanation of the precautions for Mysql master-slave replication

1. Error error connecting to master 'x@xxxx:x...