Docker Learning Note II, based on Dockerfile to build Java Tomcat Runtime environment

Source: Internet
Author: User
Tags docker run


In the first text, we completely manual, a command of a command input, implementation of a Java Tomcat runtime environment, although the initial results, but very tiring. If you rely on scripting to build a Tomcat container instance, a command can be done, why not. Fortunately, Docker provides dockerfile as a script to build Docker images, avoiding the input of people on one line. Dockerfile script can be done at any time to maintain the changes, that can be shared, more conducive to templating, let alone transmission, the benefits that is a big basket!

Ultimate goal: Create a Docker image that supports SSH terminal login and Tomcat7 autorun.

Write a Dockerfile

All environments, with the first text. After the installation of Vim under Ubuntu (spit slot, the Ubuntu system built-in VI command is very difficult to use, have to resort to vim):

sudo vim Dockerfile

Edit a Dockerfile file, here is the specific file content:

  # version 0.0.1# default Ubuntu server long-term support, currently 12.04FROM ubuntu# signed maintainer Imeixi "[email protected]" 

# Update source, install SSH serverrun echo "Deb Http:// precise main Universe" >/etc/apt/ Sources.listrun apt-get updaterun apt-get install-y openssh-serverrun mkdir-p/var/run/sshd
# Set the root ssh telnet password to 12345 6RUN echo "root:123456" | CHPASSWD

# Add Orache java7 source, one-time installation vim,wget,curl,java7,tomcat7 and other prerequisite software run Apt-get install Python-software-propertiesrun add-apt-repository ppa:webupd8team/javarun apt-get updateRUN apt-get install-y vim wget Curl Oracle-java7-installer TOMCAT7
# set JAVA_HOME environment variable run update-alternatives--display javarun echo "java_home=/usr /lib/jvm/java-7-oracle ">>/etc/environmentrun echo" java_home=/usr/lib/jvm/java-7-oracle ">>/etc/ DEFAULT/TOMCAT7
# container requires open SSH 22 port expose
# container requires open tomcat 8080 port expose 8080
# setting TOMCAT7 initialization Run, SSH Terminal Server running as background entrypoint service TOMCAT7 start &&/usr/sbin/sshd-d

Need to note:

    1. EntryPoint, which indicates that the image needs to be executed at initialization, and cannot be overwritten by overrides, it should be remembered
    2. CMD, which indicates that the mirror runs the default parameters and can be overridden by overrides
    3. Entrypoint/cmd can only exist in the file once, and the last one is valid for multiple existence, only the last one takes effect, other invalid!
    4. You need to initialize to run multiple commands, you can use && between each other, but the last one needs to run the infinite command, remember!

Entrypoint/cmd, generally the two can be used together, for example:

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

In Docker daemon mode, whether you are using entrypoint, or CMD, the final command, you must be able to prevent the container from exiting if the current process needs to run continuously.

The following invalid methods:

 ENTRYPOINT service tomcat7 start #运行几秒钟之后,容器就会退出 CMD service tomcat7 start #运行几秒钟之后,容器就会退出

This works:

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

This also works:

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

For details, please refer to the official documentation: Dockerfiles for Images

Build image

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

docker build -t yongboy/java7 .

- T: Create a label for the built image for easy memory/indexing, etc.

. : Specifies that the Dockerfile file is in the current directory

The internet is not very good and will wait a long time. Many operations may require scientific access to the Internet, forcing me to always hang a VPN, can be unimpeded.

After the build image is complete, look at the effect:

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

In the Run command, you also need to explicitly specify-p 22-p 8,080:8,080, otherwise it will not be actively mapped to the host on the Docker 0.8.1 release. It is reported that in the Docker 0.4.8 version, there is no concern about this issue. Or, you need to have a good way, you may wish to inform me, thank you.

In Dockerfile, if you do not use the ENTRYPOINT/CMD directive, you can do this if you run multiple commands:

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

Create a good image, can be saved to the index warehouse, easy to use next time (of course, we directly share dockerfile, is the simplest thing,:))), but after all, mirroring can be done out of the box.

    1. an account, such as Imeixi
    2. Build image

      docker build -t imeixi/java7 .

      You can omit this step if you have already built OK on it.

    3. Landing

      docker login
    4. Submit to Docker index Warehouse

      docker push imeixi/java7

      Now can get up to drink a cup of hot water, go out to stroll, also not necessarily can upload finished, that call a slow ah!

      Upload OK words, you can get similar address:

    5. How to use Mirroring

      docker pull imeixi/java7

      The rest of the steps, it's easy.


Advanced reading about Dockerfile:


Docker Learning Note II, based on Dockerfile to build Java Tomcat Runtime environment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.