Docker基础用法
什么是docker
docker是一个用Go语言实现的开源项目,可以让我们方便的创建和使用容器,docker将程序以及程序所有的依赖都打包到docker container,这样你的程序可以在任何环境都会有一致的表现
docker可以屏蔽环境差异,也就是说,只要你的程序打包到了docker中,那么无论运行在什么环境下程序的行为都是一致的,程序员再也无法施展表演才华了,不会再有“在我的环境上可以运行”,真正实现“build once, run everywhere”
docker的另一个好处就是快速部署,这是当前互联网公司最常见的一个应用场景,一个原因在于容器启动速度非常快,另一个原因在于只要确保一个容器中的程序正确运行,那么你就能确信无论在生产环境部署多少都能正确运行
docker中的容器:
- lxc --> libcontainer --> runC
OCI&OCF
OCI
开源的容器协议(Open Container-initiative)
- 由Linux基金会主导于2015年6月创立
- 旨在围绕容器格式和运行时制定一个开放的工业化标准
- 包含以下两个标准
- 运行规范(runtime-spec)
- 镜像规范(image-spec)
OCF
开源的容器格式(Open Container Format)
runC 是一个命令行工具,运行容器记录的一个标准
- 容器是以runC为子进程的方式启动,并且可以被其他的系统引用,不需要启动为守护模式进程
- runC 可以构建我们的容器,并且可以被数以百万的存储引擎使用
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
docker架构
- 这里的Client和DOCKER_HOST(docker server)都是在本地的,docker仓库Registry是在远程的,也可以在本地
- Client的docker命令通过Docker daemon与docker server镜像交互
- images镜像是由应用已经被docker打包好的镜像,如java、nginx的镜像,这些镜像可以运行在容器containers里
- 每一个container容器都是运行在docker server(宿主机)上的,每一个container容器都是隔离的、独立的
- 每一个container容器相当于一个Linux操作系统,每一个container容器都有自己的ip地址,所以可以在不同的container上设置相同的端口号
docker工作原理
- Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问。
- Docker-Server接收到Docker-Client的指令,就会执行这个命令
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
docker对象
当您使用docker时,您可以创建和使用镜像、容器、网络、存储卷、插件和其他对象。
- 镜像
- 一个镜像是只读的模板,是用来创建docker容器的
- 经常情况, 一个镜像是基于另外一个镜像生成的,加上一些特定的内同
- 您可以创建您自己的镜像,也可以使用别人在网站上传的镜像
- 容器
- 容器是一个用镜像运行的实例
- 您可以使用命令行模式或者API进行创建,运行,停止,移动,删除容器
- 您可以将容器连接到一个或多个网络,将存储连接到容器,甚至可以基于其当前状态创建新映像
安装及使用docker
[root@localhost ~]# vim /etc/yum.repos.d/docker-ce.repo
[docker-ce]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
enabled=1
[root@localhost ~]# yum list all|grep docker
docker-ce 21 kB/s | 14 kB 00:00
containerd.io.x86_64 1.4.8-3.1.el8 docker-ce
docker-ce.x86_64 3:20.10.7-3.el8 docker-ce
docker-ce-cli.x86_64 1:20.10.7-3.el8 docker-ce
docker-ce-rootless-extras.x86_64 20.10.7-3.el8 docker-ce
docker-scan-plugin.x86_64 0.8.0-3.el8 docker-ce
pcp-pmda-docker.x86_64 5.3.1-3.el8 appstream
podman-docker.noarch 3.1.0-0.13.module_el8.5.0+733+9bb5dffa appstream
docker安装
//建立缓存
[root@localhost ~]# yum makecache
CentOS Stream 8 - AppStream 4.3 kB/s | 4.4 kB 00:01
CentOS Stream 8 - BaseOS 5.7 kB/s | 3.9 kB 00:00
CentOS Stream 8 - Extras 2.7 kB/s | 3.0 kB 00:01
docker-ce 12 kB/s | 3.5 kB 00:00
元数据缓存已建立。
(尝试在命令行中添加 '--allowerasing' 来替换冲突的软件包 或 '--skip-broken' 来跳过无法安装的软件包 或 '--nobest' 来不只使用软件包的最佳候选)
[root@localhost ~]# yum -y --allowerasing install docker-ce
//启动服务
[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 ~]# ls /etc/docker/
key.json
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://l9h8fu9j.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker info
……
Registry Mirrors:
https://l9h8fu9j.mirror.aliyuncs.com/
//查看docker版本
[root@localhost ~]# docker version
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对象的低级信息 |
//docker search 在官网上搜索镜像
[root@localhost ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3598 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 40
centos/httpd 34 [OK]
polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO… 5 [OK]
hypoport/httpd-cgi httpd-cgi 2 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
centos/httpd-24-centos8 1
publici/httpd httpd:latest 1 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
dockerpinata/httpd 1
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK]
interlutions/httpd httpd docker image with debian-based config … 0 [OK]
amd64/httpd The Apache HTTP Server Project 0
e2eteam/httpd 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
manasip/httpd 0
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
trollin/httpd 0
webratio/httpd-with-curl Apache HTTP Server (https://httpd.apache.org… 0 [OK]
ysli/httpd Httpd for DeepWeb 0 [OK]
//docker pull 下载官网的镜像,不加版本号默认下载最新版本
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
33847f680f63: Pull complete
d74938eee980: Pull complete
963cfdce5a0c: Pull complete
8d5a3cca778c: Pull complete
e06a573b193b: Pull complete
Digest: sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
//docker images 查看有哪些镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 73b8cfec1155 5 days ago 138MB
//docker create 创建一个容器
[root@localhost ~]# docker create httpd
1f5f0825ce2fbb58ae5f8899ba9a40c7b813a3b895272478e1b02fd29066eb02
//docker ps 查看正在运行的容器;-a 表示所有的容器
[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
1f5f0825ce2f httpd "httpd-foreground" 28 seconds ago Created vigilant_meitner
//docker start 启动容器
[root@localhost ~]# docker start 1f5f0825ce2f
1f5f0825ce2f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f5f0825ce2f httpd "httpd-foreground" About a minute ago Up 7 seconds 80/tcp vigilant_meitner
//docker stop 停止容器运行
[root@localhost ~]# docker stop 1f5f0825ce2f
1f5f0825ce2f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//docker restart 重启容器
[root@localhost ~]# docker restart 1f5f0825ce2f
1f5f0825ce2f
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f5f0825ce2f httpd "httpd-foreground" 2 minutes ago Up 7 seconds 80/tcp vigilant_meitner
//docker kill 强制终止一个容器;不推荐使用,使用docker stop就可以了
[root@localhost ~]# docker kill 1f5f0825ce2f
1f5f0825ce2f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//docker rm 删除一个容器,不能删除正在运行的容器;-f可以删除正在运行的容器;rmi删除镜像
[root@localhost ~]# docker rm -f 1f5f0825ce2f
1f5f0825ce2f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//docker rmi 镜像,镜像在被容器使用的时候是不能被删除的
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 73b8cfec1155 5 days ago 138MB
[root@localhost ~]# docker rmi httpd
Untagged: httpd:latest
Untagged: httpd@sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Deleted: sha256:73b8cfec11558fe86f565b4357f6d6c8560f4c49a5f15ae970a24da86c9adc93
Deleted: sha256:8dfdbde7a5f7b804884b5e1140b66cc78155416621e63369044f370278171f2a
Deleted: sha256:c4d64464b597b3d82b3a3bdb91979a70a8db3c707d484e54420573b33300f58c
Deleted: sha256:75b62cbb47406663192fa84a9fc496c5b3dec3b2f489a01a073587af9218ca7d
Deleted: sha256:1a75986150c7f76dac53856bced40beed6b7642e1686f11286325765f0c3b00c
Deleted: sha256:814bff7343242acfd20a2c841e041dd57c50f0cf844d4abd2329f78b992197f4
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
//docker run 直接运行一个容器;-d在后台运行
[root@localhost ~]# docker run -d httpd
cc81673c1a69ebf5e80acc07c4d6ce4b84147110d50b4e0f58e44b0de1194a80
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc81673c1a69 httpd "httpd-foreground" 10 seconds ago Up 8 seconds 80/tcp determined_wiles
//docker logs 查看容器日志
[root@localhost ~]# docker logs cc81673c1a69
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
[Tue Jul 27 13:09:26.069643 2021] [mpm_event:notice] [pid 1:tid 139896291255424] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Tue Jul 27 13:09:26.069839 2021] [core:notice] [pid 1:tid 139896291255424] AH00094: Command line: 'httpd -D FOREGROUND'
在一台终端上访问ip
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
生成新的日志
[root@localhost ~]# docker logs cc81673c1a69
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
[Tue Jul 27 13:09:26.069643 2021] [mpm_event:notice] [pid 1:tid 139896291255424] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Tue Jul 27 13:09:26.069839 2021] [core:notice] [pid 1:tid 139896291255424] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [27/Jul/2021:13:10:33 +0000] "GET / HTTP/1.1" 200 45
//docker inspect 查看容器的各种信息
[root@localhost ~]# docker inspect cc81673c1a69
……
"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,
……
//docker info 查看docker整个的信息
[root@localhost ~]# 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.5.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
……
//docker attach 进入容器内部,但是不能操作,退出会终止容器,不推荐使用
在另外一个终端查看网站
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc81673c1a69 httpd "httpd-foreground" 6 minutes ago Up 6 minutes 80/tcp determined_wiles
进入容器可以看见有人在访问容器,但是不能进行任何操作,只可以观察,而且不方便退出
[root@localhost ~]# docker attach cc81673c1a69
172.17.0.1 - - [27/Jul/2021:13:18:12 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [27/Jul/2021:13:18:30 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [27/Jul/2021:13:18:36 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [27/Jul/2021:13:18:37 +0000] "GET / HTTP/1.1" 200 45
//docker exec 进入容器;-it 表示交互模式,可以执行命令并且退出不会停止容器,exit退出
[root@localhost ~]# docker start 757fa250d538
757fa250d538
[root@localhost ~]# docker exec -it 757fa250d538 /bin/bash
root@757fa250d538:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@757fa250d538:/usr/local/apache2# exit
exit
docker event state
一个容器在某个时刻可能处于以下几种状态之一:
- created:已经被创建 (使用 docker ps -a 命令可以列出)但是还没有被启动 (使用 docker ps 命令还无法列出)
- running:运行中
- paused:容器的进程被暂停了
- restarting:容器的进程正在重启过程中
- exited:上图中的 stopped 状态,表示容器之前运行过但是现在处于停止状态(要区别于 created 状态,它是指一个新创出的尚未运行过的容器)。可以通过 start 命令使其重新进入 running 状态
- destroyed:容器被删除了,再也不存在了