# 03_docker容器化技术精讲(三)
03_docker容器化技术精讲(三)
一、dockerfile-案例-自定义centos
1、Dockerfile 案例:自定义centos
1)案例:需求
自定义centos7镜像。要求:
** 默认登录路径为 /usr
** 可以使用vim
2)案例:实现步骤
① 定义父镜像:FROM centos:7
② 定义作者信息:MAINTAINER itheima itheima@itcast.cn
③ 执行安装vim命令: RUN yum install -y vim
④ 定义默认的工作目录:WORKDIR /usr
⑤ 定义容器启动执行的命令:CMD /bin/bash
⑥ 通过dockerfile构建镜像:docker bulid –f dockerfile文件路径 –t 镜像名称:版本
2、Dockerfile 案例:自定义centos 操作实践操作
1)创建 docker-files 目录,并切换到此目录:mkdir docker-files && cd docker-files
2)创建并编辑 centos_dockerfile 文件:vim centos_dockerfile
FROM centos:7
MAINTAINER dzs168<dzs168.blog.csdn.net>
RUN yum install -y vim
WORKDIR /usr
cmd /bin/bash
3)在当前目录(注意命令后面的 .
),通过dockerfile构建镜像
docker build -f ./centos_dockerfile -t centos_usr:v1 .
4)创建交互式容器(创建并进入容器):
docker run -it --name=cc centos_usr:v1
5)如果 docker build 构建镜像失败,尝试修改编辑 centos_dockerfile 文件:
vim centos_dockerfile
FROM centos:7
MAINTAINER dzs168
RUN cd /etc/yum.repos.d/ \
&& sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* \
&& sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* \
&& yum install -y vim
CMD /bin/bash
WORKDIR /usr
3、示例:
4、如果 执行 docker build -f ./centos_dockerfile -t centos_usr:v1 .
时 RUN yum install -y vim
报错
错误信息如下 :
yum-config-manager --save --setopt=.skip_if_unavailable=true
或者error :Cannot find a valid baseurl for repo: base/7/x86_64
或者 error :ERROR: failed to solve: process “/bin/sh -c yum install -y vim” did not complete successfully:
可以尝试以下方法,进行逐一排除(任何一项执行后,如果构建正常,就无须进行下一项)。
1)修改编辑 centos_dockerfile 文件:vim centos_dockerfile
FROM centos:7
MAINTAINER dzs168
RUN cd /etc/yum.repos.d/ \
&& sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* \
&& sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* \
&& yum install -y vim
CMD /bin/bash
WORKDIR /usr
2)配置 yum 源 vim /etc/yum.repos.d/CentOS-Base.repo
# 更改 yum 源
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
3)启用仓库配置
yum-config-manager --enable base
4)清理缓存,重建缓存
yum clean all
yum makecache
5)配置网卡,添加 DNS 服务器:vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
# BOOTPROTO=dhcp
BOOTPROTO=static
IPADDR=192.168.43.216
NETMASK=255.255.255.0
GATEWAY=192.168.43.1
DNS1=8.8.8.8
DNS2=114.114.114.114
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=43f2469a-7ff2-418f-ac9a-3bc5a3f14d46
DEVICE=enp0s3
ONBOOT=yes
6)重启网卡
systemctl restart network
# 或者
service network restart
7)配置 docker 镜像加速器 vim /etc/docker/daemon.json
# 配置内容如下:
{
"registry-mirrors": ["https://5y3wa0zw.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://docker.m.daocloud.io/",
"https://huecker.io/",
"https://dockerhub.timeweb.cloud",
"https://noohub.ru/",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://xx4bwyg2.mirror.aliyuncs.com",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": ["192.168.43.216:5000","harbor"]
}
# 重新加载配置文件
systemctl daemon-reload
# 重启docker
systemctl restart docker
8)在当前目录(注意命令后面的 .
),重新通过dockerfile构建镜像
docker build -f ./centos_dockerfile -t centos_usr:v1 .
二、dockerfile-案例- 部署springboot项目
1、Dockerfile 案例:部署springboot项目
1)案例:需求
定义dockerfile,发布springboot项目
2)案例:实现步骤
① 定义父镜像:FROM java:8
② 定义作者信息:MAINTAINER itheima itheima@itcast.cn
③ 将jar包添加到容器: ADD springboot.jar app.jar
④ 定义容器启动执行的命令:CMD java–jar app.jar
⑤ 通过dockerfile构建镜像:docker bulid –f dockerfile文件路径 –t 镜像名称:版本
2、Dockerfile 案例 :定义dockerfile,发布springboot项目
1)准备并测试 springboot-hello-0.0.1-SNAPSHOT.jar 项目的 jar 包。
# 打开CMD 命令窗口,切换到 jar 包所在目录,启动项目,输入:
java -jar .\springboot-hello-0.0.1-SNAPSHOT.jar
# 浏览器输入 http://localhost:8080/hello 进行测试,会输出
hello springboot for docker ~~
2)上传 springboot-hello-0.0.1-SNAPSHOT.jar 项目的 jar 包 到服务器,如:192.168.43.216 ,创建 docker_files 目录,并把springboot-hello-0.0.1-SNAPSHOT.jar 放到此目录。
[root@manager1 ~]# pwd
/root
[root@manager1 ~]# mkdir docker_files
[root@manager1 ~]# mv springboot-hello-0.0.1-SNAPSHOT.jar ./docker_files/
[root@manager1 ~]# cd docker_files/
3)创建并编辑 springboot_dockerfile 文件:vim springboot_dockerfile
# FROM java:8
FROM openjdk:8-jdk
MAINTAINER dzs168<dzs168.blog.csdn.net>
ADD springboot-hello-0.0.1-SNAPSHOT.jar app.jar
CMD java -jar app.jar
4)在当前目录(注意命令后面的 .
),通过dockerfile构建镜像
docker build -f ./springboot_dockerfile -t app .
5)创建守护式容器(后台运行),并进行端口映射
docker run -id -p 9000:8080 app
6)浏览器访问测试,如:http://192.168.43.216:9000/hello
三、docker compose-概述
1、Docker 服务编排
1)服务编排概念
2)Docker Compose 概述
3)案例
2、服务编排概念
1)微服务架构的应用系统中一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,维护的工作量会很大。
• 要从Dockerfile build image 或者去dockerhub拉取image
• 要创建多个container
• 要管理这些container(启动停止删除)
2)服务编排: 按照一定的业务规则批量管理容器。
3、Docker Compose
Docker Compose是一个编排多容器分布式部署的工具,提供命令集管理容器化应用的完整开发周期,包括服务构建,启动和停止。使用步骤:
1)利用 Dockerfile 定义运行环境镜像。
2)使用 docker-compose.yml 定义组成应用的各服务。
3)运行 docker-compose up 启动应用。
四、docker compose-安装使用
1、安装Docker Compose
1) Compose目前已经完全支持Linux、Mac OS和Windows,
2) 安装Compose之前,需要先安装Docker。
3)2025-1-14 最新的 docker 已经集成了 docker-compose 功能。
如:Docker version 26.1.4, build 5650f9b 已经集成了 Docker Compose version v2.27.1 。
# 以下是编译好的二进制包方式安装在Linux系统中。
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 安装 docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 使用代理服务器,安装 docker-compose
curl -L "https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 设置文件可执行权限
chmod +x /usr/local/bin/docker-compose
# 创建软链接
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# 查看版本信息或验证是否安装成功
docker-compose -version
# 或者
docker compose version
2、卸载Docker Compose
# 二进制包方式安装的,删除二进制文件即可
rm /usr/local/bin/docker-compose
3、 使用docker compose编排nginx+springboot项目
1) 创建docker-compose目录
mkdir ~/docker-compose
cd ~/docker-compose
2) 编写 docker-compose.yml 文件 vim docker-compose.yml
version: '3'
services:
nginx:
image: nginx
ports:
- 80:80
links:
- app
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
app:
image: app
expose:
- "8080"
3) 创建 ./nginx/conf.d目录
mkdir -p ./nginx/conf.d
cd ./nginx/conf.d
4)在 ./nginx/conf.d目录下 编写dzs168.conf文件:vim dzs168.conf
server {
listen 80;
access_log off;
location / {
proxy_pass http://app:8080;
}
}
5)在~/docker-compose 目录下(cd ~/docker-compose),使用docker-compose 启动容器
# 前台启动
docker-compose up
# 后台启动
docker-compose up -d
# 如果 80 端口占用,可能有 httpd 服务,停止apache httpd 服务即可。
systemctl stop httpd
6)测试访问(如服务器IP地址是:192.168.43.216)
http://192.168.43.216/hello
4、示例:
五、docker私有仓库-搭建
1、Docker 私有仓库搭建步骤
1)搭建私有仓库
2)上传镜像到私有仓库
3)从私有仓库拉取镜像
2、Docker 私有仓库
Docker官方的Docker hub(https://hub.docker.com)是一个用于管理公共镜像的仓库,可以从上面拉取镜像到本地,也可以把自己的镜像推送上去。
但是,有时候我们的服务器无法访问互联网,或者你不希望将自己的镜像放到公网当中,那么就需要搭建自己的私有仓库来存储和管理自己的镜像。
3、搭建自己的 docker私有仓库
# 1、拉取私有仓库镜像
docker pull registry
# 2、启动私有仓库容器
docker run -id --name=registry -p 5000:5000 registry
# 或者
sudo docker run -id -p 5000:5000 --restart=always --name registry registry:latest
# 3、打开浏览器 输入地址http://私有仓库服务器ip:5000/v2/_catalog,看到{"repositories":[]} 表示私有仓库 搭建成功
# 如:http://192.168.43.216:5000/v2/_catalog
# 4、修改daemon.json
vim /etc/docker/daemon.json
# 在上述文件中添加一个key,保存退出。此步用于让 docker 信任私有仓库地址;注意将私有仓库服务器ip修改为自己私有仓库服务器真实ip
{"insecure-registries":["私有仓库服务器ip:5000"]}
# 如: {"insecure-registries":["192.168.43.216:5000"]}
{
"registry-mirrors": ["https://5y3wa0zw.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://docker.m.daocloud.io/",
"https://huecker.io/",
"https://dockerhub.timeweb.cloud",
"https://noohub.ru/",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://xx4bwyg2.mirror.aliyuncs.com",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": ["192.168.43.216:5000","harbor"]
}
# 5、重启docker 服务
systemctl restart docker
docker start registry
4、示例:
5、CentOS7 在使用docker 时,经常出现网络问题,yum安装软件的时候经常出现找不到镜像的情况 解决方法
1)问题描述:
如:在设置Docker的稳定仓库时,出现网络超时,如下信息:
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
Could not fetch/save url https://download.docker.com/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 12] Timeout on https://download.docker.com/linux/centos/docker-ce.repo: (28, 'Connection timed out after 30819 milliseconds')
2)解决方法:
国内访问不到docker官方镜像,使用阿里云镜像
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3)其他方法:更新/etc/yum.repos.d目录下的CentOS-Base.repo文件内容,使用阿里的镜像源
vim /etc/yum.repos.d/CentOS-Base.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
或者
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
4)其他方法:修改更新 /etc/yum.repos.d目录下的docker.repo文件内容,使用阿里的镜像源,复制以下内容vim /etc/yum.repos.d/docker.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
5)其他方法:vim /etc/docker/daemon.json 配置多个镜像加速器
{
"registry-mirrors": ["https://5y3wa0zw.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://docker.m.daocloud.io/",
"https://huecker.io/",
"https://dockerhub.timeweb.cloud",
"https://noohub.ru/",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://xx4bwyg2.mirror.aliyuncs.com",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": ["192.168.43.216:5000"]
}
六、docker私有仓库-上传镜像
1、标记镜像为私有仓库的镜像
# docker tag centos:7 私有仓库服务器IP:5000/centos:7
docker tag centos:7 192.168.43.216:5000/centos:7
docker tag centos 192.168.3.29:5000/centos:latest
2、上传标记的镜像到私有仓库
# docker push 私有仓库服务器IP:5000/centos:7
docker push 192.168.43.216:5000/centos:7
docker push 192.168.3.29:5000/centos
3、示例:
4、如果 docker push 私有仓库服务器IP出现错误
open /var/lib/docker/overlay2/.../diff/root/anaconda-post.log: no such file or directory
如下图
5、解决方法:尝试以下方法:
1)删除 docker 密码认证 vim ~/.docker/config.json
# 修改前:
{
"auths": {
"192.168.3.29:5000": {
"auth": "ZHpzMTY4OjEyMzQ1Ng=="
},
"crpi-pjzeu2d5vvj7juk4.cn-hangzhou.personal.cr.aliyuncs.com": {
"auth": "ZDE3MzIyMzI4OTpkamgwMTIzMTE="
}
}
}
# 修改后:
{
"auths": { }
}
2)更新 软件包
yum update -y
3)清除 yum 存储的所有数据,包括缓存的软件包、元数据和头文件等
yum clean all
4)清理 Docker 系统资源:
可以尝试清理 Docker 的未使用资源,以解决可能的文件系统不一致问题:
docker system prune -a
5)配置多个镜像加速器:vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://5y3wa0zw.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://docker.m.daocloud.io/",
"https://huecker.io/",
"https://dockerhub.timeweb.cloud",
"https://noohub.ru/",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn",
"https://xx4bwyg2.mirror.aliyuncs.com",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": ["192.168.3.29:5000"],
"storage-driver": "overlay2"
}
6)删除所有容器,重新拉取镜像、创建容器私有仓库,推送镜像到私有仓库。
# 先停止正在运行的容器
docker stop registry
# 删除所有容器
docker rm `docker ps -aq`
# 查看所有镜像
docker images
systemctl daemon-reload
systemctl restart docker
# 拉取镜像
docker pull centos
# 给镜像打标签
docker tag centos 192.168.3.29:5000/centos:latest
# 创建守护式容器,容器始终处于运行状态,并进行端口映射
docker run -id --name=registry -p 5000:5000 --restart=always registry:latest
# 再次请求查看私有仓库,空的
curl 192.168.3.29:5000/v2/_catalog
{"repositories":[]}
# 推送镜像到私有仓库
docker push 192.168.3.29:5000/centos
# 再次请求查看私有仓库,多个 centos
curl 192.168.3.29:5000/v2/_catalog
{"repositories":["centos"]}
七、docker私有仓库-拉取镜像
1、先删除镜像
docker rmi 192.168.43.216:5000/centos:7
2、从私有仓库拉取镜像
# 拉取镜像:docker pull 私有仓库服务器ip:5000/centos:7
docker pull 192.168.43.216:5000/centos:7
八、docker和虚拟机比较
1、容器就是将软件打包成标准化单元,以用于开发、交付和部署。
1)容器镜像是轻量的、可执行的独立软件包 ,包含软件运行所需的所有内容:代码、运行时环境、系统工具、系统库和设置。
2)容器化软件在任何环境中都能够始终如一地运行。
3)容器赋予了软件独立性,使其免受外在环境差异的影响,从而有助于减少团队间在相同基础设施上运行不同软件时的冲突。
2、docker容器虚拟化 与 传统虚拟机比较
1)相同点:
• 容器和虚拟机具有相似的资源隔离和分配优势
2)不同点:
• 容器虚拟化的是操作系统,虚拟机虚拟化的是硬件。
• 传统虚拟机可以运行不同的操作系统,容器只能运行同一类型操作系统。
3)特性比较
上一节关联链接请点击:
原文地址:https://blog.csdn.net/qfyh_djh/article/details/145231446
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!