docker introduction
Official website www.docker.com
github https://github.com/docker/docker.github.io
The open source container engine allows developers to package applications and dependent libraries, and then publish them to any popular Linux distribution, which is very convenient for porting
Written in go language, released based on apache2.0 protocol
Based on linux kernel, if you want to run under win, you need to use a vm (virtual machine) to achieve
Since 2013, it has developed rapidly in recent years
Starting from 1.13x, the version of docker is divided into community version ce and enterprise version ee, and based on the timeline of year and month, the current latest stable version is 17.09. Reference http://blog.csdn.net/chenhaifeng2016/article/details/68062414
Comparison of Docker and traditional virtualization
Advantages of Docker
Start-up is very fast, realized in seconds
High resource utilization, a high configuration server can run thousands of docker containers
Faster delivery and deployment, once created and configured, it can be run anywhere
Kernel-level virtualization does not require additional hypevisor support and will have higher performance and efficiency
Easy to migrate, not strong platform dependence
Docker core concepts
Mirror is a read-only template, similar to the iso file used in the installation system. We use mirroring to complete the deployment of various applications.
Container , the image is similar to the operating system, and the container is similar to the virtual machine itself. It can be started, started, stopped, deleted, etc., and each container is isolated from each other.
Warehouse , a place for storing mirror images, warehouses are divided into public warehouses and private warehouses. The largest public warehouse is Docker hub (hub.docker.com), domestic public warehouse (dockerpool.com)
Docker installation
installation steps
method one:
1. Download a repo's yum source (provided by the official)
curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
2. Install the community edition:
yum install -y docker-ce
Method Two:
(The speed is slow, you can also download the rpm package directly)
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
After downloading, uploading to linux, you also need to install with yum, you can automatically resolve the dependencies, specify by yourself
yum install -y docker-ce-xxxx.rpm
3. Start docker:
systemctl start docker
Docker image management
Download the image : docker pull centos
[root@awei-01 ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos 7a0437f04f83: Pull complete Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest
Configure docker accelerator (refer to http://blog.csdn.net/xlemonok/article/details/71403534 )
vi /etc/docker/daemon.json//Add the following content
{ "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"] }
Note: This url is the address of the accelerator and students need to apply to Alibaba Cloud by themselves
After configuring the accelerator, restart the docker service, docker pull centos again will be much faster
View the local image : docker images
[root@awei-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 300e315adb2f 4 weeks ago 209MB
Search mirror : docker search xxx //Search mirror, where xxx is a keyword
Mirror tag : docker tag Mirror name tag //Tag the mirror TAG is his tag
[root@localhost ~]# docker tag centos6 centos6:v1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6 latest fa529b27048c 28 hours ago 512MB centos6 v1 fa529b27048c 28 hours ago 512MB registry latest 678dfa38fcfa 3 weeks ago 26.2MB
The image starts as a container : docker run -itd centos bash
[root@awei-01 ~]# docker run -itd centos bash bce3d5c8d148fab8cebf15ea288f799b92cdae4541e9c604efab7e52ac7eb1a3
-i means to open the standard input of the container, -t means to assign a pseudo terminal, -d means to start in the background , put -i -t -d in front of the image name centos Note: After the image is started, it will become a container
View running containers : docker ps //After adding the -a option, you can view all containers, including those that are not running
[root@awei-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bce3d5c8d148 centos "/bin/bash" 47 seconds ago Up 46 seconds nervous_black
Delete the specified mirror : docker rmi centos
The latter parameter can be a tag, if it is a tag, the tag is actually deleted. When the latter parameter is the mirror ID, the entire mirror will be completely deleted, and all tags will be deleted at the same time
[root@awei-01 ~]# docker rmi 300e315adb2f Untagged: centos:latest Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55 Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859
Close the container : the ID of the container started by docker stop
Container ID started by docker rm -f
Docker creates images through containers
After docker run starts the container, you can enter the container through the following command
Enter the container : docker exec -it xxxxx bash//where xxxxx is the container id, this id can be viewed with docker ps, the last bash is the command we want to execute after entering the container, so that we can open a terminal
[root@awei-01 ~]# docker exec -it bce3d5c8d148 bash [root@bce3d5c8d148 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@bce3d5c8d148 /]#
Create image : docker commit -m "change/description" -a "author information" container_id new image name //container_id is obtained through docker ps -a
E.g:
Enter the container, we make some changes, such as installing something, and then create a new image for this container, execute yum install -y net-tools in the container, and then ctrl d to exit the container
Create a change mirror: docker commit -m "install net-tools" -a "Aming" 2c74d574293f centos_with_nettool This command is a bit like a svn submission, -m adds some change information, -a specifies the author-related information 2c74d as the container id, The name of the new mirror
Docker uses templates to create images
First go to download a template
wget http://download.openvz.org/template/precreated/centos-6-x86-minimal.tar.gz// The download speed is not fast, I downloaded a centos6 template centos-6-x86-minimal.tar.gz
The command to import the image is:
cat centos-6-x86-minimal.tar.gz|docker import - centos6
docker images view the imported image
[root@awei-01 ~]# cat centos-6-x86-minimal.tar.gz|docker import - centos6 sha256:730dac82b660ad9037c2f3cad6b6f6512424ea5431a05de5a20a4704bd446841 [root@awei-01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6 latest 730dac82b660 58 seconds ago 512MB centos latest 300e315adb2f 4 weeks ago 209MB ubuntu latest f643c72bc252 6 weeks ago 72.9MB
Export the existing image as a file :
docker save -o export name image name
[root@awei-01 ~]# docker save -o centos.tar.gz centos [root@awei-01 ~]# ls 1.php centos-6-x86-minimal.tar.gz data.txt rpmbuild anaconda-ks.cfg centos.tar.gz memcache-2.2.3.tgz yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
Use this file to restore the local mirror :
docker load --input aming-centos.tar 或者 docker load < aming-centos.tar
[root@awei-01 ~]# docker load --input centos.tar.gz 2653d992f4ef: Loading layer [==================================================>] 216.5MB/216.5MB Loaded image: centos:latest
docker push image_name //You can upload your own image to the dockerhub official website, but the premise is that you need to register a user first, and then study it if you need it later