介绍
先展示下官方介绍的docker运行原理
第一要点 这里只是一个镜像,它分为四层只读层,而这四个只读层堆叠成一个Ubuntu的镜像。
第二要点 docker的存储驱动负责管理镜像内的各个数据层并提供一个对外的统一视图,当你启动一个新的容器时会出现下面图示,这一层也叫做容器层,所有的操作改变和新增文件都是发生在这里。这些东西默认存储在
第三要点 Docker 1.10引入了一个新的内容可寻址存储模型。 这是一种处理磁盘上的镜像和图层数据的全新方法。 以前,使用随机生成的UUID来引用和存储图像和层数据。 在新模型中这由安全内容哈希(content hash)替换。
新的方式更安全,更能保证你在执行pull,push,save操作时的数据一致性
开始实验
在docker里面镜像在运行文件系统和参数是,它永远不会发生变更。而容器就是镜像运行时的一个实例。
当你在docker上运行一个命令时他会运行以下几个操作:
1.检查镜像是否存在
2.如果没有就下载
3.加载镜像运行你要的操作
查看本地容器
[root@salt-node1 ~]# docker p_w_picpathsREPOSITORY TAG IMAGE ID CREATED SIZEcentos7-ssh latest 14a2c2ad7cb1 29 hours ago 320.5 MBregistry.cn-hangzhou.aliyuncs.com/forker/centos7-ssh last 14a2c2ad7cb1 29 hours ago 320.5 MBcentos-ssh latest b21aa4e2d825 29 hours ago 320.5 MBcentos7sshv1 latest c1a9c3f2f823 30 hours ago 320.5 MBdocker.com/nginxs/centos7 last 00f6c5817350 33 hours ago 191.8 MB
查×××端的容器
docker search [p_w_picpath name]
[root@salt-node1 ~]# docker search nginxNAME DESCRIPTION STARS OFFICIAL AUTOMATEDnginx Official build of Nginx. 5079 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 915 [OK]richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 329 [OK]million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... 75 [OK]webdevops/php-nginx Nginx with PHP-FPM 65 [OK]maxexcloo/nginx-php Framework container with nginx and PHP-FPM... 58 [OK]h3nrik/nginx-ldap NGINX web server with LDAP/AD, SSL and pro... 33 [OK]bitnami/nginx Bitnami nginx Docker Image 22 [OK]evild/alpine-nginx Minimalistic Docker p_w_picpath with Nginx 11 [OK]gists/nginx Nginx on Alpine 8 [OK]webdevops/nginx Nginx container 7 [OK]maxexcloo/nginx Framework container with nginx installed. 7 [OK]1science/nginx Nginx Docker p_w_picpaths that include Consul Te... 4 [OK]blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 4 [OK]ixbox/nginx Nginx on Alpine Linux. 3 [OK]dock0/nginx Arch container running nginx 2 [OK]servivum/nginx Nginx Docker Image with Useful Tools 2 [OK]frekele/nginx docker run --rm --name nginx -p 80:80 -p 4... 2 [OK]xataz/nginx Light nginx p_w_picpath 2 [OK]drupaldocker/nginx NGINX for Drupal 2 [OK]tozd/nginx Dockerized nginx. 1 [OK]unblibraries/nginx Baseline non-PHP nginx container 0 [OK]watsco/nginx nginx:1.11 0 [OK]c4tech/nginx Several nginx p_w_picpaths for web applications. 0 [OK]funkygibbon/nginx nginx + openssl automated build, customisa... 0 [OK]
拉取容器到本地
[root@controller ~]#docker pull nginxlatest: Pulling fromnginx2c49f83e0b13: Pullcomplete4a5e6db8c069: Pullcomplete08ecf065655b: Pullcompleteff0618bc0767: Pullcomplete12a77b8bf89a: Pullcomplete5dde53921c3f: Pullcompletea53219dc4d2f: Pullcomplete8c7e9b6e3131: Pullcompletef9bff7d0d06e: Pullcomplete3ac9cfbdf572: Pullcomplete491aec45eaf8: Pullcompletecd3cf76a61ee: PullcompleteDigest:sha256:e2dbdc9824482b79050a67c1e6143365d0eeefcc77bf0e22cc2715d91b8d1ad4Status: Downloadednewer p_w_picpath for nginx:latest
运行容器
把容器里面的80端口映射到本机的"0.0.0.0:80"
-p 把容器内端口映射到本地 -p [本地端口]:[容器内端口][root@controller~]# docker run -p 0.0.0.0:80:80-d nginx7153768b14e2ab37856d63ccf2fd15c87f1c57f1f2a456b05131fe923930fce7
查看启动容器列表
[root@controller ~]#docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7153768b14e2 nginx "nginx -g 'daemon of 7 minutes ago Up 7 minutes 443/tcp, 0.0.0.0:81->80/tcp modest_poincare b9cb57eb22f9 nginx "nginx -g 'daemon of 9 minutes ago Up 9 minutes 80/tcp, 443/tcp,0.0.0.0:80->8080/tcp furious_mccarthy
操作容器启动,关闭,重启,kill
docker {start|stop|restart|kill} [CONTAINER ID]
[root@controller ~]#docker kill b9cb57eb22f9
启动一个容器并多个端口
docker run -d --name"web-node1" -p 8022:22 -p80:80 nginx /usr/sbin/sshd -D
本地数据数据映射到容器内
-d 守护进程方式运行-v 映射数据卷 -v [本地目录]:[容器内目录]docker run -d -it -v /root/www/:/mnt/ --name"web-node2" -p 8024:22-p 80:80 nginx /usr/sbin/sshd-D