Using Docker to build a Java Web Runtime environment

Source: Internet
Author: User
Tags tag name docker ps docker hub docker run

This weekend experience a bit of a fire Docker technology, simple to record their own study notes.What is >docker for?

Docker is an advanced container engine based on the Linux container (Lxc-linux container), developed based on the Go language,
The source code is hosted on Github and complies with the APACHE2.0 protocol open source. The goal of Docker is to implement a lightweight operating system virtualization solution.

Learn Docker first to understand several concepts:

Mirroring the-docker image is similar to a common system ISO image, and contains information about the application;

Container-the container is the equivalent of a virtual machine that can run, the application runs in a container, and Docker runs on "Docker";

Warehouse-Warehouse is the place to store the image, there is a git-like version control, also divided into public warehouse (common) and private warehouse (privately) two forms;

Docker supports most Linux distributions by using Docker containers, which can be used on different operating systems,

Running their own applications on different machines without caring about the hardware, the running environment and the like, the migration of the application becomes very simple.

comparison of >docker and traditional virtualization technologies

Compared with traditional virtual machine technology, Docker resource occupies less, starts faster, and greatly facilitates the deployment and operation of the project.
Docker is virtualization on the operating system level, reusing the local host's operating system, the traditional way is based on the hardware, virtual out of multiple operating systems, and then deploy the relevant applications on the system.

The image below illustrates the difference between traditional virtualization technologies like Docker and VMS:

vs

> Build Docker Environment

I'm using Ubuntu 14.04, which installs the Docker service.

>>> Quick Install Docker

The 14.04 version of the Ubuntu repository already supports Docker installation,
You can use the Quick Install method,

sudo apt-get update
sudo apt-get install Docker.io

Start the service and daemon

Service Docker.io Status
Service Docker.io Start

This method of installation 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 the installation.

> Create a first docker image

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

>>> Download image files

Download the system used to make the image,

sudo docker pull Index.alauda.cn/alauda/ubuntu

Here I pull from the image center of the Spirit Bird cloud.

Or you can pull directly from the image center of Docker, but it looks very slow:

sudo docker pull Ubuntu

After the download succeeds, use the images command to view the local mirror list:

Docker images

It is important to note that when using Docker, add sudo.

After Docker is installed by default, each time you execute Docker you need to run the sudo command, and if you do not follow sudo, the Docker command will report some permissions errors.

>>> start the container and modify the mirror

Once the image is downloaded locally, you can use Docker to run it,
Start the container with the following command arguments,

Docker run < related parameters > < mirroring id> < initial commands >

-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 you want to mount locally to the container.
Format:-v < host directory >:< container directory >

My related programs are in the/data/software/directory of the current machine and want to mount it in the same directory as the container:

sudo docker run-i-t-v/data/software/:/data/software/ae983d5e88ce/bin/bash

"Mirror ID", you can also use "warehouse Name: Tag name", for example:index.alauda.cn/alauda/ubuntu:latest.

The above command, you can use the specified image to run a shell, if you want to exit the terminal, you can use the Exit command,
Or, you can switch to the host machine by pressing CTRL-P+CTRL-Q in turn. But in this way, the container is still running the day after tomorrow.

After starting the terminal, enter the/data/software/directory, you can find the current machine directory files are synchronized:



>>> install JDK and tomcat, etc.

Install the relevant JDK and other programs, all installed in the/data/directory:

TAR-ZXVF jdk-7u25-linux-x64.tar.gz-c/data/
MV Jdk1.7.0_25 JDK

Unzip apache-tomcat-7.0.54.zip-d/data/
MV apache-tomcat-7.0.54 Tomcat

Configuring 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=.:${java_home}/lib:${jre_home}/lib
Export Path=${java_home}/bin: $PATH

Export Catalina_home=/data/tomcat
Export Catalina_base=/data/tomcat

Save and exit, the settings take effect immediately:

Source/etc/profile

>>> Writing Startup scripts

When you start TOMCAT, you must implement it through $tomcat_home/bin/catalina.sh,
You cannot start with $tomcat_home/bin/startup.sh, or the container exits immediately after the script executes.

vi/data/start.sh

Add the following content:

#!/bin/bash
# Export environment variable
Source/etc/profile
# Start Tomcat
Bash/data/tomcat/bin/catalina.sh Run

Add executable permissions:

chmod u+x/data/start.sh

>>> Build Images

There are two ways to build a mirror using Docker:

Use the Docker commit command to be more intuitive;

Using the Docker build command and the Dockerfile file, you can templating the mirroring build process;

This creates the image using Docker commit.

To view a list of containers:

sudo docker ps-a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS names39b2cf60a4c1 ae983d5e88ce:latest "/bin/bash" 5 hours ago Exited (0) 9 Seconds ago Dreamy_euclid

To submit a new image:

sudo docker commit 39b2cf60a4c1 Bingyue/docdemo

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

Now look at the local Docker image,

sudo docker images

You can see that the local repository already has the Docker image that 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 months ago 255.1 MB


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

sudo docker inspect Bingyue/docdemo

>>> run the newly created image

Docker run-d-P 18080:8080--name Docdemo bingyue/docdemo/data/start.sh

-P: Represents the port mapping of the host to the container, at which point the 8080 port inside the container is mapped to the 18080 port of the host.

This exposes 18080 ports to the outside, and the Docker Bridge provides access to the 8080 ports inside the container.

To see if the background started successfully:

Docker PS

Test access:

>>> Submit to Docker Warehouse

If you have an account with a Docker repository, you can submit a locally created image to the warehouse.

> Usage Experience

To this point, almost complete the first experience of Docker, Docker application is relatively simple, really complex should be behind the virtualization technology.

Step-by-step deployment, it is true that Docker is a lot simpler than traditional virtual machine technology, and has the opportunity to continue to learn in depth.


Add a Docker user group to avoid sudo input

When Docker is installed by default, each time you execute Docker, you need to run the sudo command, which affects efficiency. If you do not follow sudo, direct execution of the Docker Images command will have the following problems:
Get http:///var/run/docker.sock/v1.18/images/json:dial Unix/var/run/docker.sock:permission denied. is trying to connect to a tls-enabled daemon without TLS?
Adding the current user execution permissions to the appropriate Docker user group will solve this problem.

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 monitor process
sudo service docker restart
# After restarting, try it and whether it takes effect
Docker version
#若还未生效, the system restarts, it takes effect
sudo reboot

Docker Common Commands

# Download an ubuntu image
sudo docker pull Ubuntu
# Use Ubuntu to run an interactive shell
sudo docker run-i-T Ubuntu/bin/bash
#docker PS Command
sudo docker PS #列出当前所有正在运行的container
sudo docker ps-l is #列出最近一次启动的 and running container
sudo docker ps-a #列出所有的container
#port命令
Docker run-p 80:8080 <image> <cmd> #映射容器的8080端口到宿主机的80端口
#删除容器命令
sudo docker rm ' sudo docker ps-a-Q ' #删除所有容器
sudo docker rm $CONTAINER _id# Delete container with container ID container_id
#其他命令快速参考:
sudo docker images #查看本地镜像
sudo docker attach $CONTAINER _id #启动一个已存在的docker实例
sudo docker stop $CONTAINER _id #停止docker实例
sudo docker logs $CONTAINER _id #查看docker实例运行日志 to ensure proper operation
sudo docker inspect $CONTAINER _id #查看container的实例属性 such as IP, etc.
_____________________________________________________________________

Reference:
Using Docker to build a Java WEB environment create a mirror with the commit command

Using Docker to build a Java Web Runtime environment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.