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 |
sudo apt-get update sudo apt-get install docker.io |
Start the service and daemon
?
12 |
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 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 |
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:
?
After the download succeeds, use the images command to view the local mirror list:
?
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 |
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 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/ mv apache-tomcat-7.0.54 tomcat |
Configuring Environment variables
?
Add the following configuration:
?
12345678 |
#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, 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 variable source /etc/profile # Start tomcat bash /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:
?
?
12 |
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 image:
?
1 |
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,
?
You can see that the local repository already has the Docker image that you just created.
?
123 |
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 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:
?
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
?
# Add current user to Docker user group
?
1 |
sudo gpasswd -a bingyue docker |
# Restart the Docker background monitor process
?
1 |
sudo service docker restart |
# After restarting, try it and whether it takes effect
?
#若还未生效, the system restarts, it takes effect
?
Docker Common Commands
# download an Ubuntu image
?
# use Ubuntu to run an interactive shell
?
1 |
sudo docker run -i -t ubuntu /bin/bash |
#docker PS Command
?
123 |
sudo docker ps #列出当前所有正在运行的container sudo docker ps -l #列出最近一次启动的,且正在运行的container sudo docker ps -a #列出所有的container |
#port命令
?
1 |
docker run -p 80:8080 <image> <cmd> #映射容器的8080端口到宿主机的80端口 |
#删除容器命令
?
12 |
sudo docker rm ` sudo docker ps -a -q` #删除所有容器 sudo docker rm $CONTAINER_ID #删除容器id为CONTAINER_ID的容器 |
#其他命令快速参考:
?
12345 |
sudo docker images #查看本地镜像 sudo docker attach $CONTAINER_ID #启动一个已存在的docker实例 sudo docker stop $CONTAINER_ID #停止docker实例 sudo docker logs $CONTAINER_ID #查看docker实例运行日志,确保正常运行 sudo docker 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