Home >Java >javaTutorial >How to build a Java environment with Docker
What does docker do?
Docker is an advanced container engine based on linux container (lxc-linux container), developed based on go language, and the source code is hosted on github , complies with the apache2.0 protocol and is open source. The goal of docker is to implement a lightweight operating system virtualization solution.
To learn docker, you must first understand a few concepts:
Image - Docker’s image is similar to the common system ISO image and contains application information;
Container - The container is equivalent to a virtual machine that can be run. The application runs in the container, and docker runs on "docker";
Warehouse - The warehouse is a place where images are stored, similar to git Version control is also divided into two forms: public warehouse (public) and private warehouse (private);
docker supports most linux distributions. By using docker containers, you can operate on different operating systems and different You can run your own applications on your own machine without having to worry about hardware, operating environment and other configurations. Application migration becomes very simple.
Comparison between docker and traditional virtualization technology
Compared with traditional virtual machine technology, docker takes up less resources and starts faster, which is very convenient. Project deployment and operation.
Docker implements virtualization at the operating system level and reuses the operating system of the local host. The traditional method is to virtualize multiple operating systems based on hardware and then deploy related Applications.
This picture vividly illustrates the difference between traditional virtualization technologies such as docker and vm:
vs
##Preparation
First you need to prepare a centos operating system, a virtual machine can also be used. The specific configuration requirements are as follows:2. It is recommended that the kernel be above 3.8
Install docker
You only need to use the following command to install docker softwareyum -y install docker-ioYou can use the following command to check whether docker is installed successfully:
docker versionIf the output shows the version number of docker, it means After the installation is successful, you can start the docker service through the following command:
service docker startIf the service command cannot be started, use the following
systemctl start docker.service
How to do it
Just like installing software, we first need to have a CD with the software burned on it. If you are using a virtual optical drive, you need to prepare the image file and use it to install the software. In the world of docker, there are also image files. The operating system we need has been installed. We generally call it docker image Download imagedocker search <image>Use docker pull imagename (mirror name ) to download the mirrorAfter the download is complete, use the following command to view all local mirrors:
docker imagesWhen the download is complete, you should see:
repository tag image id created virtual size docker.io/centos centos7 f753707788c5 12 days ago 127.1 mbIf you see the above output, it means that you can use the "docker.cn/docker/centos" image, or call it a repository. The image has a tag named "centos7", in addition to There is an image id named "25c5298b1a36" (this is randomly generated, so everyone sees it differently)
Start the container
The container is in the image Once the container is started, we can log in to the container and install the software or applications we need. Use to enter the already running dockerdocker attach dabfb413d8cf[容器id]Use the following command to start the container:
docker run -i -t -v /root/software/:/mnt/software/ --privileged=true 2a392a47afc5docker run
docker cp more.log e7de404c00bd:/tmp/ 1f8787b81bcd
Install java development environment
We need to install jdk1.7, tomcat7, nginx, install the package Just download it from the official websitetar -zxf jdk-7u71-linux-x64.tar.gz –cIf it is an rpm package
rpm –ivh jdk-7u71-linux-x64.tar.gzthen restart Name the folder
mv jdk1.7.0_71/ jdk/Finally configure the environment variables
vi ~/.bashrcAdd the following configuration at the end of the file:
export java_home=/opt/jdk export path=$path:$java_home·Open /etc/profile with a text editor
export java_home=/usr/share/jdk1.6.0_14 export path=$java_home/bin:$path export classpath=.:$java_home/lib/dt.jar:$java_home/lib/tools.jarFinally use the source command to make the environment variables take effect:
source ~/.bashrc source /etc/profile1. The same goes for installing tomcat , decompress the tomcat package:
tar –zxf apache-tomcat-7.0.55.tar.gzThen rename the tomcat directory:
mv apache-tomcat-7.0.55/ tomcat/Write a running script. When starting the container, run the script to start tomcat. The specific process is as follows:
vi /root/run.shThen, edit the script content as follows:
#!/bin/bash source ~/.bashrc sh /opt/tomcat/bin/catalina.sh run
注意:这里必须先加载环境,然后使用tomcat的运行脚本来运行tomcat
最后,为运行脚本添加执行权限:
chmod u+x /root/run.sh
1.安装nginx
先去官网下载源码包注意要是gz的
下载地址
下载完后,解压安装包:
tar -zxvf nginx-1.11.5.tar.gz
然后再配置安装变量,打开解压后的目录-执行命令
cd nginx-1.11.5
配置安装环境
./configure --prefix=/usr/local/servers/nginx “/usr/local/servers/nginx”是安装路径
有可能会出现./configure: error: c compiler cc is not found
这时需要运行命令更新一下gcc
yum install gcc gcc-c++ ncurses-devel perl yum -y install pcre-devel yum -y install zlib-devel yum -y install autoconf libtool make
在解压目录执行:
make
切换到root用户执行安装命令
make install
创建软链
ln –s /usr/local/servers/nginx/sbin/nginx /usr/local/bin/nginx
启动nginx服务
nginx
再用 ps -ef|grep nginx查看是否启动成功
提交docker镜像
首先退出你刚才配置好的docker镜像
exit
然后使用以下命令可以看到刚才退出的docker镜像
docker ps –a
再使用以下命令,根据某个”容器id”来创建一个新的”镜像”:
docker commit 57c312bbaad1 javaweb:0.1
该容器id是”57c312bbaad1”,所创建的镜像名是”javaweb”
注意:”57c312bbaad1” 这个id是使用 docker ps 命令来查看的
提交了新的镜像你可以把这个镜像储存tar包
docker –o ~/javaweb.tar javaweb
docker save –o 保存的目录 镜像名
启动容器
先用 docker images看看当前所有的镜像
启动最新创建的镜像
docker run -d -p 80:80 --name javaweb javaweb:0.1 /root/run.sh
-d:表示以”守护模式”执行/root/run.sh脚本,此时tomcat控制台不会出现在输出终端上。
-p:表示宿主机与容器的端口映射,此时将容器内部的80端口映射为宿主机的 80端口,这样就向外界暴露了80端口,可通过docker网桥来访问容器内部的80端口了
--name:表示容器名称,用一个有意义的名称命名即可
The above is the detailed content of How to build a Java environment with Docker. For more information, please follow other related articles on the PHP Chinese website!