Docker Learning method to build JAVA Tomcat operating environment based on Dockerfile

  • 2020-06-03 08:57:34
  • OfStack

preface

In the first text, we implemented an java tomcat operating environment in a completely manual way, with one command and one command input. Although initial results were achieved, it was very tiring. If you rely on a script to build an Tomcat container instance and a command to do it, why not? The good news is that Docker provides Dockerfile as a script to build Docker images, avoiding line-by-line input. Dockerfile scripts can be maintained and modified at any time, so they can be Shared, which is more conducive to templating, not to mention transmission, which is a huge benefit!

Final goal: Create an Docker image that supports SSH terminal login and Tomcat7 automatic operation.

Write 1 Dockerfile

All environments, same as text 1. After installing vim under ubuntu (joke 1, ubuntu system built-in vi command 10 difficult to use, had to resort to vim) :


sudo vim Dockerfile

I have edited 1 Dockerfile file. The following is the specific file content:


# VERSION 0.0.1
#  The default ubuntu server Long term support version, currently is 12.04
FROM ubuntu
#  The signature! 
MAINTAINER yongboy "yongboy@gmail.com"

#  Update source, install ssh server
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y openssh-server
RUN mkdir -p /var/run/sshd

#  Set up the root ssh The remote login password is 123456
RUN echo "root:123456" | chpasswd 

#  add orache java7 The source, 1 Installation time sex vim . wget . curl . java7 . tomcat7 And other essential software 
RUN apt-get install python-software-properties
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get install -y vim wget curl oracle-java7-installer tomcat7

#  Set up the JAVA_HOME The environment variable 
RUN update-alternatives --display java
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/environment
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/default/tomcat7

#  The container needs to be open SSH 22 port 
EXPOSE 22

#  The container needs to be open Tomcat 8080 port 
EXPOSE 8080

#  Set up the Tomcat7 Initialize the run, SSH The terminal server runs in the background 
ENTRYPOINT service tomcat7 start && /usr/sbin/sshd -D

Note:

ENTRYPOINT, which means the command that the mirror needs to execute when it is initialized, cannot be overridden. Keep in mind CMD, which represents the default parameter of the mirror run, can be overridden ENTRYPOINT/CMD can only exist once in a file, and the last one is valid multiple existences, only the last one is valid, others are invalid! You need to initialize multiple commands that can be used with each other & & Separate, but the last command to run for infinity, remember!

ENTRYPOINT/CMD 1 Generally the two can be used together, for example:


ENTRYPOINT ["/usr/sbin/sshd"]
CMD ["-D"]

In Dockerdaemon mode, whether you are using ENTRYPOINT or CMD, the last command must be 1 if the current process needs 1 direct line to prevent container exit.

The following invalid method:


 ENTRYPOINT service tomcat7 start # After a few seconds of running, the container exits 
 CMD service tomcat7 start # After a few seconds of running, the container exits 

This works:


ENTRYPOINT service tomcat7 start && tail -f /var/lib/tomcat7/logs/catalina.out
#  or 
CMD service tomcat7 start && tail -f /var/lib/tomcat7/logs/catalina.out

It also works:


 ENTRYPOINT ["/usr/sbin/sshd"]
 CMD ["-D"]

Build the mirror

The script is written and needs to be converted to a mirror:


docker build -t yongboy/java7 .

-ES65en: One label for built images for easy memory/indexing, etc

. : Specify the Dockerfile file in the current directory

The Internet speed is not very good. It will take a long time. Many of these operations may require scientific Internet access, forcing me to just hang VPN to be unimpeded.

Once the build image is complete, take a look at how it works:


docker run -d -p 22 -p 8080:8080 yongboy/java7

In running the command, you also need to explicitly specify -ES77en 22-ES78en 8080:8080, otherwise it will not actively map to the host in version 0.8.1 of Docker. With the Docker 0.4.8 release, this is not known to be a concern. Or, you want to have a good way, might as well tell me, thank you.

In Dockerfile, if you are not using the ENTRYPOINT/CMD instruction, if you are running multiple commands, you can do this:


docker run -d -p 22 -p 8080 yongboy/java7 /bin/sh -c "service tomcat7 start && /usr/sbin/sshd -D"

Commit/save the image

A good image can be saved in the index repository for next use (of course, we share Dockerfile directly, is the easiest thing to do :)), but it can be done out of the box after all.

1, https: / / index. docker. io/registered an account, such as yongboy

2. Build mirrors


docker build -t yongboy/java7 .

If you have built OK above, you can omit this step.

3, landing


docker login

4. Submit to Docker index warehouse


# VERSION 0.0.1
#  The default ubuntu server Long term support version, currently is 12.04
FROM ubuntu
#  The signature! 
MAINTAINER yongboy "yongboy@gmail.com"

#  Update source, install ssh server
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y openssh-server
RUN mkdir -p /var/run/sshd

#  Set up the root ssh The remote login password is 123456
RUN echo "root:123456" | chpasswd 

#  add orache java7 The source, 1 Installation time sex vim . wget . curl . java7 . tomcat7 And other essential software 
RUN apt-get install python-software-properties
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get install -y vim wget curl oracle-java7-installer tomcat7

#  Set up the JAVA_HOME The environment variable 
RUN update-alternatives --display java
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/environment
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/default/tomcat7

#  The container needs to be open SSH 22 port 
EXPOSE 22

#  The container needs to be open Tomcat 8080 port 
EXPOSE 8080

#  Set up the Tomcat7 Initialize the run, SSH The terminal server runs in the background 
ENTRYPOINT service tomcat7 start && /usr/sbin/sshd -D
0

Now you can get up and drink a cup of hot water, go out for a walk, but it's not sure you can finish uploading. That's one slow!

Upload OK, you can get similar address: https: / / index docker. io/u/yongboy java7 /

5. How to use mirroring


# VERSION 0.0.1
#  The default ubuntu server Long term support version, currently is 12.04
FROM ubuntu
#  The signature! 
MAINTAINER yongboy "yongboy@gmail.com"

#  Update source, install ssh server
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y openssh-server
RUN mkdir -p /var/run/sshd

#  Set up the root ssh The remote login password is 123456
RUN echo "root:123456" | chpasswd 

#  add orache java7 The source, 1 Installation time sex vim . wget . curl . java7 . tomcat7 And other essential software 
RUN apt-get install python-software-properties
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get install -y vim wget curl oracle-java7-installer tomcat7

#  Set up the JAVA_HOME The environment variable 
RUN update-alternatives --display java
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/environment
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/default/tomcat7

#  The container needs to be open SSH 22 port 
EXPOSE 22

#  The container needs to be open Tomcat 8080 port 
EXPOSE 8080

#  Set up the Tomcat7 Initialize the run, SSH The terminal server runs in the background 
ENTRYPOINT service tomcat7 start && /usr/sbin/sshd -D
1

The rest of the steps, it's pretty straightforward.


Related articles: