什么是docker
docker中的容器:
- lxc --> libcontainer --> runC
OCI&OCF
OCI
Open Container-initiative
- 由Linux基金会主导于2015年6月创立
- 旨在围绕容器格式和运行时制定一个开放的工业化标准
- contains two specifications
- the Runtime Specification(runtime-spec)
- the Image Specification(image-spec)
OCF
Open Container Format
runC is a CLI tool for spawning and running containers according to the OCI specification
- Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon
- runC is built on libcontainer, the same container technology powering millions of Docker Engine installations
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
docker架构
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
docker对象
Docker镜像,容器,网络,卷,插件等是Docker对象。
在Dockerland,有镜像,有容器。这两者密切相关,但截然不同。但这一切都始于Dockerfile。
一个Dockerfile是您创建镜像时,你建立的一个文件。它包含一堆指令,告知Docker如何构建Docker镜像。
你可以把它与烹饪联系起来。在烹饪中你有食谱。通过配方,您可以了解必须采取的所有步骤,以便生成您想要烹饪的任何内容。烹饪行为正在建立食谱。
docker安装
下载 centos源 epel源 docker源
docker加速
启动服务 开机自启
[root@localhost ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendo>
Active: active (running) since Sun 2022-04-24 16:11:11 CST; 36s
启动服务 开机自启 [root@localhost ~]# systemctl enable --now docker Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service. [root@localhost ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendo> Active: active (running) since Sun 2022-04-24 16:11:11 CST; 36s
[root@localhost ~]# cd /etc/docker/ [root@localhost docker]# ls key.json [root@localhost docker]# vi daemon.json [root@localhost docker]# vi daemon.json [root@localhost docker]# systemctl daemon-reload [root@localhost docker]# systemctl restart docker [root@localhost docker]# cd [root@localhost ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Docker Buildx (Docker Inc., v0.8.1-docker) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 20.10.14 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: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc version: v1.0.3-0-gf46b6ba init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.18.0-365.el8.x86_64 Operating System: CentOS Stream 8 OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 1.744GiB Name: localhost ID: DJ4R:W3Q2:3VDI:FAJ4:W4PM:GMMX:TMIJ:WVQR:CVCN:ELIL:J5LX:NSCM 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: https://4tw3yr47.mirror.aliyuncs.com/ Live Restore Enabled: false [root@localhost ~]# docker version Client: Docker Engine - Community Version: 20.10.14 API version: 1.41 Go version: go1.16.15 Git commit: a224086 Built: Thu Mar 24 01:47:44 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.14 API version: 1.41 (minimum version 1.12) Go version: go1.16.15 Git commit: 87a90dc Built: Thu Mar 24 01:46:10 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.5.11 GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc: Version: 1.0.3 GitCommit: v1.0.3-0-gf46b6ba docker-init: Version: 0.19.0 GitCommit: de40ad0
docker常用操作
| 命令 | 功能 |
|---|---|
| docker search | 在 Docker Hub 中搜索镜像 |
| docker pull | 从注册表中拉取镜像或存储库 |
| docker images | 列出镜像 |
| docker create | 创建一个新的容器 |
| docker start | 启动一个或多个停止的容器 |
| docker run | 在新容器中运行命令 |
| docker attach | 附加到正在运行的容器 |
| docker ps | 列出容器 |
| docker logs | 获取容器的日志 |
| docker restart | 重启一个容器 |
| docker stop | 停止一个或多个正在运行的容器 |
| docker kill | 杀死一个或多个正在运行的容器 |
| docker rm | 移除一个或多个容器 |
| docker exec | 在正在运行的容器中运行命令 |
| docker info | 显示系统范围的信息 |
| docker inspect | 返回有关 Docker 对象的低级信息 |
[root@localhost ~]# docker search httpd NAME DESCRIPTION STARS OFFICIAL AUTOMATED httpd The Apache HTTP Server Project 3975 [OK] centos/httpd-24-centos7 Platform for running Apache http or bui… 44 centos/httpd 35 [OK] hypoport/httpd-cgi httpd-cgi 2 [OK] solsson/httpd-openidc mod_auth_openidc on official httage, ve… 2 [OK] manageiq/httpd Container with httpd, built on C for Ma… 1 [OK] lead4good/httpd-fpm httpd server which connects via proxy h… 1 [OK] dockerpinata/httpd 1 publici/httpd httpd:latest 1 [OK] dariko/httpd-rproxy-ldap Apache httpd reverse proxy with authent… 1 [OK] inanimate/httpd-ssl A play container with httpd, sslled, an… 1 [OK] centos/httpd-24-centos8 1 clearlinux/httpd httpd HyperText Transfer ProtocoTP) ser… 1 manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK] manasip/httpd 0 e2eteam/httpd 0 paketobuildpacks/httpd 0 httpdocker/kubia 0 sandeep1988/httpd-new httpd-new 0 httpdocker/kubia-unhealthy 0 amd64/httpd The Apache HTTP Server Project 0 patrickha/httpd-err 0 httpdss/archerysec ArcherySec repository 0 [OK] 19022021/httpd-connection_test This httpd image will test the ctivity … 0 itsziget/httpd24 Extended HTTPD Docker image basethe off… 0 [OK]docker pull
[root@localhost ~]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd 最新的 a2abf6c4d29d: Pull complete dcc4698797c8: Pull complete 41c22baa66ec: Pull complete 67283bbdd4a0: Pull complete d982c879c57e: Pull complete Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32 Status: Downloaded newer image for httpd:latest docker.io/library/httpd:latestdocker images
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest dabbfbe0c57b 4 months ago 144MBdocker create 创建容器 docker ps 查看 docker start开启
[root@localhost ~]# docker create --name web -p 80:80 httpd 30aadc28b10b37ba01301649d3a84722c0ba13d512acf15f112dd8e9f30a4f28 [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 30aadc28b10b httpd "httpd-foreground" 32 seconds ago Created web [root@localhost ~]# docker start web web [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30aadc28b10b httpd "httpd-foreground" About a minute ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web 然后关闭防火墙 就可以访问
docker stop 停用容器
[root@localhost ~]# docker stop 30aadc28b10b 用id停用 也可 30aadc28b10b [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30aadc28b10b httpd "httpd-foreground" 7 minutes ago Exited (0) 5 seconds ago webdocker restart 重启容易
[root@localhost ~]# docker restart web web [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30aadc28b10b httpd "httpd-foreground" 12 minutes ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp webdocker kill 强制退出
[root@localhost ~]# docker kill web web [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30aadc28b10b httpd "httpd-foreground" 13 minutes ago Exited) 6 seconds ago webdocker logs
[root@localhost ~]# docker logs web 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 [Sun Apr 24 09:40:07.676129 2022] [mpm_event:notice] [pid 1:tid 139654408637760] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations [Sun Apr 24 09:40:07.676400 2022] [core:notice] [pid 1:tid 139654408637760] AH00094: Command line: 'httpd -D FOREGROUND' 10.10.10.1 - - [24/Apr/2022:09:43:03 +0000] "GET /favicon.ico HTTP/1.1" 404 196 10.10.10.1 - - [24/Apr/2022:09:43:04 +0000] "GET /favicon.ico HTTP/1.1" 404 196 10.10.10.1 - - [24/Apr/2022:09:43:06 +0000] "GET / HTTP/1.1" 200 45 10.10.10.1 - - [24/Apr/2022:09:43:06 +0000] "GET /favicon.ico HTTP/1.1" 404 196 10.10.10.1 - - [24/Apr/2022:09:43:07 +0000] "GET /favicon.ico HTTP/1.1" 404 196docker rm
[root@localhost ~]# docker rm -f web web [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 容易删除 镜像还在docker run
[root@localhost ~]# docker run -it --name test busybox /bin/sh 交互模式 Unable to find image 'busybox:latest' locally latest: Pulling from library/busybox 5cc84ad355aa: Pull complete Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678 Status: Downloaded newer image for busybox:latest / # ip a 1: lo:docker attachmtu 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 11: eth0@if12: 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@localhost ~]# docker attach test / #docker exec
[root@localhost ~]# docker exec -it test /bin/sh / # ip a / # 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 11: eth0@if12: 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



