Docker安装及基础

网友投稿 308 2022-10-23

Docker安装及基础

有一年多没发文章了,在提升自己的经历与技术,回头看看写过的文章。唉。。。。一言难尽!!!今天开始,接着会发一些技术行的文章,同样自己也是作为笔记,分享给大家!今天这个文章开始,发一些有关于Docker的安装及使用,到会玩Docker。.

Docker概述

Docker是一种运行与Linux和Windows上的软件,用于创建、管理和编排容器 Docker是在GitHub上开发的Moby 开源项目的一部分 Docker 公司,位于旧金山,是整个 Moby 开源项目的维护者。Docker 公司还提供包含支持服务的商业版本的 Docker

Docker公司有意思的是,Docker 公司起初是一家名为 dotCloud 的平台即服务(Platform-as-a-Service, PaaS)提供商。底层技术上,dotCloud 平台利用了 Linux 容器技术。为了方便创建和管理这些容器,dotCloud 开发了一套内部工具,之后被命名为“Docker”。Docker就是这样诞生的!2013年,dotCloud 的 PaaS 业务并不景气,公司需要寻求新的突破。于是他们聘请了 Ben Golub 作为新的 CEO,将公司重命名为“Docker”,放弃dotCloud PaaS 平台,怀揣着“将 Docker 和容器技术推向全世界”的使命,开启了一段新的征程。如今 Docker 公司被普遍认为是一家创新型科技公司,据说其市场价值约为 10 亿美元。Docker 公司已经通过多轮融资,吸纳了来自硅谷的几家风投公司的累计超过 2.4 亿美元的投资。几乎所有的融资都发生在公司更名为“Docker”之后。提示:“Docker”一词来自英国口语,意为码头工人(Dock Worker),即从船上装卸货物的人。.

docker是基于Go语言开发的 docker官网: docker的文档是非常详细的仓库地址:

docker的思想来源于集装箱本质:所有的技术都是因为出现问题,我们需要解决,才去学习!.

Docker能干什么

\虚拟机技术缺点:

占用资源多 冗余步骤多 启动速度慢

容器化技术:容器化技术不是模拟一个完整的操作系统\Docker和虚拟机的区别

传统虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上运行和安装软件。 容器内的应用,直接运行在宿主机上,容器是没有自己的内核的,也没有虚拟硬件,所以更轻便。 每个容器都是相互隔离的,每个容器内部都有属于自己文件系统,互不影响。\DevOps开发、运维)应用更快速的交付和部署传统:一堆帮助文档,安装程序Docker :打包说像发布测试,一键运行更便捷的升级和扩编容使用了Docker之后,我们部署应用就和搭积木一样!项目打包为一个挽像,扩展服务器A!服务器B更简单的系统运维在容器化之后我们的开发, 测试环境都是高度一致的。更高效的计算资源利用:Docker是内核级别的虚拟化,可以在一个物理机上运行很多个容器实例!服务器的性能可以压榨(利用)到极致。

Docker中的名词概述

.

镜像(images)Docker镜像就好比模板,可以通过这个模板来创建一个容器,tomcat镜像----run----容器(服务),一个镜像可以启动多个容器(最终服务运行和项目运行都运行在容器里)容器(Container)docker利用容器技术,运行一个或一组应用,通过镜像来创建的。仓库(repository)仓库就是存放镜像的,分为私有仓库、公有仓库,docker hub(默认都是国外的)国内仓库阿里云等。。。.

Docker安装

安装环境centos7\查看环境

#系统内核3.10 [root@XiangGuo ~]# uname -r 3.10.0-1160.el7.x86_64

#系统版本 [root@XiangGuo ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"

安装

帮助文档

