一、Docker基础环境的安装
1. 环境说明
本机采用操作系统如下: CentOS-7.3-X86-64,内核3.10 x64位,docker 1.12.x版本。
建议采用CentOS7版本或ubuntu版本,本文采用CentOS7即CentOS-7-x86_64-DVD-1611.iso版本最小化安装。
Docker可以在虚拟机环境中运行进行使用测试。本文出自:http://koumm.blog.51cto.com/
2. 主机网络
1)关闭firewalld防火墙和SELinux
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
systemctl disable firewalld
systemctl stop firewalld
docker是必须要防火墙功能模块的,个人目前用iptables
systemctl disable iptables.service
systemctl stop iptables.service
注意最小化需要补充安装的软件包:
yum install vim net-tools iptables-config
2)配置hosts文件(主机名称)
echo "192.168.1.90 docker1" >> /etc/hosts
3. 安装Docker软件包
1)可以直接安装包yum源
# yum install -y docker
2)配置docker开机自启动以及启动服务
# systemctl start docker.service
# systemctl enable docker.service
3)查看docker服务器,表示docker已安装成功
查看版本:docker version
帮助信息:docker --help
概要信息:docker info
镜像查看:docker images
进程查看:docker ps -a
搜索镜像:docker search centos #搜索可用docker镜像
示例: 表示docker已安装成功。
[root@docker1~]#dockerinfo Containers:4 Running:0 Paused:0 Stopped:4 Images:3 ServerVersion:1.12.6 StorageDriver:devicemapper PoolName:docker-253:0-16798063-pool PoolBlocksize:65.54kB BaseDeviceSize:10.74GB BackingFilesystem:xfs Datafile:/dev/loop0 Metadatafile:/dev/loop1 DataSpaceUsed:849.3MB DataSpaceTotal:107.4GB DataSpaceAvailable:15.27GB MetadataSpaceUsed:1.937MB MetadataSpaceTotal:2.147GB MetadataSpaceAvailable:2.146GB ThinPoolMinimumFreeSpace:10.74GB UdevSyncSupported:true DeferredRemovalEnabled:false DeferredDeletionEnabled:false DeferredDeletedDeviceCount:0 Dataloopfile:/var/lib/docker/devicemapper/devicemapper/data WARNING:Usageofloopbackdevicesisstronglydiscouragedforproductionuse.Use`--storage-optdm.thinpooldev`tospecifyacustomblockstoragedevice. Metadataloopfile:/var/lib/docker/devicemapper/devicemapper/Metadata LibraryVersion:1.02.135-RHEL7(2016-09-28) LoggingDriver:journald CgroupDriver:systemd Plugins: Volume:local Network:bridgehostnulloverlay Swarm:inactive Runtimes:docker-runcrunc DefaultRuntime:docker-runc SecurityOptions:seccomp KernelVersion:3.10.0-514.el7.x86_64 OperatingSystem:CentOSLinux7(Core) OSType:linux Architecture:x86_64 NumberofDockerHooks:2 cpus:2 TotalMemory:984.8MiB Name:docker1 ID:YZZ7:NRHH:WJM7:PYUR:DGRH:4MLD:4GT4:UR2I:5PUZ:EAGS:P4Z2:JSL4 DockerRootDir:/var/lib/docker DebugMode(client):false DebugMode(server):false Registry:https://index.docker.io/v1/ InsecureRegistries: 127.0.0.0/8 Registries:docker.io(secure)
二、Docker使用基础
1. 下载docker基础镜像
1)搜索centos基础镜像
[root@docker1~]#dockersearchcentos INDEXNAMEDESCRIPTIONSTARSOFFICIALAUTOMATED docker.iodocker.io/centosTheofficialbuildofCentOS.3362[OK] docker.iodocker.io/jdeathe/centos-sshCentOS-66.9x86_64/CentOS-77.3.1611x8...69[OK] docker.iodocker.io/nimmis/java-centosThisisdockerimagesofCentOS7withdif...26[OK] docker.iodocker.io/gluster/gluster-centosOfficialGlusterFSImage[CentOS-7+Glu...19[OK] docker.iodocker.io/million12/centos-supervisorBaseCentOS-7withsupervisordlauncher,h...16[OK] docker.iodocker.io/kinogmt/centos-sshCentOSwithSSH13[OK] docker.iodocker.io/egyptianbman/docker-centos-Nginx-PHPAsimpleandhighlyconfigurabledockerco...9[OK] docker.iodocker.io/torusware/speedus-centosAlwaysupdatedofficialCentOSdockerimag...8[OK] docker.iodocker.io/centos/mariadb55-centos75[OK] docker.iodocker.io/nathonfowlie/centos-jreLatestCentOSimagewiththeJREpre-insta...5[OK] docker.iodocker.io/darksheer/centosBaseCentosImage--Updatedhourly3[OK] docker.iodocker.io/harisekhon/centos-javaJavaonCentOS(OpenJDK,tagsjre/jdk7-8)2[OK] docker.iodocker.io/harisekhon/centos-scalaScala+CentOS(OpenJDKtags2.10-jre7-2...2[OK] docker.iodocker.io/blacklabelops/centosCentOSBaseImage!BuiltandUpdatesDaily!1[OK] docker.iodocker.io/freenas/centosSimpleCentOSLinuxinteractivecontainer1[OK] docker.iodocker.io/sgfinans/docker-centosCentOSwitharunningsshdandDocker1[OK] docker.iodocker.io/timhughes/centosCentoswithsystemdinstalledandrunning1[OK] docker.iodocker.io/vorakl/centosCentOS7,EPEL,tools.Updated/Testeddaily!1[OK] docker.iodocker.io/grossws/centosCentOS6and7baseimageswithgosuandl...0[OK] docker.iodocker.io/januswel/centosyumupdate-edCentOSimage0[OK] docker.iodocker.io/kz8s/centosOfficialCentOSplusepel-release0[OK] docker.iodocker.io/labengine/centosCentosimagebase0[OK] docker.iodocker.io/repositoryjp/centosDockerImageforCentOS.0[OK] docker.iodocker.io/vcatechnology/centosACentOSImagewhichisupdateddaily0[OK] docker.iodocker.io/wenjianzhou/centoscentos0[OK] [root@docker1~]#
2)下载centos基础镜像
本文采用官方默认镜像仓库下载。
[root@docker1 ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
343b09361036: Pull complete
Digest: sha256:bba1de7c9d900a898e3cadbae040dfe8a633c06bc104a0df76ae24483e03c077
下载其它示例:下载支持ssh服务器的centos镜像
[root@docker1~]#dockerpulljdeathe/centos-ssh Usingdefaulttag:latest Tryingtopullrepositorydocker.io/jdeathe/centos-ssh... latest:Pullingfromdocker.io/jdeathe/centos-ssh 45a2e645736c:Pullcomplete f0d571ff94cf:Pullcomplete ea228e12ac20:Pullcomplete edd63eef0a83:Pullcomplete 27c3eb39991d:Pullcomplete 4bf26ef9f3dd:Pullcomplete 4e572750aff7:Pullcomplete b94a26882ae9:Pullcomplete 91ee52de11b1:Pullcomplete e89f8a480c42:Pullcomplete bd26710715b3:Pullcomplete 75c421c47829:Pullcomplete f6eabaf37913:Pullcomplete Digest:sha256:c80d3b79c853ce41a993cfa76f1b5983dffeb08aceefaaf04d64c9dc2e177d8e
2. 通过导出、导入的方式部署基础镜像
#官方镜像的下载很慢,有时多次尝试才能下载。
方法一:
#一台已经下载的好的镜像的服务器上操作。
docker save centos > /opt/centos.tar #Docker导出镜像
docker load < /opt/centos.tar #Docker导入镜像
方法二:
docker export id > /opt/centos.tar #Docker导出镜像
cat /opt/centos.tar | docker import centos #Docker导入镜像
*注1:用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。
*注2:容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也要大。
此外,从容器快照文件导入时可以重新指定标签等元数据信息。
3. 查看下载的基础镜像
[root@docker1~]#dockerimages ----------------------------------------------------------------------------------------------------- REPOSITORYTAGIMAGEIDCREATEDSIZE docker.io/jdeathe/centos-sshlatest1d551d635e994daysago214MB docker.io/centoslatest8140d0c643102weeksago192.5MB
说明 :
REPOSITORY:来自于哪个仓库,比如 centos
TAG : 镜像的标记,一般修改版本号,latest为最后版本
IMAGE ID : 镜像的id号
CREATED : 创建镜像的时间
SIZE : 镜像的大小
4. 通过基础镜像创建一个容器
1)通过基础镜像centos创建一个容器名为centos_v1,并在后台执行
[root@docker1 ~]# docker run -it -d --name centos_v1 centos /bin/bash
0c1841e5d1115d35b80177ed5ea4fc5782e8a3ffc5f5c801979fb90ac5f26423
常用参数说明:
-i: 开启交互式shell
-t: 为容器分配一个伪tty终端
centos: 指定镜像的名字
/bin/bash: 运行/bin/bash
2) 查看容器
[root@docker1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" 7 seconds ago Up 4 seconds centos_v1
查看docker镜像的状态,-a表示列出所有的容器,STATUS如果为Exited为退出,UP为运行。
3)进入容器
[root@docker1 ~]# docker exec -it centos_v1 /bin/bash
提示符变成了0c1841e5d111,也是该容器的ID,也是该容器的主机名。
[root@0c1841e5d111 /]# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@0c1841e5d111 /]# hostname
0c1841e5d111
4)退出容器
[root@0c1841e5d111 /]# exit
exit
[root@docker1 ~]#
5. 查看容器状态
[root@docker1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" About an hour ago Up About an hour centos_v1
6. 关闭与启动容器
关闭容器
[root@docker1 ~]# docker stop centos_v1
centos_v1
[root@docker1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" About an hour ago Exited (137) 3 seconds ago centos_v1
说明:STATUS状态为EXIT表示容器是退出状态。
启动容器
[root@docker1 ~]# docker start centos_v1
centos_v1
[root@docker1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" About an hour ago Up 3 seconds centos_v1
[root@docker1 ~]#
[root@docker1 ~]#
7. 删除容器
[root@docker1 ~]# docker rm centos_v1 或 docker rm 0c1841e5d111
2)对于正在运行的容器,可以加-f参数强制进行删除。
[root@docker1 ~]# docker rm -f centos_v1
8. 通过一个容器创建镜像
说明:只能对运行中的容器提交本地镜像,关闭状态的容器无法提交本地镜像。
1)通过容器提交镜像
[root@docker1 ~]# docker commit centos_v1 centos:v1
sha256:66d2dc12144a87def3c16080553cca41d2a2965d9332f2a085dc23cc30366afa
[root@docker1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v1 66d2dc12144a 6 seconds ago 192.5 MB
docker.io/jdeathe/centos-ssh latest 1d551d635e99 4 days ago 214 MB
docker.io/centos latest 8140d0c64310 2 weeks ago 192.5 MB
[root@docker1 ~]# docker rmi centos:v1
或
[root@docker1 ~]# docker rmi 66d2dc12144a
Untagged: centos:v1
Deleted: sha256:66d2dc12144a87def3c16080553cca41d2a2965d9332f2a085dc23cc30366afa
Deleted: sha256:9c6100b12798ef72aeb16c13d3d8da8bef0a060d3162f068ea8730025e3508ea
9. 示例:创建一个提供apache服务器的容器
1)通过最基础镜像创建一个容器,用于制做
[root@docker1 ~]# docker run -it -d --name abc centos /bin/bash
[root@docker1 ~]# docker exec -it httpd-80 /bin/bash
# yum install net-tools httpd -y
2) 通过httpd-80容器制一个http本地镜像
[root@docker1 ~]# docker commit httpd-80 centos:apache
3) 通过centos:apache镜像可以创建很多个容器
参数说明:-p 80:80 映射本地端口到容器的对应端口上,可以配置多个端口。
[root@docker1~]#dockerrun-it-d-p8082:80--nameapache-8082centos:apache/usr/sbin/apachectl-DFOREGROUND 1fb934170108b66fcdfa7c77f6673d33aca44fac63ca0b61b35f06519c3ef242 [root@docker1~]#dockerrun-it-d-p8081:80--nameapache-8081centos:apache/usr/sbin/apachectl-DFOREGROUND cc4e7689db7bd5674f131782c470cf640594d298dbc88b680fc3caeb77bce256 [root@docker1~]#dockerrun-it-d-p8080:80--nameapache-8080centos:apache/usr/sbin/apachectl-DFOREGROUND da494401dd34f843dac50e67a1a1c3077b44f9faeadff3403e50196fe4ce5318 [root@docker1~]#dockerps-a CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES da494401dd34centos:apache"/usr/sbin/apachectl"5secondsagoUp4seconds0.0.0.0:8080->80/tcpapache-8080 cc4e7689db7bcentos:apache"/usr/sbin/apachectl"17secondsagoUp15seconds0.0.0.0:8081->80/tcpapache-8081 1fb934170108centos:apache"/usr/sbin/apachectl"26secondsagoUp25seconds0.0.0.0:8082->80/tcpapache-8082 f4b6bffcca1bcentos"/bin/bash"8minutesagoUp8minuteshttpd-80 [root@docker1~]#