目录
一、安装Docker环境和Docker-compose环境
1、安裝docker
#更新yum源
yum -y update
#安装软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
#设置镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#查看版本列表
yum list docker-ce-cli --showduplicates|sort -r
#指定安装版本
yum install --setopt=obsoletes=0 docker-ce-20.10.9-3.el7 -y
#启动docker
systemctl start docker
#设置开机启动
systemctl enable docker
#检测安装是否成功
docker version
2、安裝dockers-compose
#下载安装包
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#授权
sudo chmod +x /usr/local/bin/docker-compose
#检测是否安装成功
docker-compose --version或者docker-compose -v
二、创建证书
#创建文件存储证书文件
mkdir /data/docker-registry/conf -p
#生成证书
openssl req -new -newkey rsa:4096 -days 365 -subj "/CN=localhost" \
-nodes -x509 -keyout /data/docker-registry/conf/auth.key -out /data/docker-registry/conf/auth.cert
三、创建配置文件
1、在上面的conf的目录下创建文件 registry-srv.yml(配置registry)和 registry-web.yml(配置web)
registry-srv.yml
version: 0.1
storage:
filesystem:
rootdirectory: /var/lib/registry
http:
addr: 0.0.0.0:5000
auth:
token:
# 验证密码的路径
realm: http://192.168.139.178:8080/api/auth
service: 192.168.139.178:5000
issuer: 'admin'
# 证书的路径
rootcertbundle: /etc/docker/registry/auth.cert
registry-web.yml
registry:
url: http://192.168.139.178:5000/v2 #对应自己的ip
name: 192.168.139.178:5000
readonly: false
auth:
enabled: true
issuer: 'admin'
key: /conf/auth.key
2、在 上面的conf的目录下创建目录db
mkdir db -p
四、Docker方式启动容器
1、启动registry-srv
docker run -d \
-v /data/docker-registry/conf/registry-srv.yml:/etc/docker/registry/config.yml:ro \
-v /data/docker-registry/conf/auth.cert:/etc/docker/registry/auth.cert:ro -p 5000:5000 \
--name registry-srv registry:2
2、启动docker-registry-web
docker run -d -it -p 8080:8080 \
-v /data/docker-registry/conf/registry-web.yml:/conf/config.yml:ro \
-v /data/docker-registry/conf/auth.key:/conf/auth.key \
-v /data/docker-registry/conf/db:/data \
--link registry-srv --name registry-web hyper/docker-registry-web:latest
3、访问http://ip:8080 默认账号密码 admin/admin
五、 Docker-compose方式启动
1、在对应目录下(安装docker-conpose配置的路径/usr/local/bin/docker-compose)创建 docker-compose.yml文件 。
version: '2'
services:
registry-web-v2:
image: hyper/docker-registry-web:latest
ports:
- 8080:8080
volumes:
- /data/docker-registry/conf/registry-web.yml:/conf/config.yml:ro
- /data/docker-registry/conf/auth.key:/conf/auth.key
- /data/docker-registry/conf/db:/data
networks:
- registry-net
depends_on:
- registry-srv-v2
restart: always
registry-srv-v2:
image: registry:2
ports:
- 5000:5000
volumes:
- /data/docker-registry/conf/registry-srv.yml:/etc/docker/registry/config.yml:ro
- /data/docker-registry/conf/auth.cert:/etc/docker/registry/auth.cert:ro
environment:
- REGISTRY_STORAGE_DELETE_ENABLED=true
networks:
- registry-net
restart: always
networks:
registry-net:
2、启动
docker-compose -f docker-compose.yml up -d
注意:我的docker-compose.yml文件端口号,没改,是因为我删掉了docker方式安装的容器,如果你没删,请记得修改端口号,不然显示端口号被占用。
3、添加用户
六、上传镜像文件到私有仓库
1、登录
#登录
docker login 192.168.139.178:5000
此时会报错
Error response from daemon: Get "https://192.168.139.178:5000/v2/": http: server gave HTTP response to HTTPS client
解决方法
#修改下面的文件
vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://3kcpregv.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.139.178:5000"] #新增这句,多个地址用逗号隔开
}
# 重新加载配置文件
systemctl daemon-reload
# 重新启动 docker
systemctl restart docker
2、再重新登录,然后上传mysql镜像到私服,如果上传自己的项目,请先打包成镜像
#tag,其中tag后面是镜像id
docker tag 3218b38490ce 192.168.139.178:5000/mysql/mysql-v1
#上传镜像
docker push 192.168.139.178:5000/mysql/mysql-v1
#拉取镜像
docker pull 192.168.139.178:5000/mysql/mysql-v1:1
3、结果是这个样子
拉取的镜像
4、删除仓库
#删除仓库 d9b12dc2e25b 为容器id
docker exec d9b12dc2e25b rm -rf /var/lib/registry/docker/registry/v2/repositories/mysql/mysql-v1
七、构建SpringBoot项目为镜像推送到远程仓库
构建镜像 参考 Docker+Jenkins+Gitee+Maven构建自动化部署_熟透的蜗牛的博客-CSDN博客_docker gitee 自动化部署
1、使用maven插件推送镜像
maven的settings文件添加如下配置
<server>
<id>docker-repo</id>
<username>admin</username>
<password>admin</password>
</server>
2、pom文件添加docker插件
<!--构建项目到maven私服的配置-->
<distributionManagement>
<repository>
<id>snail-release</id>
<url>http://192.168.139.169:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snail-snapshots</id>
<url>http://192.168.139.169:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<finalName>jenkins</finalName>
<plugins>
<!-- Srping Boot 打包工具 打包成可执行的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.0</version>
<configuration>
<mainClass>com.xiaojie.JenkinsApp</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--打包后 复制jar包到指定文件目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 复制资源后的输出目录 -->
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>src/main/docker</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--Docker 插件-->
<!-- docker插件 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 -->
<serverId>${docker.serverId}</serverId>
<!-- docker私服地址 -->
<registryUrl>${docker.registry}</registryUrl>
<!-- 指定docker server的地址,该配置不需要本机安装docker -->
<dockerHost>${docker.host}</dockerHost>
<imageName>${project.name}/${project.artifactId}:${project.version}</imageName>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<!-- docker的构建目录(构建上下文),包含Dockerfile -->
<dockerDirectory>target</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<!-- package之前清除上一次构建的image -->
<execution>
<id>remove-image</id>
<phase>package</phase>
<goals>
<goal>removeImage</goal>
</goals>
<configuration>
<imageName>
${project.name}/${project.artifactId}
</imageName>
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
</configuration>
</execution>
<!-- package时执行build -->
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
<configuration> <imageName>${project.name}/${project.artifactId}:${project.version}</imageName>
</configuration>
</execution>
<!-- package时执行tag 修改镜像名称 -->
<execution>
<id>tag-image</id>
<phase>package</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<!-- docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG] -->
<!-- images与IMAGE[:TAG]对应,必须在build阶段已经构建了 -->
<image>${project.name}/${project.artifactId}:${project.version}</image>
<!-- newName与tag命令的第二个参数对应 -->
<newName>
${docker.registry}/${project.name}/${project.artifactId}:${project.version}
</newName>
</configuration>
</execution>
<!--执行deploy时 推送镜像 -->
<execution>
<id>push-image</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>
${docker.registry}/${project.name}/${project.artifactId}:${project.version}
</imageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行 mvn clean package 时 会执行docker 的build、tag命令
执行 mvn deploy 时,会执行 docker 的build、tag、push命令
注意 :如果报2375端口连接不上执行下面命令
vim /usr/lib/systemd/system/docker.service
修改 在这行 ExecStart=。。。。后面添加 -H tcp://0.0.0.0:2375