Docker Learning Note II, based on Dockerfile to build Java Tomcat Runtime environmentObjective
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 version, is currently 12.04FROM ubuntu# signed maintainer yongboy "[email protected]" # Update source, install SSH Serverru N echo "Deb Http://archive.ubuntu.com/ubuntu 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 123456RUN 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 TOMC at7# setting 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 22 # container needs to open tomcat 8080 port expose 8080# set TOMCAT7 initialize run, SSH Terminal Server Run as background entrypoint service TOMCAT7 start &&/usr/sbin/ Sshd-d
Need to note:
- EntryPoint, which indicates that the image needs to be executed at initialization, and cannot be overwritten by overrides, it should be remembered
- CMD, which indicates that the mirror runs the default parameters and can be overridden by overrides
- 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!
- 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 #运行几秒钟之后, the container exits the CMD service tomcat7 start #运行几秒钟之后 and the container exits
This works:
EntryPoint service tomcat7 start && tail-f/var/lib/tomcat7/logs/catalina.out# or cmd service tomcat7 start && Amp 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.
- https://index.docker.io/register an account, such as Yongboy
Build image
Docker build-t Yongboy/java7.
You can omit this step if you have already built OK on it.
Landing
Docker Login
Submit to Docker index Warehouse
Docker Push Yongboy/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: https://index.docker.io/u/yongboy/java7/
How to use Mirroring
Docker Pull Yongboy/java7
The rest of the steps, it's easy.
Summary
Advanced reading about Dockerfile:
- http://www.docker.io/learn/dockerfile/level2/
- http://www.docker.io/learn/dockerfile/level2/
Please read the official documentation for more details on the above dragonfly water. If you are free, in the next chapter, discuss some of the Docker mechanisms. Well, recently unemployed, if you have a heart, you can help recommend, thank you.
Docker Learning Note II, based on Dockerfile to build Java Tomcat Runtime environment