《Docker学习与实践.docx》由会员分享,可在线阅读,更多相关《Docker学习与实践.docx(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Docker学习与实践一、docker的安装.依赖包安装yum -y install yum-utils device-mapper-persistent-data lvm2.添加yum源yum-config-manager -add-repo . ustc. edu. cn/docker-ce/1inux/centos/docker-ce. repo yum-config-manager 一一enable docker-ce-edge#配置为安装最新版Docker CE.安装 dockeryum -y install docker-ce*官方为了简化安装流程提供了便捷的安装脚本curl -f
2、sSL get. docker. com -o get-docker.sh sh get-docker, sh -mirror Aliyun.启动 dockersystemctl enable docker & systemctl start docker.建立docker用户和组1 .测试docker是否安装完成dockerdockertest $ docker run hello-worldUnable to find image hello-world:latest locallylatest: Pulling from 1ibrary/hello-worIdca4f61bl923c:
3、Pull completeDigest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7Status: Downloaded newer image for hello-world:latestHello from Docker!This message shows that your installation appears to be working correctly.To1.2.generate this message, Docker took the following steps:3.
4、4.The Docker The Docker (amd64)The Docker executable The Dockerclientdaemondaemoncontacted the Docker daemon.pulled the hello-world“ image from the Docker Hub.created a new container from that image which runs thethat produces the output you are currently reading.daemon streamed that output to the D
5、ocker client, which sent itTo$to your terminal.try something more ambitious, you can run an Ubuntu container with: docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:rootdockertest#docker run -d -p 80:80-name web-mountsource=vl,target=/webapp nginx3f315ab9ed57
6、6d5f0b72dc9e8c067331e0ef785a9577c2c3c6d2e74ec77e51fd rootdockertest # docker exec -it web /bin/bashroot3f315ab9ed57:/# cd /webapp/root3f315ab9ed57:/webapp# touch ss.txtroot3f315ab9ed57:/webapp# exitexitrootdockertest Is /var/1ib/docker/volumes/vl/_data/ Ss. txt查看容器信息rootdockertest docker inspect -f
7、.Mounts webvolume vl /var/1ib/docker/volumes/vl/_ data /webapp local z true 删除数据卷rootdockertest # docker volume rm vl挂载主机目录rootdockertest docker run -d -p 80:80-name web-mounttype=bind,source=/dockerdata, target=/webapp nginx67f90a8a2c6171bfbfce4c84606f0742adb7e283cdb45b488d47035b7f02871b#挂载的主机目录默认权
8、限是读写,也可以通过增加readonly来指定为只读 rootdockertest # docker run -d -p 81:80-name web2-mounttype=bind, source=/dockerdata, target=/webapp, readonly nginxced71fc7a97d251bfea388768e3e45cafe3al2680282d820e032d9845c74albf#加上readonly后在容器内/webapp目录新建文件就会报错rootdockertest docker exec -it web2 /bin/bashrootced71fc7a97
9、d:/# cd webapp/rootced71fc7a97d:/webapp# touch sstouch: cannot touch ss: Read-only file system rootced71fc7a97d:/webapp# exitExit2 .数据卷容器如果用户需要在容器之间共享一些持续更新的数据,可以采用数据卷容器,数据卷容器其实 是一个普通的容器,专门用来提供数据卷供其它容器挂载。创立一个数据卷并挂载数据卷到weblrootdockertest # docker volume create vlvlrootdockertest docker run -dit -moun
10、t source=vl, target=/tmp/test -name webl centos237879201elc8fedae870af923083625ab8d0fb2b375f66784elda4179e068c7 rootdockertest # docker exec -it webl Is -d /tmp/test/tmp/test创立两个容器并从webl挂载数据卷rootdockertest docker run -dit -volumes-from web1 -name dbl centos 0b656f36fe24ce835b94d2c891645962e6545e3el8
11、c70bbc6a3e24edbd45fl53rootdockertest docker run -dit -volumes-from webl -name db2 centos4468244e0b8b7d6caa57801a2c98dl272c6fdb7d59e89ce704533b53fe969b70 rootdockertest # docker inspect -f .Mounts” db2volume vl /var/1ib/docker/volumes/vl/_data /tmp/test local true 分别在webl和dbl中新建两个测试文件rootdockertestdo
12、cker exec -it webl touch /tmp/test/webl. txtrootdockertestdocker exec -it dbl touch /tmp/test/dbl.txt#在db2和本地主机中查看rootdockertest docker exec -it db2 Is /tmp/test dbl.txt web1. txtrootdockertest # Is /var/1ib/docker/volumes/vl/_data/ dbl. txt webl. txt使用db2作为db3的容器数据卷rootdockertest docker run -dit -v
13、olumes-from db2 -name db3 centos 7ccd5f862cel25121b2e216c3f312cl921a41df0214a327e957al5bd2041cf07rootdockertest docker exec -it db3 Is /tmp/test dbl.txt webl. txt停止webl并查看关联容器的挂载文件 rootdockertest docker stop webl weblrootdockertest docker exec -it db3 Is /tmp/test dbl.txt web1. txt3 ,使用数据卷容器迁移数据创立数据
14、卷并挂载rootdockertest docker volume create v2 v2rootdockertest docker run -it -mount source二v2, target=/backup -name datamove centosroot19de5488667a /# cd /backup/root19de5488667a backup# touch a,b, c,d,ssroot19de5488667a backup# Is a b c d ss数据卷的备份rootdockertest 1# docker run -volumes-from datamove -v
15、 /dockerdata/:/back 一name backup centos tar cvf /back/backup. tar /backuptar: Removing leading / from member names/backup/backup/a/backup/b/backup/c/backup/d/backup/ssrootdockertest Is /dockerdata/ backup, tar创立一个容器savedata还原数据卷rootdockertest docker run -volumes-from datamove -v /dockerdata/:/back-n
16、ame savedata centos tar xvf /back/backup. tarbackup/backup/abackup/bbackup/cbackup/dbackup/ss创立一个容器挂载savedatarootdockertest docker run -dit -volumes-from savedata -name savetest centosfaa008b4fl8360b0bed3619f740ccc6a326d7e718020347bdb3027750d48ef60rootdockertest docker exec -it savetest Is /backupa
17、b c d ss六、网络配置1 .端口映射一对一映射rootdockertest docker run -dit -p 80:80 -name portl centos多对多映射Erootdockertest docker run -dit -p 8088:8088 -p 8080:8080 -name port2 centos随机映射一个端口rootdockertest docker run -dit -p :80 -name port3 centos映射UDP端口rootdockertest docker run -dit -p :80/udp -name port4 centos查看端口
18、映射rootdockertest docker container IsCONTAINER IDSTATUSPORTSNAMESdbdbe92054f2centos/bin/bash7 minutes agoUp 7 minutes0. 0. 0. 0:32769-80/udpport43880ae523333centos/bin/bash7 minutes agoUp 7 minutes0. 0. 0. 0:32769-80/tcpport38293f668125fcentos/bin/bash7 minutes agoUp 7 minutes0. 0. 0. 0:8080-8080/tcp
19、, 0. 0. 0. 0:8088-8088/tcpport2e01160bll472centos/bin/bash8 minutes agoUp 7 minutes0. 0. 0. 0:80-80/tcpportlIMAGECOMMANDCREATED.容器互联新建一个网络rootdockertestdocker network create -d bridge my-netcf09779c2aac2043c84b98a9728ed597c2dac7e8f67c8946b57dc4b9aa3f7cd2 rootdockertestdocker network IsNETWORK IDNAME
20、DRIVERSCOPEda971fe6813bbridgebridgelocaleec69c6ab2dahosthostlocalcf09779c2aacmy-netbridgelocald2be30ca65banonenulllocal运行一个容器并连接到新建my-net的网络rootdockertest docker run -it -rm -name webl -network my-net centos rootac92ecff44el /#翻开新的终端,再运行一个容器并加入到my-net网络rootdockertest docker run -it -rm -name web2 -n
21、etwork my-net centos roota6e5609d4e6f /#测试连接roota6e5609d4e6f /# ping weblPING webl (172. 18. 0. 2) 56(84) bytes of data.64 bytes from webl. my-net64 bytes from webl. my-net(172. 18. 0. 2)(172. 18. 0. 2)icmp seq=l ttl=64 time=0. 102 msicmp seq=2 ttl=64 time=0. 045 ms64 bytes from webl. my-net(172. 18
22、. 0. 2)icmp_seq=3ttl=64 time=0. 053 ms webl ping statistics 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 0. 045/0. 066/0. 102/0. 026 ms roota6e5609d4e6f /#2 .配置DNS在容器中查看挂载信息Lrootac92ecff44el /# mount | grep etc/dev/mapper/centos-rooton(rw, relatime, attr2, in
23、ode64, noquota) /dev/mapper/centos-rooton(rw, relatime, attr2, inode64, noquota) /dev/mapper/centos-rooton(rw, relatime, attr2, inode64, noquota) rootac92ecff44el /#/dev/mapper/centos-rooton(rw, relatime, attr2, inode64, noquota) /dev/mapper/centos-rooton(rw, relatime, attr2, inode64, noquota) /dev/
24、mapper/centos-rooton(rw, relatime, attr2, inode64, noquota) rootac92ecff44el /#/etc/resolv. conftypexfs/etc/hostnametypexfs/etc/hoststypexfs配置通过这种机制可以让宿主主机DNS信息发生更新后,所有Docker容器的DNS/etc/resolv. conf文件会得到更新。配置全部容器的DNS可以在/etc/docker/daemon, json中增加DNS相关项来设置 dns :z,114. 114. 114. 114z,,8. 8. 8. 8)#这样每次启
25、动容器时容器的DNS会自动配置为添加的地址rootdockertest docker run -it -rm centos cat etc/resolv. conf search localdomainnameserver 114. 114. 114.114nameserver 8. 8. 8. 8如果想要手动指定容器的配置,可以在使用docker run命令启动容器时加入如下参数: -h设定容器的主机名,它会被写到容器内的/etc/hostname和/etc/hosts中,但它在容器 外部看不到,既不会在docker ps中显示,也不会在其他的容器的/etc/hosts看到。-dns-IP_
26、ADDRESS添加DNS服务器到容器的/etc/resolv. conf中,让容器用这个服务器 来解析所有不在/etc/hosts中的主机名。-dns-search=DOMAIN设定容器的搜索域,当设定搜索域为.example, com时,在搜索一个名 为host的主机时,DNS不仅搜索host,还会搜索host, example, com。七、Docker Compose1 .简介使用一个Dockerfile模板文件,可以让用户很方便的定义一个单独的应用容器。然而, 在日常工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个 Web工程,除了 Web服务容器本身,往往还
27、需要再加上后端的数据库服务容器,甚至还包 括负载均衡容器等。Compose恰好满足了这样的需求。它允许用户通过一个单独的docker-compose, yml模 板文件(YAML格式)来定义一组相关联的应用容器为一个工程(project)o 使用Compose基本上是一个三步过程:1 .用一个定义你的应用程序的环境,Dockerfile这样它就可以在任何地方再现。2 .定义组成您的应用程序的服务,docker-compose. yml以便它们可以在隔离的环境中一起 运行。3 .运行docker-compose up和撰写启动并运行您的整个应用程序。4 .安装在Linux上安装直接从官方GitH
28、ub Release处下载编译好的二进制文件。rootdockertest#curl-L. com/docker/compose/releases/download/1. 21. 2/docker-compose-unam e -s-uname -nf -o /usr/local/bin/docker-composerootdockertest chmod +x /usr/local/bin/docker-compose rootdockertest docker-compose -versiondocker-compose version 1.21.2, build al33471#需要卸载
29、时直接删除二进制文件即可.入门使用.新建文件夹并创立一个app. py文件 rootdockertest # mkdir test rootdockertest cd test/ rootdockertest test# cat app. py from flask import Flask from redis import Redis app = Flask(name)redis = Redis (host=redis, port=6379) app. route ( /) def hello():count = redis. incr(hits)return ? Hello World!
30、该页面已被访问次。n,. format (count) if name = _main_:app. run (host=0 0. 0. 0, debug=True).创立一个Dockerfile文件 rootdockertest test# cat Dockerfile FROM python:3. 6-alpine ADD . /code WORKDIR /codeRUN pip install redis flask CMD python,app. py.创立一个 docker-compose. yml 文件 rootdockertest test# cat docker-compose.
31、 yml version: 3 services:web:build:. ports:-5000:5000redis:image: redis:alpine”#这个compose文件定义了两个服务,web和rediso#web服务使用当前目录中的Dockerfile构建镜像,并映射5000端口。#redis服务使用公共库中的redis镜像.运行compose工程rootdockertest test# docker-compose up.访问C。 192.168.10.131:5000Hello World!该页面已被访问2次。刷新页面可以看见数字增加切换终端查看运行的容器rootdocker
32、test # docker psCONTAINER IDIMAGECOMMANDSTATUS 7846aed7560eUp 2 minutes f7c8c051d057 Up 2 minutesPORTS redis:alpine 6379/tcp test_webNAMES“docker-entrypoint stest_redis_lpython app. py0.0.0.0:5000-5000/tcp test_web_lCREATED3 minutes ago3 minutes ago,停止rootdockertestrootdockertesttest# docker-compose
33、 downStopping test_redis_l . doneStopping testweb 1.doneRemoving test_redis_l . doneRemoving testwebl . doneRemoving network test_default4.实例:WordPress.创立一个文件夹并新建docker-compose. yml文件rootdockertest mkdir wordpressrootdockertest # cd wordpress/rootdockertest wordpress# cat docker-compose. yml version
34、: 3services:db:image: mysql:5. 7 volumes:- db data:/var/lib/mysqlrestart: alwaysenvironment:MYSQL_ROOT_PASSWORD: somewordpressMYSQL DATABASE: wordpressMYSQLUSER: wordpressMYSQL_PASSWORD: wordpresswordpress:depends_on:- dbimage: wordpress:latestports:- 8000:80restart: alwaysenvironment:WORDPRESS_DB_H
35、OST: db:3306WORDPRESS_DB_USER: wordpressWORDPRESS_DB_PASSWORD: wordpress volumes:dbdata:.构建和运行rootdockertest wordpress# docker-compose up -dCreating network wordpress default with the default driver Creating volume wordpress_db_data with default driver Pulling db (mysql:5. 7).5. 7: Pulling from libr
36、ary/mysqlf2aa67a397c4 laccf44cb7e0 2d830ea9fa68 740584693b89 4d620357ec48 ac3b7158d73d a48d784ee503 bfll94add2f3 0e5c74178a02 e9201d309436 bfIac4524e8ef2aa67a397c4 laccf44cb7e0 2d830ea9fa68 740584693b89 4d620357ec48 ac3b7158d73d a48d784ee503 bfll94add2f3 0e5c74178a02 e9201d309436 bfIac4524e8ePull co
37、mplete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull completeDigest: sha256:f030e84582d939d313fe2ef469b5c65ffd0f7dff3b4b98e6ec9ae2dccd83dcdfStatus: Downloaded newer image for mysql:5. 7Pulling wordpress (wordpress:l
38、atest).latest: Pulling from library/wordpressf2aa67a397c4: Already exists4c3122805fd6: Pull complete98ce407eel8a: Pull complete8a56b8flca72 3eb04e3939f2 f5ecc66c0a5f 30ebdb4a78el 9e9c0e2ba7fl da976026223e c97e6cef8eb4 d7d37db38fe6 4fbe9ea008c2 b5247a62a2b6 a430e65efb58 9afdee095b5b 12d5f9al9746 f785
39、e67a07ef 5663bbfd9606 ffl3a5b7e05e 710c4b4563738a56b8flca72 3eb04e3939f2 f5ecc66c0a5f 30ebdb4a78el 9e9c0e2ba7fl da976026223e c97e6cef8eb4 d7d37db38fe6 4fbe9ea008c2 b5247a62a2b6 a430e65efb58 9afdee095b5b 12d5f9al9746 f785e67a07ef 5663bbfd9606 ffl3a5b7e05e 710c4b456373Pull complete Pull complete Pull
40、complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull complete Pull completeDigest: sha256:b0460dball737144b232a7794403d4052982f2332caeea82f618fc98d0547387Status: Downloade
41、d newer image for wordpress:latestCreating wordpress_db_l . doneCreating wordpress_wordpress_l . done.访问(D 192.168.10.1 31 8idrr- Il r)hpSlovencma SlovenscinaShqipCpncKM je3MK Svenska $ldnskd godka 丹心gIwtjTagalogTurtcqeTarap Tene Reo TahitiYKpaiHCbKaOzbekcha Tieng Viet 古港中文版擎髓中文Continue八、Docker Mach
42、ine.安装rootdockertestcurl-L. com/docker/machine/releases/download/vO. 15. 0/docker-machine-$(un ame -s)-$(uname -m) /tmp/docker-machinerootdockertest # chmod +x /tmp/docker-machinerootdockertest # cp /tmp/docker-machine /usr/local/bin/docker-machine rootdockertest docker-machine -vdocker-machine vers
43、ion 0. 15.0, build b48dc28d.在远程主机上安装Docker远程环境准备a.新建一台远程主机并完成基础配置b.配置到远程主机ssh免密登陆执行安装rootdockertest # docker-machine create -d generic 一一generic-ip-address=192. 168.10.128 -generic-ssh-user=root -generic-ssh-key V. ssh/id_rsa machinelRunning pre-create checks.Creating machine.(machinel) Importing SS
44、H key.Waiting for machine to be running, this may take a few minutes.Detecting operating system of created instance.Waiting for SSH to be available.Detecting the provisioner.Provisioning with centos.Copying certs to the local machine directory.Copying certs to the remote machine.Setting Docker confi
45、guration on the remote daemon.Checking connection to Docker.Docker is up and running!To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env machinel rootdockertest docker-machine IsNAMEACTIVEDRIVERSTATEURLSWARM DOCKER ERRORSmachinel -ge
46、neric Running tcp:/192.168.10. 128:2376vl8. 05. 0-ce查看machinel的信息rootdockertest docker-machine env machinelexport DOCKER_TLS_VERIFY=/,r,export D0CKER_H0ST=,ztcp:/192. 168. 10. 128:2376export DOCKER_CERT_PATH=,Z/root/. docker/machine/machines/machiner,export DOCKER_MACHINE_NAME=machine/Run this command to configure your shell:. docker, com/For more examples and ideas, visit: . docker. com/engine/userguide/7.配置镜像加速器并重启服务 cat /etc/docker/daemon, json (“registry-mirrors