Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker to build the running environment of Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use Docker to build the running environment of Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Docker to build the running environment of Java" article.

What does docker do?

Docker is a high-level container engine based on linux Container (lxc-linux container) and developed in go language.

The source code is hosted on github and is open source in accordance with the apache2.0 protocol. Docker's goal is to implement a lightweight operating system virtualization solution.

The first thing to learn about docker is to understand several concepts:

Mirror-the docker image is similar to the common system iso image and contains information about the application

Container-Container is equivalent to a virtual machine that can be run, the application runs in the container, and the docker runs on "docker"

Warehouse-Warehouse is the place where images are stored, with version control similar to git, which is also divided into two forms: public warehouse (public) and private warehouse (private).

Docker supports most linux distributions, and by using docker containers, it can run on different operating systems

Running your own applications on different machines, regardless of the configuration of hardware, running environment, and so on, the migration of applications becomes very simple.

Comparison between docker and traditional virtualization technologies

Compared with the traditional virtual machine technology, docker takes up less resources and starts faster, which greatly facilitates the deployment and operation of the project.

Docker implements virtualization at the operating system level and reuses the operating system of the local host. The traditional way is to virtualize multiple operating systems on the basis of hardware, and then deploy related applications on the system.

The following picture, referring to the relevant blog post, vividly illustrates the difference between traditional virtualization technologies such as docker and vm:

Vs

Building a docker environment

I am using ubuntu 14.04, which installs the docker service.

Quickly install docker

The installation of docker is already supported in the 14.04 ubuntu repository.

You can use a quick installation

Sudo apt-get updatesudo apt-get install docker.io

Start services and daemons

Service docker.io statusservice docker.io start

Installation in this way is usually not the latest version of docker

If you want to install the latest version, you can go to the docker website to download and install it.

Create the first docker image

The general process of building a docker image is to first create a container, modify the image in the container, configure the relevant environment, etc., and finally submit the modification to a new image.

(1) download the image file

Download the system used to make the image

Sudo docker pull index.alauda.cn/alauda/ubuntu

Here I pull from the mirror center of the Finch Cloud.

Or you can pull it directly from the image center of docker, but it seems to be very slow:

Sudo docker pull ubuntu

After the download is successful, use the images command to view the list of local images:

Docker images

It should be noted here that when using docker, please add sudo.

After installing docker by default, you need to run the sudo command every time you execute docker. If you do not follow sudo, executing the docker command directly will report some permission errors.

(2) start the container and modify the image

After the image is downloaded locally, it can be run using docker

Start the container with the following command parameters

Docker run

-I: means to run the container in "interactive mode"

-t: indicates that the container will enter its command line after startup

-v: indicates which local directory needs to be mounted to the container

Format:-v:

All my related programs are in the / data/software/ directory of the current machine, and I want to mount it to the same directory of the container:

Sudo docker run-I-t-v / data/software/:/data/software/ ae983d5e88ce / bin/bash

"Mirror id", or "Repository name: tag signature", for example: index.alauda.cn/alauda/ubuntu: latest.

In the above command, you can run a shell using the specified image. If you want to exit the terminal, you can use the exit command, or press ctrl-p+ctrl-Q to switch to the host machine. However, in this way, the container still runs the day after tomorrow.

After starting the terminal, enter the / data/software/ directory and find that the files in the current machine directory have been synchronized:

(3) install jdk, tomcat, etc.

Install related jdk and other programs, all of which are installed in the / data/ directory:

Tar-zxvf jdk-7u25-linux-x64.tar.gz-c / data/mv jdk1.7.0_25 jdkunzip apache-tomcat-7.0.54.zip-d / data/mv apache-tomcat-7.0.54 tomcat

Configure environment variables

Vi / etc/profile

Add the following configuration:

# set java environment export java_home=/data/jdk export jre_home=$ {java_home} / jre export classpath=.:javahome/lib:javahome/lib: {jre_home} / lib export path=javahome/bin:javahome/bin:path export catalina_home=/data/tomcat export catalina_base=/data/tomcat