1.##卸载旧版本 [root@XiangGuo ~]# yum remove docker \ > docker-client \ > docker-client-latest \ > docker-common \ > docker-latest \ > docker-latest-logrotate \ > docker-logrotate \ > docker-engine 已加载插件:fastestmirror 参数 docker 没有匹配 参数 docker-client 没有匹配 参数 docker-client-latest 没有匹配 参数 docker-common 没有匹配 参数 docker-latest 没有匹配 参数 docker-latest-logrotate 没有匹配 参数 docker-logrotate 没有匹配 参数 docker-engine 没有匹配 不删除任何软件包 ###新准备的机器没有docker 2.需要的安装包 [root@XiangGuo ~]# yum -y install yum-utils 3.设置镜像仓库 ##默认是国外公有仓库地址 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo ##配置国内阿里云镜像仓库 [root@XiangGuo ~]# yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 更新yum索引包 yum makecache fast 4.安装docker相关的 yum install docker-ce docker-ce-cli containerd.io 5.启动docker systemctl start docker 6.使用docker version测试docker是否启动成功

7.测试hello-hello-world docker run hello-world

查看镜像是否存在 docker images

卸载docker yum remove docker-ce docker-ce-cli containerd.io (卸载依赖) rm -rf /var/lib/docker (删除资源,docker的默认工作路径) rm -rf /var/lib/containerd 配置阿里镜像加速器 登录阿里云找到镜像加速器

配置阿里镜像加速器

配置使用加速器 mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://qj3fi97b.mirror.aliyuncs.com"] } EOF systemctl daemon-reload systemctl restart docker

回顾hello-world

底层原理

Docker的常用命令

帮助命令

docker version #显示docker版本信息 docker info #显示docker的系统信息,包括镜像容器的数量 docker 命令 --help #帮助命令

< #命令文档.

镜像命令

docker images 查看所有本地主机上的镜像

