Docker安装及管理
Centos7安装docker
操作系统要求
要安装 Docker Engine,您需要 CentOS 7 或 8 的维护版本。存档版本不受支持或测试。
centos-extras
必须启用存储库。此存储库默认启用,但如果您已禁用它,则需要 重新启用它。推荐使用overlay2
存储驱动。
卸载旧版本
旧版本的 Docker 被称为docker
或docker-engine
. 如果安装了这些,请卸载它们以及相关的依赖项。
[root@localhost ~]# yum remove docker \ > docker-client \ > docker-client-latest \ > docker-common \ > docker-latest \ > docker-latest-logrotate \ > docker-logrotate \ > docker-engine
安装新版本
您可以根据需要以不同的方式安装 Docker Engine:
大多数用户 设置 Docker 的存储库并从中安装,以便于安装和升级任务。这是推荐的方法。
在新主机上首次安装 Docker Engine 之前,您需要设置 Docker 存储库。之后,您可以从存储库安装和更新 Docker。
设置存储库
安装yum-utils包(提供yum-config-manager 实用程序)并设置稳定的存储库。
[root@localhost ~]# yum install -y yum-utils
下载并安装存储库
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
查看安装的文件信息
[root@localhost ~]# ll /etc/yum.repos.d/ total 56 drwxr-xr-x. 2 root root 4096 Aug 30 2019 bak -rw-r--r--. 1 root root 2523 Dec 26 2020 CentOS-Base.repo -rw-r--r-- 1 root root 1309 Nov 23 2020 CentOS-CR.repo -rw-r--r-- 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo -rw-r--r-- 1 root root 314 Nov 23 2020 CentOS-fasttrack.repo -rw-r--r-- 1 root root 630 Nov 23 2020 CentOS-Media.repo -rw-r--r-- 1 root root 1331 Nov 23 2020 CentOS-Sources.repo -rw-r--r-- 1 root root 8515 Nov 23 2020 CentOS-Vault.repo -rw-r--r-- 1 root root 616 Nov 23 2020 CentOS-x86_64-kernel.repo -rw-r--r-- 1 root root 1919 Jan 5 15:22 docker-ce.repo -rw-r--r-- 1 root root 664 Dec 26 2020 epel.repo.rpmsave -rw-r--r-- 1 root root 636 May 14 2021 zabbix.repo
1:安装最新 Docker引擎
安装最新版本的 Docker Engine 和 containerd,或者进入下一步安装特定版本:
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
如果您启用了多个 Docker 存储库,使用yum install 或yum update 安装或更新的时候未指定版本库,通常是安装最新的版本,但是这样可能不稳定。
此命令会安装 Docker,但不会启动 Docker。它还会创建一个 docker组,但是默认情况下它不会将任何用户添加到该组中。
2: 要安装特定版本的Docker Engine,请在 repo 中列出可用版本,然后选择并安装:
列出并排序当前存储库中可用的版本,此示例按版本号从最高到最低对结果进行排序。
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r * updates: mirrors.aliyun.com Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror Installed Packages * extras: mirrors.aliyun.com docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.12-3.el7 @docker-ce-stable docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.15-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: mirrors.aliyun.com Available Packages
通过其完全限定的包名称安装特定版本,即包名称 ( docker-ce) 加上版本字符串(第 2 列),从第一个冒号 ( :) 开始,直到第一个连字符,用连字符 ( ) 分隔-。例如,docker-ce-18.09.1。
执行一下命令即可安装需要的版本
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
我当前安装的是最新的,即列表的最上面一个,版本号为:20.10.9 。
此命令会安装 Docker,但不会启动 Docker。它还会创建一个 docker组,但是默认情况下它不会将任何用户添加到该组中。
3:启动 Docker。
[root@localhost ~]# systemctl start docker
执行hello-world
hello-world 通过运行映像来验证 Docker 引擎是否已正确安装。
[root@localhost ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
此命令下载测试映像并在容器中运行它。当容器运行时,它会打印一条消息(红色部分)并退出。
这将安装并运行 Docker 引擎。用于sudo运行 Docker 命令。继续Linux 后安装以允许非特权用户运行 Docker 命令和其他可选配置步骤。
4:卸载 docker
删除安装包:
yum remove docker-ce
删除镜像、容器、配置文件等内容:
rm -rf /var/lib/docker
配置docker registry-mirrors,
docker下载镜像过慢是因为默认的镜像地址是国外的,我们只要配置一下docker的镜像地址就可以了!
配置中国科技大学的源
vim /etc/docker/daemon.json
{ "registry-mirrors":["https://docker.mirrors.ustc.edu.cn","https://almtd3fa.mirror.aliyuncs.com"] }
重新加载和启动docker
systemctl daemon-reload systemctl restart docker.service
Compose介绍
详细介绍: https://www.jianshu.com/p/87d701d0801d
Docker Compose是一个用来定义和运行复杂应用的Docker工具。一个使用Docker容器的应用,通常由多个容器组成。使用Docker Compose不再需要使用shell脚本来启动容器。
Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景。
Compose和Docker兼容性
compose文件格式版本 | docker版本 |
---|---|
3.4 | 17.09.0+ |
3.3 | 17.06.0+ |
3.2 | 17.04.0+ |
3.1 | 1.13.1+ |
3.0 | 1.13.0+ |
2.3 | 17.06.0+ |
2.2 | 1.13.0+ |
2.1 | 1.12.0+ |
2.0 | 1.10.0+ |
1.0 | 1.9.1.+ |
安装 docker-compose
最新发行的版本地址:https://github.com/docker/compose/releases
运行以下命令以下载Docker Compose的当前稳定版本:
[root@localhost ~]# curl -L "https://github.com/docker/compose/releases/download/1.28.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 664 100 664 0 0 1156 0 --:--:-- --:--:-- --:--:-- 1158
100 11.6M 100 11.6M 0 0 2253k 0 0:00:05 0:00:05 --:--:-- 2838k
其中地址中有系统参数,实际请求的地址是: https://github.com/docker/compose/releases/download/1.28.4/docker-compose-Linux-x86_64
注意: 如果无法下载,可以本地下载好后上传到服务器即可:
下载地址:https://github.com/docker/compose/releases
将可执行权限应用于二进制文件:
注意:如果命令docker-compose
在安装后失败,请检查您的路径。您还可以创建指向/usr/bin
或路径中任何其他目录的符号链接。
测试安装
[root@localhost bin]# docker-compose -v docker-compose version 1.28.4, build cabd5cfb
bash 命令补齐
curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
卸载docker-compose
直接移除二进制包即可
rm /usr/local/bin/docker-compose
简单示例:docker-compose 安装redis
先创建redis启动的compose文件: redis-compose.yml
version: '3' services: my-redis: image: redis:latest restart: always container_name: redis-server privileged: true ports: - 6379:6379 environment: TZ: Asia/Shanghai volumes: - /usr/local/docker/redis/redis.conf:/etc/redis/redis.conf command: ["redis-server","/etc/redis/redis.conf"]
创建reids的配置文件 /usr/local/docker/redis/redis.conf 内容如下:
port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel verbose logfile /data/redis.log databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
配置项目:
1: Version 3.x
最新版本,也是推荐使用版本,推出该版的目的是为了在Compose和Docker Engine的swarm模式之间形成交叉兼容。
通过YAML的根配置项version来指定,具体配置如version: '3'或version: '3.1'等。
2: services: 服务配置
服务定义了该服务启动的每个容器的配置
3: privileged: true #使用该参数,container内的root拥有真正的root权限,否则,container内的root只是外部的一个普通用户权限,设置为true,不然数据卷可能挂载不了,启动不起
4: restart: always #设置无论遇到什么错,重启容器
5:command: 覆盖容器启动后默认执行的命令 ,有 2种写法
字符串形式: command: redis-server /etc/redis/redis.conf
JSON数组形式: command: ["redis-server","/etc/redis/redis.conf"]
执行命令:
docker-compose -f redis-compose.yml up -d
参数 -f 指定相关的compose文件,如果文件名为docker-compose.yml ,则可以不用当前参数,
Portainer.io管理工具
https://blog.csdn.net/qq_39177602/article/details/113854625
Docker之网络配置和部署SpringCloud项目
https://copyfuture.com/blogs-details/20210617154040825s
使用Portainer-ce管理监控docker容器实例
https://blog.csdn.net/cnskylee/article/details/125005339
搭建docker中的portainer 访问宿主机端口报错 curl: (56) Recv failure: Connection reset by peer
解决方法 : 重建docker0网桥,但出现错误原因不清楚
重建docker0网桥步骤:
# 停止docker systemctl stop docker # 安装bridge-utils yum install -y bridge-utils # 关闭docker0网桥 ip link set dev docker0 down # 删除docker0网桥 brctl delbr docker0 # 添加docker0网桥 brctl addbr docker0 # 添加网桥信息 ip addr add 172.16.10.1/24 dev docker0 # 启动docker0网桥 ip link set dev docker0 up # 查看网桥是否正常 ip addr # 启动docker systemctl restart docker
可能每次重启centos都要运行一遍这个
安装问题:
1: 执行下来命令是出现错误
[root@xzkj-huanan2 docker]# docker run --name nginx-docker -p 8089:80 -d nginx:v3
617d51685ad437fec194da91f78ad8c8dd9da91e4161283fc5dace17b095c555
docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx-docker (a5e8b9bf56ee08008d6aabc7ef434b0b6898cb578dc842e71b343e93e89971cc): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8089 -j DNAT --to-destination 172.17.0.3:80 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
详细原因:docker服务启动时定义的自定义链DOCKER,当 centos7 firewall 被清掉时,
firewall的底层是使用iptables进行数据过滤,建立在iptables之上,这可能会与 Docker 产生冲突。
当 firewalld 启动或者重启的时候,将会从 iptables 中移除 DOCKER 的规则,从而影响了 Docker 的正常工作。
当你使用的是 Systemd 的时候, firewalld 会在 Docker 之前启动,但是如果你在 Docker 启动之后操作 firewalld ,你就需要重启 Docker 进程了。
解决方法:输入指令 systemctl restart docker 重启docker服务及可重新生成自定义链DOCKER
Ubuntu系统安装Docker
1.卸载旧版本
ubuntu下自带了docker的库,不需要添加新的源。
但是ubuntu自带的docker版本太低,需要先卸载旧的再安装新的, 然后获取软件最新源
sudo apt-get remove docker docker-engine docker.io containerd runc sudo apt-get update
3.安装 apt 依赖包,用于通过HTTPS来获取仓库
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
3.安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
4.验证
sudo apt-key fingerprint 0EBFCD88
5.设置稳定版仓库
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
6. 更新 apt 包索引
sudo apt-get update
7.安装最新版本docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
也可以选择某个版本安装:
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
8.配置用户组
sudo usermod -aG docker your-user 比如:
sudo usermod -aG docker wb2010
二、卸载docker
1.删除安装包
sudo apt-get autoremove docker docker-ce docker-engine docker.io containerd runc
2.删除相关配置文件
dpkg -l | grep docker
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
3.卸载相关插件
sudo apt-get autoremove docker-ce-*