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
will be default=1
modified to default=0
.
Finally, restart the reboot
operating system by 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 of our installers are placed in the directory of the host /root/software/
, we now need to mount them in 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 might as well choose /opt/
the directory as the installation directory, first through cd /opt/
the 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 source
the 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 exit
the 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 that are running, because the container exit
that you just exited with the command is in a stopped state, and you can view all the containers by using this 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'll create a mirror that runs 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
: Indicates that the script is executed in "daemon mode" /root/run.sh
, 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 docker ps
view the currently running containers by 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, then use docker build
the command to do all of the above manual operation.
http://blog.csdn.net/enweitech/article/details/51544084
Using Docker to build a Java WEB runtime environment