Home > Java > javaTutorial > How to use Docker to build a Java environment

How to use Docker to build a Java environment

王林
Release: 2023-05-11 13:22:06
forward
3221 people have browsed it

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:

How to use Docker to build a Java environment vsHow to use Docker to build a Java environment

##Preparation

First you need to prepare a centos operating system, a virtual machine can also be used. The specific configuration requirements are as follows:


1. Must be a 64-bit operating system

2. It is recommended that the kernel be above 3.8

Install docker

You only need to use the following command to install docker software


yum -y install docker-io
Copy after login

You can use the following command to check whether docker is installed successfully:

docker version
Copy after login

If 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 start
Copy after login

If the service command cannot be started, use the following


systemctl start docker.service
Copy after login

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 image

docker search <image>
Copy after login

Use docker pull imagename (mirror name ) to download the mirror

After the download is complete, use the following command to view all local mirrors:

docker images
Copy after login

When the download is complete, you should see:


repository        tag         image id      created       virtual size
docker.io/centos   centos7       f753707788c5    12 days ago     127.1 mb
Copy after login

If 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 docker

docker attach dabfb413d8cf[容器id]
Copy after login

Use the following command to start the container:


docker run -i -t -v /root/software/:/mnt/software/ --privileged=true 2a392a47afc5
Copy after login

docker run

The relevant parameters include:

-i: Indicates running the container in interactive mode

-t: Indicates that the container will enter its command line after starting

-v: Indicates which local directory needs to be mounted into the container, the format -v:

Assume that all our installation programs It is placed in the /root/software/ directory of the host, and now it needs to be mounted to the /mnt/software/ directory of the container.

After all this is done, you can install software for this container.

docker file transfer command

docker cp more.log e7de404c00bd:/tmp/ 

1f8787b81bcd
Copy after login

Install java development environment

We need to install jdk1.7, tomcat7, nginx, install the package Just download it from the official website


1. Install jdk

First, unzip the jdk package:

tar -zxf jdk-7u71-linux-x64.tar.gz –c
Copy after login

If it is an rpm package

rpm –ivh jdk-7u71-linux-x64.tar.gz
Copy after login

then restart Name the folder

mv jdk1.7.0_71/ jdk/
Copy after login

Finally configure the environment variables

vi ~/.bashrc
Copy after login

Add the following configuration at the end of the file:

export java_home=/opt/jdk

export path=$path:$java_home
Copy after login

·Open /etc/profile with a text editor


·Add at the end of the profile file:


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.jar
Copy after login

Finally use the source command to make the environment variables take effect:

source ~/.bashrc

source /etc/profile
Copy after login

1. The same goes for installing tomcat

, decompress the tomcat package:

tar –zxf apache-tomcat-7.0.55.tar.gz
Copy after login

Then rename the tomcat directory:

mv apache-tomcat-7.0.55/ tomcat/
Copy after login

Write a running script. When starting the container, run the script to start tomcat. The specific process is as follows:

vi /root/run.sh
Copy after login

Then, edit the script content as follows:

#!/bin/bash

source ~/.bashrc

sh /opt/tomcat/bin/catalina.sh run
Copy after login

注意:这里必须先加载环境,然后使用tomcat的运行脚本来运行tomcat

最后,为运行脚本添加执行权限:

chmod u+x /root/run.sh
Copy after login

1.安装nginx

先去官网下载源码包注意要是gz的

下载地址

下载完后,解压安装包:

tar -zxvf nginx-1.11.5.tar.gz
Copy after login

然后再配置安装变量,打开解压后的目录-执行命令

cd nginx-1.11.5
Copy after login

配置安装环境

./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
Copy after login

在解压目录执行:

make
Copy after login

切换到root用户执行安装命令

make install
Copy after login

创建软链

ln –s /usr/local/servers/nginx/sbin/nginx /usr/local/bin/nginx
Copy after login

启动nginx服务

nginx
Copy after login

再用 ps -ef|grep nginx查看是否启动成功

提交docker镜像

首先退出你刚才配置好的docker镜像

exit
Copy after login

然后使用以下命令可以看到刚才退出的docker镜像

docker ps –a
Copy after login

再使用以下命令,根据某个”容器id”来创建一个新的”镜像”:

docker commit 57c312bbaad1 javaweb:0.1
Copy after login

该容器id是”57c312bbaad1”,所创建的镜像名是”javaweb”

注意:”57c312bbaad1” 这个id是使用 docker ps 命令来查看的

提交了新的镜像你可以把这个镜像储存tar包

docker  –o ~/javaweb.tar javaweb
Copy after login

docker save –o 保存的目录 镜像名

启动容器

先用 docker images看看当前所有的镜像

启动最新创建的镜像

docker run -d -p 80:80 --name javaweb javaweb:0.1 /root/run.sh
Copy after login

-d:表示以”守护模式”执行/root/run.sh脚本,此时tomcat控制台不会出现在输出终端上。

-p:表示宿主机与容器的端口映射,此时将容器内部的80端口映射为宿主机的 80端口,这样就向外界暴露了80端口,可通过docker网桥来访问容器内部的80端口了

--name:表示容器名称,用一个有意义的名称命名即可

The above is the detailed content of How to use Docker to build a Java environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template