Detailed use of Docker to build the Java Web Runtime environment

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

What does >docker do?

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 following picture, which looks at the relevant blog post, 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.

Quickly install Docker

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

?
12 sudoapt-get updatesudo apt-get installdocker.io

Start the service and daemon

?
12 service docker.io statusservice 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 first create a container, modify the image in the container, configure the relevant environment, and finally commit the changes as a new image.

(1) Download image file

Download the system used to make the image,

?
1 sudodocker 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:

?
1 sudodocker pull ubuntu

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

?
1 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.

(2) Start the container and modify the image

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:

?
1 sudodocker 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 press ctrl-p+ctrl-q in turn to switch to the host machine. 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:

(3) Installing JDK and Tomcat, etc.

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

?
12345 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/mvapache-tomcat-7.0.54 tomcat

Configuring Environment variables

?
1 vi/etc/profile

Add the following configuration:

?
12345678 #set java environment  export JAVA_HOME=/data/jdkexport JRE_HOME=${JAVA_HOME}/jreexport CLASSPATH=.:JAVAHOME/lib:JAVAHOME/lib:{JRE_HOME}/libexport PATH=JAVAHOME/bin:JAVAHOME/bin:PATH export CATALINA_HOME=/data/tomcatexport CATALINA_BASE=/data/tomcat

Save and exit, the settings take effect immediately:

Source/etc/profile

(4) Writing startup scripts

When you start Tomcat, you must implement it through tomcathome/bin/catalina.sh, you cannot use the tomcathome/bin/catalina.sh implementation, and you cannot use tomcat_home/bin/ Startup.sh is started, or the container exits immediately after the script executes.

vi/data/start.sh

Add the following content:

?
12345 #!/bin/bash# Export environment variablesource/etc/profile# Start tomcatbash/data/tomcat/bin/catalina.sh run

Add executable permissions: chmod u+x/data/start.sh

(5) Build image

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:

?
1 sudodocker ps-a
?
12 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:

?
1 sudodocker 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,

?
1 sudodocker images

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

?
123 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEbingyue/docdemolatest bfc7ed316d42 About a minute ago 528.2 MBindex.alauda.cn/alauda/ubuntulatest 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

?
1 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:

?
1 docker ps

Test access:

(7) 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.

Attached: Add 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

?
1 sudogroupadd docker

# Add current user to Docker user group

?
1 sudogpasswd -a bingyue docker

# Restart the Docker background monitor process

?
1 sudoservice docker restart

# After restarting, try it and whether it takes effect

?
1 docker version

#若还未生效, the system restarts, it takes effect

?
1 sudoreboot

Docker Common Commands

# download an Ubuntu image

?
1 sudodocker pull ubuntu

# use Ubuntu to run an interactive shell

?
1 sudodocker run -i -t ubuntu /bin/bash

#docker PS Command

?
123 sudodocker ps #列出当前所有正在运行的containersudo docker ps -l #列出最近一次启动的,且正在运行的containersudo docker ps-a #列出所有的container

#port命令

?
1 docker run -p 80:8080 <image> <cmd> #映射容器的8080端口到宿主机的80端口

#删除容器命令

?
12 sudodocker rm `sudo docker ps -a -q`#删除所有容器sudo docker rm$CONTAINER_ID#删除容器id为CONTAINER_ID的容器

#其他命令快速参考:

?
12345 sudodocker images #查看本地镜像sudo docker attach $CONTAINER_ID #启动一个已存在的docker实例sudo docker stop $CONTAINER_ID #停止docker实例sudo docker logs $CONTAINER_ID #查看docker实例运行日志,确保正常运行sudodocker inspect $CONTAINER_ID #查看container的实例属性,比如ip等等

Original link: http://www.cnblogs.com/binyue/p/5015284.html

To use Docker to build a Java Web Runtime environment

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.