Docker is one of the hottest technologies of the 2014, and almost all programmers have heard about it. Docker is a "lightweight" container technology that almost shakes the status of traditional virtualization technology, and more and more companies are now starting to use Docker to replace existing virtualization Platforms. As a Java programmer, it's time for us to learn Docker together!
This article makes a comparison between virtualization technology and Docker container technology, and then leads to some Docker terminology, such as containers, mirrors, and so on, then uses Docker to build a Java Web runtime environment, and finally a summary of this article.
Let's go back to the architecture of traditional virtualization Technologies:
As we can see, we have multiple virtual machines installed on the Host's operating system, and in each virtual machine, through virtualization technology, a virtual operating system is implemented, and then, on that virtual operating system, you can install the application you Need. It all seems very simple, but the technical details are quite inscrutable, and the big-god figures are not necessarily clear.
Anyone who has used a virtual machine should know that starting a virtual machine is like starting a computer, the initialization process is quite slow, we need to wait a long time to see the login interface. Once the virtual machine is started, you can establish a network connection with the host to ensure that the virtual machine is connected to the Host. Different virtual machines are isolated from each other, that is, they do not know each Other's existence, but each virtual machine occupies a host of hardware and network Resources.
Let's compare the architecture of Docker technology:
visible, on the Host's operating system, there is a Docker service running (or "docker engine"), on this service, we can open multiple Docker containers, each Docker container can run its own required applications, Docker containers are isolated from each other, similarly, the Host's hardware and network resources are Occupied.
Docker containers In contrast to virtual machines, in addition to the technical implementation of a completely different, boot speed than the virtual machine has an essential leap, start a container only in the blink of an Eye. Whether they are virtual machines or Docker containers, They are designed to isolate the Application's operating environment, Save Our hardware resources, and provide benefits for our Developers.
Let's look at the Docker Logo again:
It is clear that this is a whale, which is holding many Containers. We can treat the host as the whale, and the isolated containers as containers, each containing its own application. This Logo is simply too image!
It should be stressed that the author is not denying virtualization technology, but to let more readers understand how to use Docker technology, let everyone know that in addition to virtualization technology, there is another alternative technology, but also to isolate the Application.
below, we'll Combine the deployment process of a Java Web application to describe how to "cook" Docker's delicacy. Are you ready? Let's get Started now!
Pre-requisites for raw materials
first, you have to prepare a CentOS operating system, and the virtual machine is Ok. In summary, the CentOS operating system can be accessed via the Linux client Tool.
It should be explained that Ubuntu or other Linux operating systems can also play Docker, but this article chose to use CentOS as an example, that's All.
CentOS specific requirements are as Follows:
- Must be a 64-bit operating system
- Recommended kernel is above 3.8
View your CentOS kernel using the following command:
Uname-r
If the kernel version number of the output is less than 3.8 after executing the above command, please refer to the following method to upgrade your Linux Kernel.
For CentOS 6.5, the kernel version defaults to 2.6. first, You can install the latest kernel with the following command:
Rpm--import HTTPS://WWW.ELREPO.ORG/RPM-GPG-KEY-ELREPO.ORGRPM-IVH http://www.elrepo.org/ Elrepo-release-6-5.el6.elrepo.noarch.rpmyum-y--enablerepo=elrepo-kernel Install Kernel-lt
then, Edit the following configuration file:
Vi/etc/grub.conf
Modify the Default=1 to default=0.
finally, Restart the operating system with the reboot Command.
If there is no accident after restarting, check the kernel again and your CentOS kernel will be displayed as 3.10.
If you come here, you and we expect the result is Consistent. Congratulations to you! Now we're going to install Docker together.
Installing Docker
You can install Docker software with the following command:
RPM-UVH http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpmyum-y Install Docker-io
You can use the following command to see if Docker is installed successfully:
Docker version
If the version number of the Docker is output, the installation is successful and the Docker service can be started with the following command:
Service Docker start
Once the Docker service starts, we're ready to start using Docker.
Practice
Just like installing software, we first need to have a disc that burns the software, and if you are using a virtual optical drive, you need to run a file called "mirroring" to install the Software. In the Docker world, there is also a thing called "mirror", has installed our required operating system, we generally become "Docker mirror", this article referred to as "mirror".
So Here's the question, where do we download the image?
The Docker website does provide all the mirrors, but it's not accessible at Home. fortunately, the domestic goodwill people have provided a Docker Chinese web site where we can download the Docker images we need.
Download image
We might as well use CentOS as an example to download a CentOS image by following the steps below.
first, visit the Docker Chinese web, search the first page of the image named "centos", in the results of the search, there is an "official image", it is what we need.
then, go to the official CentOS image page, in the "pull this repository" input box, there is a command, copy it, run the command on its own command line, and then download the image immediately.
finally, use the following command to view all local mirrors:
Docker images
When the download is complete, you should see:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL sizedocker.cn/docker/centos centos6 25c5298b1a36 7 weeks ago 215.8 MB
If you see the above output, you can use the "docker.cn/docker/centos" image, or call it a warehouse (Repository), which has a label named "centos6", and a tag called " 25c5298b1a36 "image ID (perhaps the image ID you see is inconsistent with this one, which is normal because the number is randomly generated). In addition, we can see that the image is only 215.8 MB, very small, and not as large as the virtual Machine's image File.
Now that the image is there, we need to use the image below to start the Container.
Start container
Containers are run on the basis of mirroring, and once the container is started, we can log in to the container and install the software or application we Need. Now that the image has been downloaded locally, how can you start the container?
Just use the following command to start the Container:
Docker Run-i-t-v/root/software/:/mnt/software/25c5298b1a36/bin/bash
This command is longer, we break it down a little bit, actually contains the following three Parts:
Docker run < related parameters > < mirroring id> < initial commands >
Among them, the relevant parameters include:
- -i: indicates that the container is running in "interactive mode"
- -t: indicates that the container will enter its command line when it is started
- -v: indicates which directory needs to be mounted to the container, format:-v < host directory >:< container directory >
Assuming that all our installers are placed in the/root/software/directory of the host, it is now necessary to mount it to the Container's/mnt/software/directory.
It is necessary to note that you do not have to use "mirror ID" or "warehouse name: tag name", for example: docker.cn/docker/centos:centos6.
The initial command indicates the command to run once the container is started, using "/bin/bash" at this time to indicate nothing, just go to the command line.
Install related software
In order to build the Java Web environment, we need to install the JDK and Tomcat, the following procedures are carried out inside the Container. We may as well choose The/opt/directory as the installation directory, first through the Cd/opt/command to enter the Directory.
Installing the JDK
first, Unpack the JDK package:
Tar-zxf/mnt/software/jdk-7u67-linux-x64.tar.gz-c.
then, Rename the JDK directory:
MV jdk1.7.0_67/jdk/
Installing Tomcat
first, Unpack the Tomcat package:
Tar-zxf/mnt/software/apache-tomcat-7.0.55.tar.gz-c.
then, Rename the Tomcat directory:
MV apache-tomcat-7.0.55/tomcat/
Setting environment variables
first, Edit The. BASHRC file
VI ~/.BASHRC
then, Add the following configuration at the end of the file:
Export Java_home=/opt/jdkexport path= $PATH: $JAVA _home
finally, you need to use the source command to make the environment variable effective:
SOURCE ~/.BASHRC
Writing Run scripts
We need to write a run script that, when the container is started, runs the script and launches Tomcat, as Follows:
first, Create a run script:
vi/root/run.sh
then, edit the script content as Follows:
#!/bin/bashsource ~/.bashrcsh/opt/tomcat/bin/catalina.sh Run
Note: You must load the environment variable first, and then use Tomcat's Run script to start the Tomcat Service.
finally, Add execute permissions to run the Script:
chmod u+x/root/run.sh
Exit container
When all of the above steps are complete, you can use the Exit command to exit the Container.
You can then use the following command to view the running Container:
Docker PS
At this point, you should not see any programs running, because the container that you just exited with the Exit command is in a stopped state, and you can view all the containers using the following command:
Docker Ps-a
The output is as Follows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS names57c312bbaad1 docker.cn/docker/ CENTOS:CENTOS6 "/bin/bash" minutes ago Exited (0) seconds ago naughty_goldstine
Remember the above container ID (container id), and then we will create a mirror that can run the Java Web through the Container.
Create a Java Web image
Use the following command to create a new "mirror" based on a "container ID":
Docker commit 57c312bbaad1 huangyong/javaweb:0.1
The Container's ID is "57c312bbaad1", the image name created is "huangyong/javaweb:0.1", and the Java Web container can then be started using Mirroring.
Start the Java Web container
It is necessary to first use the Docker Images command to view all current mirrors:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL sizehuangyong/javaweb 0.1 fc826a4706af 38 Seconds ago 562.8 mbdocker.cn/docker/centos centos6 25c5298b1a36 7 weeks ago 215.8 MB
visible, you now see the newly created mirror "huangyong/javaweb:0.1" with the image ID "fc826a4706af". As described above, we can start the container with a "mirror name" or "mirror ID", Unlike the last time the container was started, we are now no longer entering the Container's command line, but instead directly start the Tomcat service inside the Container. At this point, you need to use the following command:
Docker run-d-p 58080:8080--name Javaweb huangyong/javaweb:0.1/root/run.sh
A little Explanation:
- -d: the/root/run.sh script is executed in "daemon mode", at which point the Tomcat console does not appear on the output terminal.
- -p: represents the port mapping of the host to the container, at which point the 8080 port inside the container is mapped to the 58080 port of the host, exposing 58080 ports to the outside, and can access the 8080 ports inside the container via the Docker bridge.
- --name: represents the container name and is named with a meaningful Name.
The content of the Docker bridge needs to be supplemented with instructions. actually, Docker builds a bridge between the host and the container, and we can use the host IP address and port number to map the IP address and port number inside the Container.
After a series of parameters is the "mirror name" or "mirror ID", how convenient to Come. The last is the "initial command", which is a running script written above that encapsulates the commands that load environment variables and start the Tomcat service.
When the above command is run, a long list of "container ids" is immediately output, and we can view the currently running containers through the Docker PS Command.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS names82f47923f926 huangyong/javaweb : 0.1 "/root/run.sh" 4 seconds ago up 3 seconds 0.0.0.0:58080->8080/tcp javaweb
Taste
In the browser, enter the following address to access the Tomcat home page:
http://192.168.65.132:58080/
Note: the IP address of the host is used here, with the externally exposed port number 58080, which maps the port number 8080 inside the Container.
Summarize
Through this article, we understand what Docker is? What is the difference between it and the virtual machine? And how do I install Docker? How do I download a Docker image? How do I run a Docker container? How do I install an application inside a container? How do I create a mirror on a container? How do I start a container as a service? All this seems simple, but the operation is quite cumbersome, but practice makes perfect, we need to keep practicing.
In addition to this way of manually generating Docker images, there is a way to create Docker images automatically, much like writing Code. Just need us to write a Dockerfile file, and then use the Docker Build command to do all of the above manual operations.
Using Docker to build a Java WEB runtime environment