Detailed use Docker to build the Java Web running environment _docker

Source: Internet
Author: User
Tags commit java web tomcat docker ps docker hub docker run

This weekend experienced a Docker technology, recording learning notes.

What does >docker do?

Docker is an advanced container engine based on the Linux container (Lxc-linux container), based on the Go language development,

Source code hosted on Github, compliant 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:

Mirror-docker mirrors are similar to common system ISO mirrors and contain information about the application;

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

Warehouse-Warehouse is a place for mirroring, with version control similar to Git, which is also divided into public warehouses and private warehouses (private);

Docker supports most Linux distributions, and by using the Docker container, it can be used on a variety of operating systems,

Running your applications on different machines, without being concerned with configurations such as hardware and running environments, makes the migration of applications very simple.

Comparison of >docker and traditional virtualization technologies

Compared with the traditional virtual machine technology, Docker resources occupy less, start faster, greatly facilitate the deployment of the project and operational dimension.

Docker is virtualization at the operating system level, reusing the local host's operating system, traditionally on the basis of hardware, virtual multiple operating systems, and then deploying related applications on the system.

This image below is a reference to the blog, which 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 Docker installation is already supported in the 14.04 version of the Ubuntu warehouse,
You can use the Quick Install method,

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

Start the service and daemon process

Service Docker.io Status
service Docker.io start

This installation is usually not the latest version of Docker,

If you want to install the latest version, you can go to Docker website download installation.

> Create the first Docker mirror

The general process of constructing Docker mirrors is to first create a container, modify the mirror in the container, configure the environment, and so on, and then submit the modification as a new mirror.

(1) Download image files

Download the system used to make mirrors,

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


Here I pull from the mirror center of the Soul Bird cloud.

Or it can be pulled directly from the mirror center of the Docker, but it looks very slow:

sudo docker pull Ubuntu

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

Docker images

Note that you need to add sudo when using Docker.

After the default installation of Docker, each execution Docker need to run sudo command, if not with sudo, direct execution Docker command will report some permissions error.

(2) Start the container and modify the mirror

Once the mirror is downloaded locally, you can use Docker to run it.

Start the container with the following command parameters.

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

-I: means to run the container in interactive mode

-T: Indicates that the container will enter its command line when it starts

-V: Indicates that the local directory needs to be mounted in the container.

Format:-v < host directory >:< container directory >

My 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 the warehouse name: label name, for example: Index.alauda.cn/alauda/ubuntu:latest.

The above command, you can run a shell with the specified mirror, and if you want to exit the terminal, you can switch to the host machine by using the Exit command, or by pressing CTRL-P+CTRL-Q in turn. In this way, however, 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 have been synchronized:

(3) Installation of JDK and Tomcat, etc.

Install the relevant JDK and other programs, all installed to 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

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, set to take effect immediately:

Source/etc/profile

(4) Write startup script

Tomcat must be started by tomcathome/bin/catalina.sh implementation, cannot be implemented using TOMCATHOME/BIN/CATALINA.SH, cannot be used tomcat_home/bin/ startup.sh boot, or the container will exit immediately after the script executes.

vi/data/start.sh

Add the following:

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

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

(5) Build mirror

Two ways to build a mirror using Docker:

Use Docker commit command, more intuitive;

Using the Docker build command and the Dockerfile file, you can template the image-building process;

This creates a mirror using the Docker commit.

To view the list of containers:

sudo docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39b2cf60a4c1 ae983d5e88ce:latest "/bin/bash" 5 hours ago exited (0) 9 seconds ago Dreamy_euclid

To submit a new mirror:

sudo docker commit 39b2cf60a4c1 Bingyue/docdemo

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

Now look at the local Docker mirror,

sudo docker images

You can see that the local warehouse already has the Docker mirror that you just created.

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
bingyue/docdemo latest bfc7ed316d42 about a minute ago 528.2 MB
Index . alauda.cn/alauda/ubuntu latest Ae983d5e88ce months ago 255.1 MB

Docker inspect can view the details of newly created mirrors:

sudo docker inspect Bingyue/docdemo

(6) running a newly created mirror

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

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

This exposes 18080 ports to the outside world and can be accessed through the Docker Network Bridge to 8080 ports inside the container.

To see if the background started successfully:

 
 

Test access:

(7) Submit to Docker Warehouse

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

> Use Experience

To this step, almost completed the initial experience of Docker, Docker application is relatively simple, the real complex should be behind the virtualization technology.

Deployed step by step, indeed Docker compared to the traditional virtual machine technology to a lot simpler, have the opportunity to continue to further study.

Attach: Add Docker user group, avoid sudo input

After the default installation of Docker, each execution Docker need to run sudo command, affecting 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. Are you trying to connect to a tls-enabled daemon without TLS?

The problem can be solved by adding the current user execution rights to the appropriate 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 Docker background monitoring process

sudo service docker restart

# after reboot, try it and see if it takes effect

Docker version

#若还未生效, the system reboots, and 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 #列出最近一次启动的, and the running container
sudo docker ps-a #列出所有的contain Er

#port命令

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

#删除容器命令

sudo docker rm ' sudo docker ps-a-Q ' #删除所有容器
sudo docker rm $CONTAINER _id# Remove container ID container_id container

#其他命令快速参考:

sudo docker images #查看本地镜像
sudo docker attach $CONTAINER _id #启动一个已存在的docker实例
sudo docker stop $CONTAINER _id #停止d Ocker instance
sudo docker logs $CONTAINER _id #查看docker实例运行日志 to ensure normal operation
sudo docker inspect $CONTAINER _id # View container instance properties, such as IP, etc.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.