Docker Use dockerfile to build tomcat services

  • 2020-06-03 08:48:51
  • OfStack

In the previous example, we downloaded the image, started the container, and typed commands into the container to run the program, all of which were manually typed, unrepeatable, and inefficient. So you need a file or a script, you write the action that you want to do as a command, and then you let docker read it and analyze it and execute it, and it becomes very convenient to repeat the build and update, so Dockerfile was born.

Common parameters:

FROM command. Usage, FROM < image > : < tag > . The FROM command tells docker which (release) image we are building based on

RUN command. Usage RUN < command > . RUN is followed by the command to be executed. For example, if we want to install vim in the image, we can simply write RUN yum ES30en-ES31en vim in Dockfile

ENV command. Usage, ENV < key > < value > . The ENV command is used primarily to set environment variables for the container runtime

ADD command. Usage, ADD < src > < dest > . ADD is mainly used to add files from the host machine to the image

Start by building a directory to build our environment.


mkdir test/web

Upload tomcat and jdk to this directory.


[root@wls12c web]$ ls
apache-tomcat-7.0.70.tar.gz jdk-7u80-linux-x64.tar.gz 

Edit Dockerfile


vim Dockerfile

#pull down centos image
FROM centos
MAINTAINER test@test.com
#copy jdk and tomcat into image
ADD ./apache-tomcat-7.0.70.tar.gz /root
ADD ./jdk-7u80-linux-x64.tar.gz /root
#set environment variable
ENV JAVA_HOME /root/jdk1.7.0_80
ENV PATH $JAVA_HOME/bin:$PATH
#define entry point which will be run first when the container starts up
ENTRYPOINT /root/apache-tomcat-7.0.70/bin/startup.sh && tail -F /root/apache-tomcat-7.0.70/logs/catalina.out

Build the mirror


[root@wls12c web]$ docker build -t keven/centos:tomcat-centos --rm=true .
Sending build context to Docker daemon 470.4 MB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> d83a55af4e75
Step 1 : MAINTAINER test@test.com
 ---> Running in 955747d64da5
 ---> 1619dc8f6d58
................
70/logs/catalina.out
 ---> Running in fe48acf12d70
 ---> 52076383f11b
Removing intermediate container fe48acf12d70
Successfully built 52076383f11b

-ES73en selects the user name, warehouse name, and tag to specify which image will be generated

--rm=true specifies that temporary containers generated in the middle are removed during mirror-building.

View the newly generated image


[root@wls12c web]$ docker images keven/centos
REPOSITORY     TAG         IMAGE ID      CREATED       VIRTUAL SIZE
keven/centos    tomcat-centos    52076383f11b    19 minutes ago   516.6 MB

Run the mirror


[root@wls12c web]$ docker run -d -p 8090:8080 5207
8260fa22aa32126f613a6b64f121e78545ceae01da58c61396968bfafeda3706

-ES87en specifies that host port 80 is bound to container port 8080

-ES90en specifies that the container is run separately from the current tty and run in the background

The 5207 is the mirror image of the ID top 4.

Through http:// host IP:8090, you can see the familiar tomcat homepage.

There is also a startup method that automatically maps host ports


[root@wls12c web]$ docker run -d -p 8080 --name myweb 520
de39869a8c560e5e0cf48fc6022c05ed9f9a145bdafb897767fa468dc24ebfff
[root@wls12c web]$ docker port de3
8080/tcp -> 0.0.0.0:32768
[root@wls12c web]$ 

This will be accessed via http:// host IP:32768.


Related articles: