(个人理解)有三台主机分别是docker客户端,docker host,doker 仓库,其中docker客户端的主机在docker host上面,更符合docker实现功能docker客户端命令docker build(b you d)构建docker pull(po)拉取docker run(rua)运行 当我使用docker命令运行后,会到docker-host,交给主机docker dameon(di meng)容器守护进程,寻找本地镜像有没有镜像,没有去docker仓库去拉取,来启动容器。有的话直接,使用本地镜像启动容器,此过程只需要几秒。
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
(个人理解)registry(rua que s zui)镜像仓库,放了很多镜像,比例说有nginx ,httpd,whidows,centos各种各样独立的镜像。镜像是静态的,容器是动态的。容器可能会挂掉,有使用周期,容器与镜像的关系类似于,执行文件和执行文件执行后状态。镜像可以一直保持不变,但是容器不可能保持不变他会产生日志等数据。
When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.
当你使用docker时,你正在创建和使用图像、容器、网络、卷、插件和其他对象。
IMAGES
图片
An image is a read-only template with instructions for creating a docker container.
映像是一个只读模板,它带有创建docker容器的指令。
Often, an image is based on another image, with some additional customization.
通常,一个映像基于另一个映像,并具有一些额外的自定义。
You might create your own images or you might only use those created by others and published in a registry.
您可以创建自己的映像,也可以只使用其他人创建并在注册表中发布的映像。
CONTAINERS
容器
A conntainer is a runnable instance of an image.
容器是映像的可运行实例。
You can create, run, stop, move, or delete a container using the docker API or CLI.
您可以通过docker API或CLI创建、运行、停止、移动或删除容器。
You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
您可以将容器连接到一个或多个网络,将存储连接到它,甚至根据它的当前状态创建一个新映像。
| 主机名 | IP地址 | 部署功能 |
|---|---|---|
| root@102 | 192.168.143.102 | docker |
[root@102 ~]# cd /etc/yum.repos.d/ [root@102 yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo [root@102 yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo [root@102 yum.repos.d]# ls CentOS-Linux-AppStream.repo CentOS-Linux-baseOS.repo CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Debuginfo.repo CentOS-Linux-Devel.repo CentOS-Linux-Extras.repo CentOS-Linux-FastTrack.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-Media.repo CentOS-Linux-Plus.repo CentOS-Linux-PowerTools.repo CentOS-Linux-Sources.repo docker-ce.repo [root@102 yum.repos.d]# yum clean all 21 文件已删除 [root@102 yum.repos.d]# yum makecache CentOS Linux 8 - AppStream 478 kB/s | 8.1 MB 00:17 CentOS Linux 8 - baseOS 1.5 MB/s | 3.5 MB 00:02 CentOS Linux 8 - Extras 12 kB/s | 10 kB 00:00 Docker CE Stable - x86_64 4.7 kB/s | 19 kB 00:03 元数据缓存已建立。 [root@102 yum.repos.d]# yum -y install docker-ce [root@102 yum.repos.d]# systemctl enable --now docker Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service. //docker版本 [root@102 ~]# docker version Client: Docker Engine - Community Version: 20.10.11 API version: 1.41 Go version: go1.16.9 Git commit: dea9396 Built: Thu Nov 18 00:36:58 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.11 API version: 1.41 (minimum version 1.12) Go version: go1.16.9 Git commit: 847da18 Built: Thu Nov 18 00:35:20 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.12 GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc: Version: 1.0.2 GitCommit: v1.0.2-0-g52b36a2 docker-init: Version: 0.19.0 GitCommit: de40ad0 //[root@102 ~]# docker version Client: Docker Engine - Community Version: 20.10.11 API version: 1.41 Go version: go1.16.9 Git commit: dea9396 Built: Thu Nov 18 00:36:58 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.11 API version: 1.41 (minimum version 1.12) Go version: go1.16.9 Git commit: 847da18 Built: Thu Nov 18 00:35:20 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.12 GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc: Version: 1.0.2 GitCommit: v1.0.2-0-g52b36a2 docker-init: Version: 0.19.0 GitCommit: de40ad0 //docker信息 [root@102 ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.6.3-docker) scan: Docker Scan (Docker Inc., v0.9.0) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 20.10.11 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc Default Runtime: runc Init Binary: docker-init containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc version: v1.0.2-0-g52b36a2 init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.18.0-305.3.1.el8.x86_64 Operating System: CentOS Linux 8 OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 3.623GiB Name: 102 ID: 46EB:YSAB:QFVU:DAE5:JCY3:5K7N:IIUT:NX66:EY3U:XCYN:D5OW:F3N2 Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: Live Restore Enabled: falsedocker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
-
docker cn
-
中国科技大学加速器
-
阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器
systemctl start docker //清华大学加速器 cat > /etc/docker/daemon.json <
//阿里云私有加速器配置(前提私有账号)
访问官方网站https://www.aliyun.com/
[root@102 ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
[root@102 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
[root@102 ~]# systemctl daemon-reload
[root@102 ~]# systemctl restart docker
docker常用操作(注加粗表示使用率很高)
| 命令 | 功能 |
|---|---|
| docker search | 在Docker Hub中搜索镜像 |
| docker pull | 从注册表中拉出映像或存储库 |
| docker images | 列出镜像 |
| docker create | 创建一个新的容器 |
| docker start | 启动一个或多个停止的容器 |
| docker run | 在新容器中运行命令 |
| docker attach | 附加到一个运行的容器 |
| docker ps | 获取容器的ps进程 |
| docker logs | 获取容器的日志 |
| docker restart | 重启容器 |
| docker stop | 停止容器 |
| docker kill | 杀死正在运行的容器 |
| docker rm | 删除未运行的容器 |
| docker exec | 在容器中运行命令 |
| docker info | 在容器中显示信息 |
| docker inspect | 返回Docker对象的低级信息 |
//实例
//docker search xxx [root@102 ~]# docker search nginx NAME DEscriptION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 15899 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2098 [OK] richarvey/nginx-php-fpm Container ...省略 // docker pull xxx [root@102 ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx eff15d958d66: Pull complete 1e5351450a59: Pull complete 2df63e6ce2be: Pull complete 9171c7ae368c: Pull complete 020f975acd28: Pull complete 266f639b35ad: Pull complete Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@102 ~]# docker pull busybox Using default tag: latest latest: Pulling from library/busybox 3aab638df1a9: Pull complete Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1 Status: Downloaded newer image for busybox:latest docker.io/library/busybox:latest //docker images [root@102 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest d23834f29b38 29 hours ago 1.24MB nginx latest ea335eea17ab 13 days ago 141MB //docker create xxx [root@102 ~]# docker create busybox 15fc463740241e461d455f7d7928985e170120a08b63b080a537c6ad4359eb52 [root@102 ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.6.3-docker) scan: Docker Scan (Docker Inc., v0.9.0) Server: Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 2 ......省略 //docker ps docker ps -a [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@102 ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15fc46374024 busybox "sh" 2 minutes ago Created elegant_carson //docker start xxx #因为类型是sh,执行完成直接停止 [root@102 ~]# docker start 15fc46374024 15fc46374024 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #创建新的nginx容器 [root@102 ~]# docker create nginx a5e4676a32f3e439aaaa2b9e4324576893eeb153f9c21297a4fb487707b2099e [root@102 ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a5e4676a32f3 nginx "/docker-entrypoint.…" 5 seconds ago Created thirsty_nobel 15fc46374024 busybox "sh" 7 minutes ago Exited (0) 3 minutes ago elegant_carson [root@102 ~]# docker start a5e4676a32f3 a5e4676a32f3 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a5e4676a32f3 nginx "/docker-entrypoint.…" 34 seconds ago Up 14 seconds 80/tcp thirsty_nobel #创建新的busybox 加命令 [root@102 ~]# docker create busybox sleep 9999 de62fd2ae074328d6c92832ef725454fa0d32d7b4acf81917b124b6fc9716e02 [root@102 ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de62fd2ae074 busybox "sleep 9999" 13 seconds ago Created objective_buck a5e4676a32f3 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp thirsty_nobel 15fc46374024 busybox "sh" 9 minutes ago Exited (0) 6 minutes ago elegant_carson [root@102 ~]# docker start de62fd2ae074 de62fd2ae074 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de62fd2ae074 busybox "sleep 9999" 32 seconds ago Up 3 seconds objective_buck a5e4676a32f3 nginx "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 80/tcp thirsty_nobel //docker restart xxxxx [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de62fd2ae074 busybox "sleep 9999" 2 minutes ago Up 2 minutes objective_buck a5e4676a32f3 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp thirsty_nobel [root@102 ~]# docker restart a5e4676a32f3 a5e4676a32f3 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de62fd2ae074 busybox "sleep 9999" 3 minutes ago Up 2 minutes objective_buck a5e4676a32f3 nginx "/docker-entrypoint.…" 5 minutes ago Up 4 seconds 80/tcp thirsty_nobel //docker stop kill xxxx [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES de62fd2ae074 busybox "sleep 9999" 5 minutes ago Up 4 minutes objective_buck a5e4676a32f3 nginx "/docker-entrypoint.…" 7 minutes ago Up about a minute 80/tcp thirsty_nobel [root@102 ~]# docker stop de62fd2ae074 de62fd2ae074 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a5e4676a32f3 nginx "/docker-entrypoint.…" 8 minutes ago Up 2 minutes 80/tcp thirsty_nobel [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a5e4676a32f3 nginx "/docker-entrypoint.…" 8 minutes ago Up 2 minutes 80/tcp thirsty_nobel [root@102 ~]# docker kill a5e4676a32f3 a5e4676a32f3 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES //docker run [选项] [容器名] [命令] [root@102 ~]# docker run -it busybox /bin/sh / # ls bin dev etc home proc root sys tmp usr var / # ip a 1: lo:mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 12: eth0@if13: mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever / # exit [root@102 ~]# docker run -d httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd eff15d958d66: Already exists ba1caf8ba86c: Pull complete ab86dc02235d: Pull complete 0d58b11d2867: Pull complete e88da7cb925c: Pull complete Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02 Status: Downloaded newer image for httpd:latest ab0f14526973a73065bb487964ef97584ab3fb35e159631664a3ce92a7842de8 [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 59 seconds ago Up 57 seconds 80/tcp focused_wu [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 59 seconds ago Up 57 seconds 80/tcp focused_wu [root@102 ~]# docker inspect ab0f14526973 ........省略 "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2",//容器IP "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@102 ~]# curl 172.17.0.2 It works! //docker logs xxxx [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 5 minutes ago Up 5 minutes 80/tcp focused_wu [root@102 ~]# docker logs ab0f14526973 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Wed Dec 01 09:14:57.307204 2021] [mpm_event:notice] [pid 1:tid 140493502614848] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations [Wed Dec 01 09:14:57.307392 2021] [core:notice] [pid 1:tid 140493502614848] AH00094: Command line: 'httpd -D FOREGROUND' 172.17.0.1 - - [01/Dec/2021:09:18:59 +0000] "GET / HTTP/1.1" 200 45 [root@102 ~]# curl 172.17.0.2 It works! [root@102 ~]# curl 172.17.0.2 It works! [root@102 ~]# docker logs ab0f14526973 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Wed Dec 01 09:14:57.307204 2021] [mpm_event:notice] [pid 1:tid 140493502614848] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations [Wed Dec 01 09:14:57.307392 2021] [core:notice] [pid 1:tid 140493502614848] AH00094: Command line: 'httpd -D FOREGROUND' 172.17.0.1 - - [01/Dec/2021:09:18:59 +0000] "GET / HTTP/1.1" 200 45 172.17.0.1 - - [01/Dec/2021:09:20:27 +0000] "GET / HTTP/1.1" 200 45 172.17.0.1 - - [01/Dec/2021:09:20:29 +0000] "GET / HTTP/1.1" 200 45 //docker rm xxx [root@102 ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 7 minutes ago Up 7 minutes 80/tcp focused_wu bd9c8b8ce6f8 busybox "/bin/sh" 9 minutes ago Exited (0) 9 minutes ago elegant_turing de62fd2ae074 busybox "sleep 9999" 18 minutes ago Exited (137) 13 minutes ago objective_buck a5e4676a32f3 nginx "/docker-entrypoint.…" 21 minutes ago Exited (137) 11 minutes ago thirsty_nobel 15fc46374024 busybox "sh" 28 minutes ago Exited (0) 24 minutes ago elegant_carson [root@102 ~]# docker ps -aq ab0f14526973 bd9c8b8ce6f8 de62fd2ae074 a5e4676a32f3 15fc46374024 [root@102 ~]# docker rm $(docker ps -aq) bd9c8b8ce6f8 de62fd2ae074 a5e4676a32f3 15fc46374024 Error response from daemon: You cannot remove a running container ab0f14526973a73065bb487964ef97584ab3fb35e159631664a3ce92a7842de8. Stop the container before attempting removal or force remove [root@102 ~]# docker ps -a ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 8 minutes ago Up 8 minutes 80/tcp focused_wu //docker attach xxxx [root@102 ~]# docker run -itd busybox sleep 6666 becd714e60eaab9215f4cfc9375980ec4c973485668047a79feada601e486c9d [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES becd714e60ea busybox "sleep 6666" 10 seconds ago Up 10 seconds vibrant_volhard ab0f14526973 httpd "httpd-foreground" 10 minutes ago Up 10 minutes 80/tcp focused_wu [root@102 ~]# docker attach becd714e60ea #进去发现卡在哪里 #退出只有一个结果终止后台 //docker exec [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 15 minutes ago Up 15 minutes 80/tcp focused_wu [root@102 ~]# docker exec -it ab0f14526973 /bin/bash root@ab0f14526973:/usr/local/apache2# exit exit [root@102 ~]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab0f14526973 httpd "httpd-foreground" 15 minutes ago Up 15 minutes 80/tcp focused_wu #退出发现后台没有退出



