1.docker一句话概括容器:容器就是将软件打包成标准化单元,以用于开发、交付和部署。
- 容器镜像是轻量的、可执行的独立软件包 ,包含软件运行所需的所有内容:代码、运行时环境、系统工具、系统库和设置。
- 容器化软件适用于基于 Linux 和 Windows 的应用,在任何环境中都能够始终如一地运行。
- 容器赋予了软件独立性,使其免受外在环境差异(例如,开发和预演环境的差异)的影响,从而有助于减少团队间在相同基础设施上运行不同软件时的冲突。
2.docker的基本组成:
- 镜像(Image)
- 容器(Container)
- 仓库(Repository)
3.docker安装
命令:
uname-r#查看系统内核版本cat/etc/os-release#查看系统版本
事例:
[root/]#uname-r4.18.0-193.14.2.el8_2.x86_64[root/]#cat/etc/os-releaseNAME="CentOS Linux"VERSION="8 (Core)"ID="centos"ID_LIKE="rhel fedora"VERSION_ID="8"PLATFORM_ID="platform:el8"PRETTY_NAME="CentOS Linux 8 (Core)"ANSI_COLOR="0;31"CPE_NAME="cpe:/o:centos:centos:8"HOME_URL="https://www.centos.org/"BUG_REPORT_URL="https://bugs.centos.org/"CENTOS_MANTISBT_PROJECT="CentOS-8"CENTOS_MANTISBT_PROJECT_VERSION="8"REDHAT_SUPPORT_PRODUCT="centos"REDHAT_SUPPORT_PRODUCT_VERSION="8"
正式开始安装docker:
命令:
yumremovedocker\docker-client\docker-client-latest\docker-common\docker-latest\docker-latest-logrotate\docker-logrotate\docker-engine
示例:
[root/]#yumremovedocker\>docker-client\>docker-client-latest\>docker-common\>docker-latest\>docker-latest-logrotate\>docker-logrotate\>docker-engineNomatchforargument: dockerNomatchforargument: docker-clientNomatchforargument: docker-client-latestNomatchforargument: docker-commonNomatchforargument: docker-latestNomatchforargument: docker-latest-logrotateNomatchforargument: docker-logrotateNomatchforargument: docker-engine没有软件包需要移除。依赖关系解决。无需任何处理。完毕!
下载依赖安装包
yuminstall-yyum-utils
配置镜像仓库
#国外的地址yum-config-manager\--add-repo\https://download.docker.com/linux/centos/docker-ce.repo #设置阿里云的Docker镜像仓库yum-config-manager\--add-repo\https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新yum软件包
yummakecachefast
下载docker
yuminstalldocker-cedocker-ce-clicontainerd.io#安装社区版yuminstalldocker-eedocker-ee-clicontainerd.io#安装企业版
一般情况下安装社区版
启动Docker
命令:
systemctlstartdocker#启动Dockerdockerversion#查看当前版本号,是否启动成功systemctlenabledocker#设置开机自启动
事例:
[root/]#systemctlstartdocker[root/]#dockerversionClient: DockerEngine-CommunityVersion: 20.10.7APIversion: 1.41Goversion: go1.13.15Gitcommit: f0df350Built: WedJun211:56:242021OS/Arch: linux/amd64Context: defaultExperimental: trueServer: DockerEngine-CommunityEngine: Version: 20.10.7APIversion: 1.41 (minimumversion1.12) Goversion: go1.13.15Gitcommit: b0f5bc3Built: WedJun211:54:482021OS/Arch: linux/amd64Experimental: falsecontainerd: Version: 1.4.9GitCommit: e25210fe30a0a703442421b0f60afac609f950a3runc: Version: 1.0.1GitCommit: v1.0.1-0-g4144b63docker-init: Version: 0.19.0GitCommit: de40ad0
Docker的HelloWorld
命令:
dockerrunhello-world
事例:
[root/]#dockerrunhello-worldUnabletofindimage'hello-world:latest'locally#本地没有latest: Pullingfromlibrary/hello-world#pull一个最新版b8dfde127a29: Pullcomplete#pull成功Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9eStatus: Downloadednewerimageforhello-world:latestHellofromDocker!#运行结果Thismessageshowsthatyourinstallationappearstobeworkingcorrectly. Togeneratethismessage, Dockertookthefollowingsteps: 1.TheDockerclientcontactedtheDockerdaemon. 2.TheDockerdaemonpulledthe"hello-world"imagefromtheDockerHub. (amd64) 3.TheDockerdaemoncreatedanewcontainerfromthatimagewhichrunstheexecutablethatproducestheoutputyouarecurrentlyreading. 4.TheDockerdaemonstreamedthatoutputtotheDockerclient, whichsentittoyourterminal. Totrysomethingmoreambitious, youcanrunanUbuntucontainerwith: $dockerrun-itubuntubashShareimages, automateworkflows, andmorewithafreeDockerID: https://hub.docker.com/Formoreexamplesandideas, visit: https://docs.docker.com/get-started/
Docker卸载
yumremovedocker-cedocker-ce-clicontainerd.io#卸载依赖rm-rf/var/lib/docker#删除资源 . /var/lib/docker是docker的默认工作路径
配置阿里云镜像
sudomkdir-p/etc/dockersudotee/etc/docker/daemon.json<<-'EOF'{ "registry-mirrors": ["https://axvfsf7e.mirror.aliyuncs.com"] } EOFsudosystemctldaemon-reloadsudosystemctlrestartdocker
基本命令:
dockerversion#查看docker的版本信息dockerinfo#查看docker的系统信息,包括镜像和容器的数量docker命令--help#帮助命令(可查看可选的参数) dockerCOMMAND--help
镜像命令
docker images 查看本地主机的所有镜像
docker search 搜索镜像
docker pull 镜像名[:tag] 下载镜像
docker rmi 删除镜像
#1.删除指定的镜像id[root~]#dockerrmi-f镜像id#2.删除多个镜像id[root~]#dockerrmi-f镜像id镜像id镜像id#3.删除全部的镜像id[root~]#dockerrmi-f$(dockerimages-aq)
容器命令
docker run [可选参数] image 运行容器
dockerrun [可选参数] image#参数说明--name="名字"指定容器名字-d后台方式运行-it使用交互方式运行,进入容器查看内容-p指定容器的端口( -pip:主机端口:容器端口配置主机端口映射到容器端口-p主机端口:容器端口-p容器端口) -P随机指定端口(大写的P)
进入容器
[root~]#dockerrun-it [容器ID] /bin/bash
docker ps列出容器
#dockerps#列出当前正在运行的容器-a#列出所有容器的运行记录-n=?#显示最近创建的n个容器-q#只显示容器的编号[root~]#dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES[root~]#dockerps-aCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMESbca129320bb5centos"/bin/bash"4minutesagoExited (0) 3minutesagooptimistic_shternbd1b8900c547centos"/bin/bash"6minutesagoExited (0) 5minutesagocool_teslacf6adbf1b506bf756fb1ae65"/hello"5hoursagoExited (0) 5hoursagooptimistic_darwin
删除容器
dockerrm容器id#删除指定的容器,不能删除正在运行的容器,强制删除使用rm-fdockerrm-f$(dockerps-aq) #删除所有的容器dockerps-a-q|xargsdockerrm#删除所有的容器
启动和重启容器命令
dockerstart容器id#启动容器dockerrestart容器id#重启容器dockerstop容器id#停止当前运行的容器dockerkill容器id#强制停止当前容器
其他命令
1 查看日志
[root~]#dockerlogs--helpUsage: dockerlogs [OPTIONS] CONTAINERFetchthelogsofacontainerOptions: --detailsShowextradetailsprovidedtologs-f, --followFollowlogoutput--sincestringShowlogssincetimestamp (e.g. 2013-01-02T13:23:37Z) orrelative (e.g. 42mfor42minutes) -n, --tailstringNumberoflinestoshowfromtheendofthelogs (default"all") -t, --timestampsShowtimestamps--untilstringShowlogsbeforeatimestamp (e.g. 2013-01-02T13:23:37Z) orrelative (e.g. 42mfor42minutes) 常用:dockerlogs-tf容器iddockerlogs--tailnumber容器id#num为要显示的日志条数#docker容器后台运行,必须要有一个前台的进程,否则会自动停止#编写shell脚本循环执行,使得centos容器保持运行状态[root~]#dockerrun-dcentos/bin/sh-c"while true;do echo hi;sleep 5;done"c703b5b1911ff84d584390263a35707b6024816e1f46542b61918a6327a570dc[root~]#dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMESc703b5b1911fcentos"/bin/sh -c 'while t…"13secondsagoUp10secondspedantic_banach[root~]#dockerlogs-tf--tail10c703b5b1911f2020-12-27T03:34:07.255599560Zhi2020-12-27T03:34:12.257641517Zhi2020-12-27T03:34:17.259706294Zhi2020-12-27T03:34:22.261693707Zhi2020-12-27T03:34:27.262609289Zhi2020-12-27T03:34:32.267862677Zhi2020-12-27T03:34:37.270382873Zhi2020-12-27T03:34:42.272414182Zhi2020-12-27T03:34:47.274823243Zhi2020-12-27T03:34:52.277419274Zhi
2 查看容器中进程信息
[root~]#dockertopc703b5b1911fUIDPIDPPIDCSTIMETTYTIMECMDroot1115611135011:31?00:00:00/bin/sh-cwhiletrue;doechohi;sleep5;doneroot1188611156011:43?00:00:00/usr/bin/coreutils--coreutils-prog-shebang=sleep/usr/bin/sleep5
3 查看容器的元数据
[root~]#dockerinspect容器id
4 进入当前正在运行的容器
方法一
[root~]#dockerexec-itc703b5b1911f/bin/bash[root/]#lsbindevetchomeliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvar[root/]#ps-efUIDPIDPPIDCSTIMETTYTIMECMDroot10003:31?00:00:00/bin/sh-cwhiletrue;doechohi;sleep5;doneroot2790003:54pts/000:00:00/bin/bashroot3151003:56?00:00:00/usr/bin/coreutils--coreutils-prog-shebang=sleep/usr/bin/sleep5root316279003:56pts/000:00:00ps-ef
方法二:
[root~]#dockerattachc703b5b1911f
5 拷贝容器文件到主机
dockercp容器id:容器内路径目的主机路径[root~]#dockerexec-itc703b5b1911f/bin/bash[root/]#cdhome[roothome]#ls#touch新建文件[roothome]#touchtest.java[roothome]#lstest.java[roothome]#exitexit[root~]#dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMESc703b5b1911fcentos"/bin/sh -c 'while t…"35minutesagoUp35minutespedantic_banach[root~]#dockercpc703b5b1911f:/home/test.java/home[root~]#ls/homehaipantest.java