Save and exit, and the settings take effect immediately:

Source / etc/profile

(4) write startup script

When starting tomcat, it must be implemented through tomcathome/bin/catalina.sh, not tomcathome/bin/catalina.sh, nor tomcat_home/bin/startup.sh, otherwise the container will exit immediately after the script is executed.

Vi / data/start.sh

Add the following:

#! / bin/bash# export environment variablesource / etc/profile# start tomcatbash / data/tomcat/bin/catalina.sh run

Add executable permissions: chmod Ubunx / data/start.sh

(5) build an image

There are two ways to build an image using docker:

Use the docker commit command to be more intuitive

Using the docker build command and the dockerfile file, you can template the mirror build process

Here we use docker commit to create an image.

View the list of containers:

Sudo docker ps-acontainer id image command created status ports names39b2cf60a4c1 ae983d5e88ce:latest "/ bin/bash" 5 hours ago exited (0) 9 seconds ago dreamy_euclid

Submit a new image:

Sudo docker commit 39b2cf60a4c1 bingyue/docdemo

If you have a docker account, you can push the image to the private registry of docker hub or funds.

Now look at the local docker image

Sudo docker images

You can see that the local repository already has the docker image you just created.

Repository tag image id created virtual sizebingyue/docdemo latest bfc7ed316d42 about a minute ago 528.2 mbindex.alauda.cn/alauda/ubuntu latest ae983d5e88ce 10 months ago 255.1 mb

Docker inspect can view the details of the newly created image:

Sudo docker inspect bingyue/docdemo

(6) run the newly created image

Docker run-d-p 18080R 8080-- name docdemo bingyue/docdemo / data/start.sh

-p: indicates the port mapping between the host and the container. The port 8080 inside the container is mapped to the port 18080 of the host.

This exposes port 18080 to the outside world, and port 8080 inside the container can be accessed through the docker bridge.

Check whether the background starts successfully:

Docker ps

Test access:

(7) submit to docker warehouse

If you have an account in the docker repository, you can submit the locally created images to the repository.

> user experience

At this stage, almost completed the initial experience of docker, docker application is still relatively simple, the real complexity should be the virtualization technology behind.

Step by step deployment, it is true that docker is much simpler than the traditional virtual machine technology, there is an opportunity to continue in-depth learning.

Attachment: add docker user group to avoid sudo input

After docker is installed by default, every time you execute docker, you need to run the sudo command, which affects efficiency. If you do not follow sudo, executing the docker images command directly will have the following problems:

Get http:///var/run/docker.sock/v1.18/images/json: dial unix / var/run/docker.sock: permission denied. Are you trying to connect to a tls-enabled daemon without tls?

This problem can be solved by adding the current user execution rights to the corresponding docker user group.

Add a new docker user group

Sudo groupadd docker

# add current user to docker user group

Sudo gpasswd-a bingyue docker

# restart the docker background monitoring process

Sudo service docker restart

# after reboot, try to see if it works

Docker version

# if the system restarts if it does not take effect, it will take effect

Sudo reboot

Docker common commands

# download a ubuntu image

Sudo docker pull ubuntu

# run an interactive shell using ubuntu

Sudo docker run-I-t ubuntu / bin/bash

# docker ps command

Sudo docker ps # list all currently running containersudo docker ps-l # list the most recently started and running containersudo docker ps-a # list all container

# port command

Docker run-p 80Plus 8080 # Mapping container port 8080 to host port 80

# delete container command

Sudo docker rm `sudo docker ps-a-q` # Delete all containers sudo docker rm $container_id# delete containers whose id is container_id

# Quick reference for other commands:

Sudo docker images # View the local image sudo docker attach $container_id # start an existing docker instance sudo docker stop $container_id # stop the docker instance sudo docker logs $container_id # View the running log of the docker instance, make sure that sudo docker inspect $container_id # is running properly to view the instance properties of container, such as ip, and so on, these are the contents of this article on "how to use Docker to build a running environment for Java". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to all of you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report