[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d1165f221234 2 weeks ago 13.3kB ###解释 REPOSITORY 镜像的仓库源 TAG 镜像的标签 IMAGE ID 镜像的ID CREATED 镜像创建时间 SIZE 镜像的大小 [root@localhost ~]# docker images --help Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show image IDs ###可选项 -a, --all #列出所有的镜像 -q, --quiet #只显示镜像的ID

docker search 搜索镜像

[root@localhost ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10665 [OK] mariadb MariaDB Server is a high performing open sou… 3997 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 779 [OK] percona Percona Server is a fork of the MySQL relati… 528 [OK] ###可选项,根据收藏来过滤 [root@localhost ~]# docker search --help Usage: docker search [OPTIONS] TERM Search the Docker Hub for images Options: -f, --filter filter Filter output based on conditions provided --format string Pretty-print search using a Go template --limit int Max number of search results (default 25) --no-trunc Don't truncate output --filter=STARS=3000 #搜索镜像收藏度大于3000的 [root@localhost ~]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10665 [OK] mariadb MariaDB Server is a high performing open sou… 3997 [OK]

docker pull 下载镜像

###下载镜像 docker pull 镜像名[:ta] [root@localhost ~]# docker pull mysql Using default tag: latest #如果不写版本,默认会下载latest latest: Pulling from library/mysql a076a628af6f: Pull complete #分成下载,docker images的核心 联合文件系统 f6c208f3f991: Pull complete 88a9455a9165: Pull complete 406c9b8427c6: Pull complete 7c88599c0b25: Pull complete 25b5c6debdaf: Pull complete 43a5816f1617: Pull complete 1a8c919e89bf: Pull complete 9f3cf4bd1a07: Pull complete 80539cea118d: Pull complete 201b3cad54ce: Pull complete 944ba37e1c06: Pull complete Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c #签名 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #真实地址 ###等价于他 docker pull mysql docker pull docker.io/library/mysql:latest ###指定版本下载 [root@localhost ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql a076a628af6f: Already exists f6c208f3f991: Already exists 88a9455a9165: Already exists 406c9b8427c6: Already exists 7c88599c0b25: Already exists 25b5c6debdaf: Already exists 43a5816f1617: Already exists 1831ac1245f4: Pull complete 37677b8c1f79: Pull complete 27e4ac3b0f6e: Pull complete 7227baa8c445: Pull complete Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7

docker rmi 删除镜像

[root@localhost ~]# docker rmi -f 容器ID #删除指定镜像 [root@localhost ~]# docker rmi -f 容器ID 容器ID 容器ID 容器ID #删除多个镜像 [root@localhost ~]# docker rmi -f $(docker images -aq) #删除全部的镜像

容器命令

说明:有了镜像才可以创建容器,Linux,下载一个centos镜像

[root@localhost ~]# 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

新建容器并去启动

docker run [可选参数] images #参数说明 --name="Name" 容器的名字 比如 tomcat1、tomcat2 -d 后台运行 -it 使用交互运行容器,进入容器查看内容 -p(小) 指定容器的端口。8080:8080 -p ip:主机端口:容器端口 -p 主机端口:容器端口(常用) -p 容器端口 -P(大) 随机指定端口 #测试 (启动并进入容器) [root@localhost ~]# docker run -it centos /bin/bash [root@463ca4471b4a /]# ls #查看容器的内容 bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var #退出容器命令 [root@463ca4471b4a /]# exit exit [root@localhost ~]# [root@localhost ~]# ls / bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

列出运行中的容器

#docker ps 命令 docker ps #列出当前运行的容器 docker ps -a #列出当前运行的容器+历史运行过的容器 docker ps -n=? #列出最近创建的容器 docker ps -q #只显示容器的ID [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 463ca4471b4a centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago musing_booth 5d2dd4a1dc09 d1165f221234 "/hello" 4 hours ago Exited (0) 4 hours ago cranky_dubinsky [root@localhost ~]# docker ps -a -n=1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 463ca4471b4a centos "/bin/bash" 7 minutes ago Exited (0) 6 minutes ago musing_booth [root@localhost ~]# docker ps -aq 463ca4471b4a 5d2dd4a1dc09

退出容器

exit #直接容器停止退出 Ctrl+P+Q #容器不停止退出

删除容器

docker rm 容器ID #删除指定容器,不能删除正在运行的容器。 docker rm -f $(docker ps -ap) #删除所有容器 docker rm -a -q | xargs docker rm #删除所有容器

启动和停止容器

docker start 容器ID #启动容器 docker restart 容器ID #重启容器 docker stop 容器ID #停止容器 docker kill 容器ID #强制停止容器

Docker的常用其他命令

后台启动容器

#命令 docker run -d 启动容器 [root@localhost ~]# docker run -d centos 584914023d7dc2550f2c69dd4ac193a36648985d25736cab194bb9d3e2089ad9 #问题:docker ps 发现centos启动 之后就停止了 #docker容器使用后台运行,就必须要有一个前台的的进程,dicker发现没有应用,就会自动停止 #nginx,docker启动后,发现自己没有提供服务,机会立刻停止,就没有程序了

查看日志

docker log -t -f --tail 容器,没有日志 #编写一段shell [root@localhost ~]# docker run -d centos /bin/sh -c "while true; do echo xiangguo;sleep 1;done" #启动容器,输入shell ,循环 true 输出xiangguo 每隔一秒输出一次。 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2bd68a163659 centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds mystifying_golick #显示日志 -tf #f显示日志,t时间戳 -f会显示新的日志 --tail uamber #显示日志的条数 -n #查看日志 [root@localhost ~]# docker logs -tf --tail 10 容器ID #查看十条容器日志, 2021-03-29T02:50:30.966605133Z xiangguo 2021-03-29T02:50:31.977875749Z xiangguo 2021-03-29T02:50:32.987759325Z xiangguo 2021-03-29T02:50:34.000598526Z xiangguo 2021-03-29T02:50:35.013745798Z xiangguo 2021-03-29T02:50:36.026779729Z xiangguo 2021-03-29T02:50:37.040159241Z xiangguo 2021-03-29T02:50:38.051079698Z xiangguo 2021-03-29T02:50:39.066190840Z xiangguo 2021-03-29T02:50:40.082803927Z xiangguo 2021-03-29T02:50:41.094551377Z xiangguo 2021-03-29T02:50:42.101716945Z xiangguo 2021-03-29T02:50:43.109167939Z xiangguo 2021-03-29T02:50:44.118610024Z xiangguo

查看容器中的进程信息

#docker top 容器ID [root@localhost ~]# docker top 2bd68a163659 UID PID PPID C STIME TTY TIME CMD root 2257 2237 0 10:48 ? 00:00:00 /bin/sh -c while true; do echo xiangguo;sleep 1;done

查看镜像的元数据

#命令 docker inspect 容器ID #测试 [root@localhost ~]# docker inspect 2bd68a163659 [ { "Id": "2bd68a163659abab5edd8336403c600b5ced4bb178c689c5b0f08ba207c218c3", "Created": "2021-03-29T02:48:51.900853778Z", "Path": "/bin/sh", "Args": [ "-c", "while true; do echo xiangguo;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2257, "ExitCode": 0, "Error": "", "StartedAt": "2021-03-29T02:48:52.764747872Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", "ResolvConfPath": "/var/lib/docker/containers/2bd68a163659abab5edd8336403c600b5ced4bb178c689c5b0f08ba207c218c3/resolv.conf", "HostnamePath": "/var/lib/docker/containers/2bd68a163659abab5edd8336403c600b5ced4bb178c689c5b0f08ba207c218c3/hostname", "HostsPath": "/var/lib/docker/containers/2bd68a163659abab5edd8336403c600b5ced4bb178c689c5b0f08ba207c218c3/hosts", "LogPath": "/var/lib/docker/containers/2bd68a163659abab5edd8336403c600b5ced4bb178c689c5b0f08ba207c218c3/2bd68a163659abab5edd8336403c600b5ced4bb178c689c5b0f08ba207c218c3-json.log", "Name": "/mystifying_golick", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/6d26d160d81bee7ad7ea8f45aeb4b5a95acef50c8dffdb531e84b6569b8e3bd8-init/diff:/var/lib/docker/overlay2/872fc4eda8a5db644c3b18799100457e71277274b2902766f1975b70bd7cee96/diff", "MergedDir": "/var/lib/docker/overlay2/6d26d160d81bee7ad7ea8f45aeb4b5a95acef50c8dffdb531e84b6569b8e3bd8/merged", "UpperDir": "/var/lib/docker/overlay2/6d26d160d81bee7ad7ea8f45aeb4b5a95acef50c8dffdb531e84b6569b8e3bd8/diff", "WorkDir": "/var/lib/docker/overlay2/6d26d160d81bee7ad7ea8f45aeb4b5a95acef50c8dffdb531e84b6569b8e3bd8/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "2bd68a163659", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "while true; do echo xiangguo;sleep 1;done" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201204", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "a5e7cbf01268a113b43d8f13d34fd4ed4cf0bdbf0fd0e39c0612c1e16034d6ab", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/a5e7cbf01268", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "16947d1bb341350e026d0a2ae70c1f0b9c15a76df153a76bb11453fb86c65926", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "57ec418726492e2fc2b4ab0c0b27a7f2ba3fa065586de0715c3c36683418c973", "EndpointID": "16947d1bb341350e026d0a2ae70c1f0b9c15a76df153a76bb11453fb86c65926", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ]

进入当前正在运行的容器

#通常是后台运行容器,有时需要进入容器 修配置 #命令 docker exec -it 容器ID bashshell #测试 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2bd68a163659 centos "/bin/sh -c 'while t…" 21 minutes ago Up 21 minutes mystifying_golick [root@localhost ~]# docker exec -it 2bd68a163659 /bin/bash [root@2bd68a163659 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2bd68a163659 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 02:48 ? 00:00:01 /bin/sh -c while true; do echo xiangguo;sleep 1;done root 1306 0 0 03:10 pts/0 00:00:00 /bin/bash root 1488 1 0 03:13 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1 root 1489 1306 0 03:13 pts/0 00:00:00 ps -ef 、 #方式二 docker inspect 容器ID #测试 [root@localhost ~]# docker inspect 2bd68a163659 正在执行当前的代码 、 #docker exec #进入容器后开启一个新的终端,可以在里面操作(常用) #docker attach #进入容器正在执行的终端,不会启动新的进程

从机器内拷贝文件到主机

#命令 docker cp 容器ID:文件路径 主机目的目录 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9285d5fb3e06 centos "/bin/bash" 7 minutes ago Up 3 seconds zealous_wozniak [root@localhost ~]# docker attach 9285d5fb3e06 #连接容器 [root@9285d5fb3e06 /]# touch /home/xiangguo.txt #在容器home目录下创建文件 [root@9285d5fb3e06 /]# ls /home/ xiangguo.txt [root@9285d5fb3e06 /]# exit [root@localhost ~]# cd /home/ [root@localhost home]# ls #查看宿主机home目录为空 [root@localhost home]# docker cp 9285d5fb3e06:/home/xiangguo.txt /home/ #从容器内部拷贝到宿主机 [root@localhost home]# ls xiangguo.txt #拷贝是一个手动的过程,后续会使用-v 卷的技术,可以实现自动同步。

.

小结

attach Attach to a running container #当前shell下attach连接指定运行镜像 build Build an image from a Dockerfile # 通过Dockerfile定制镜像 commit create a new image from a container changes #提交当前让其问新的镜像 cp Copy files/folders from yhe containers filesystem to the host path #从容器钟拷贝文件或目录到宿主机 create Create a new container 创建一个新的容器,同run,但不启动容器 diff Inspect changes on a containers filesystem #查看docker的变化 events Get real time evens from the server #从docker服务获取容器实时事件 exec Run a command in an existing container #在已存在的容器上运行命令 export Stream the contents of a container as a tar archive #导出容器的内容作为一个tar归档文件【对应import】 history Show the history of an image #展示一个镜像形成历史 images List images #列出系统当前镜像 import Create a new filesystem image from the contents of a tarball #从tar包钟的内容创建一个新的文件系统映像【对应export】 info Display system-wide information #显示系统相关信息 inspect Return 1ow-1evel information on a container #查看容器详细信息 kill Kill a running container #kill 指定docker容器 load Load an image from a tar archive #从一个tar包中加载一个镜像【对应 save】 login Register or Login to the docker registry server #注册或登录一个docker源服务器 logout Log out from a Docker registry server #当前docker registry 退出 logs Fetch the logs of a container #输出当前容器日志信息 port Lookup the public-facing port which is NAT-ed to PRIVATE_ PORT #查看映射端口对应的容器内部源 、 pause Pause all processes within a container #暂停容器 ps List containers #列出容器列表 pull pull an image or a repository from the docker registry server #从docker镜像源服务器拉去指定镜像或者库镜像 push Push an image or a repository to the docker registry server #推送指定镜像或库镜像至docekr源服务器 restart Restart a running container #重启运行的容器 rm Rmove one or more containers #移除一个或多个容器 rmi Rmove one or more images #移除一个或多个镜像,【无容器使用该镜像,才可以删除,否则需删除相关容器才可继续或-f 强制删除】 run Run a command in a new container #创建一个新的容器并运行一个命令 save Save an image to a tar archive #保存一个镜像为一个tar包【对应lod】 search Search for an image on the Docker Hub #在docker hub 中搜索镜像 start Start a stopped containers #启动容器 stop Stop a running containers #停止容器 tag Tag an image into a repository #给源中镜像打标签 top Lookup the running processes of a container #查看容器中运行的进程信息 unpause unpause a paused container #取消暂停容器 version Show the docker version informtion #查看docker版本 wait Block until a container stops, then print its exit code #截取容器停止时的退出状态值

安装及基础命令都了解了,下面安装两个服务搞一下

安装Nginx

1.查找镜像 search (建议在docker hub 查找) 2.下载镜像 3.启动镜像 [root@localhost ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a076a628af6f: Pull complete 0732ab25fa22: Pull complete d7f36f6fe38f: Pull complete f72584a26f32: Pull complete 7125e4df9063: Pull complete Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f6d0b4767a6c 2 months ago 133MB centos latest 300e315adb2f 3 months ago 209MB -d #后台运行 --name #命名 -p #映射端口,宿主机的80:容器的80 [root@localhost ~]# docker run -d --name nginx01 -p 80:80 nginx bd3af5c9cbca712f50da5354ba772cb1954c5d1f41fb02fa847602cdf419f05f [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd3af5c9cbca nginx "/docker-entrypoint.…" 5 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp nginx01 4.测试 [root@localhost ~]# curl http://localhost:80 Welcome to nginx! Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

. ###进入容器 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 98836b5d5855 tomcat "/bin/bash" 32 minutes ago Up 32 minutes 0.0.0.0:8080->8080/tcp tomcat01 7d3947cfae06 centos "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:81->80/tcp centos01 bd3af5c9cbca nginx "/docker-entrypoint.…" 7 days ago Up 4 hours 0.0.0.0:80->80/tcp nginx01 nginx:[root@localhost ~]# docker exec -it bd3af5c9cbca /bin/bash root@bd3af5c9cbca:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx

端口暴露的概念

安装Tomcat

###官方使用方法 docker run -it --rm tomcat:9.0 #之前的启动都是后台,停止容器之后,容器还是可以查到 docker run -it --rm ,一般用来测试,用完就删除 . . . #下载镜像 [root@localhost ~]# docker pull tomcat #启动 [root@localhost ~]# docker run -d -p 8080:8080 --name tomcat01 tomcat ee3ffd6fc7481ce8903877a80aea3babc25b7e0d9e3b6fe323569c432b6c65ac #测试

#进入容器 [root@localhost ~]# docker exec -it ee3ffd6fc748 /bin/bash root@ee3ffd6fc748:/usr/local/tomcat# . #发现问题 1.Linux命令少了。2.没有webapps。 阿里云镜像的原因,默认是最小的镜像,所有不必要的都剔除掉。 保证最小可以运行的环境。 root@ee3ffd6fc748:/usr/local/tomcat# ls -al total 128 drwxr-xr-x 1 root root 30 Jan 13 08:25 . drwxr-xr-x 1 root root 20 Jan 13 08:19 .. -rw-r--r-- 1 root root 18982 Dec 3 11:48 BUILDING.txt -rw-r--r-- 1 root root 5409 Dec 3 11:48 CONTRIBUTING.md -rw-r--r-- 1 root root 57092 Dec 3 11:48 LICENSE -rw-r--r-- 1 root root 2333 Dec 3 11:48 NOTICE -rw-r--r-- 1 root root 3257 Dec 3 11:48 README.md -rw-r--r-- 1 root root 6898 Dec 3 11:48 RELEASE-NOTES -rw-r--r-- 1 root root 16507 Dec 3 11:48 RUNNING.txt drwxr-xr-x 2 root root 4096 Jan 13 08:25 bin drwxr-xr-x 1 root root 22 Apr 7 07:06 conf drwxr-xr-x 2 root root 4096 Jan 13 08:25 lib drwxrwxrwx 1 root root 177 Apr 7 07:06 logs drwxr-xr-x 2 root root 134 Jan 13 08:25 native-jni-lib drwxrwxrwx 2 root root 30 Jan 13 08:25 temp drwxr-xr-x 2 root root 6 Jan 13 08:25 webapps drwxr-xr-x 7 root root 81 Dec 3 11:45 webapps.dist drwxrwxrwx 2 root root 6 Dec 3 11:43 work root@ee3ffd6fc748:/usr/local/tomcat# ls webapps root@ee3ffd6fc748:/usr/local/tomcat# ls webapps.dist/ ROOT docs examples host-manager manager root@ee3ffd6fc748:/usr/local/tomcat# cp -r webapps.dist/* webapps root@ee3ffd6fc748:/usr/local/tomcat# ls webapps ROOT docs examples host-manager manager

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:RGMII接口信号和帧格式_RGMII接口应用框图
下一篇:java集合Collection实现类解析ArrayList LinkedList及Vector
相关文章

 发表评论

暂时没有评论,来抢沙发吧~