目录
1.Dockerfile制作微服务镜像
在之前配置的Jenkins服务器中配置docker环境,还再需要配置一个harbor服务。
我们利用Dockerfile制作一个Eureka注册中心的镜像。
[root@jenkins ~]# mkdir test
[root@jenkins ~]# cd test/
编写Dockerfile
[root@jenkins test]# vim Dockerfile
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 10086
ENTRYPOINT ["java","-jar","/app.jar"]
构建镜像
[root@jenkins test]# docker build --build-arg JAR_FILE=tensquare_eureka_server-1.0-SNAPSHOT.jar -t eureka:v1 .
Sending build context to Docker daemon 44.76MB
Step 1/5 : FROM openjdk:8-jdk-alpine
8-jdk-alpine: Pulling from library/openjdk
e7c96db7181b: Pull complete
f910a506b6cb: Pull complete
c2274a1a0e27: Pull complete
查看镜像是否创建成功
[root@jenkins test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
eureka v1 8285b5478a5c About a minute ago 150MB
创建容器
[root@jenkins test]# docker run -i --name=eureka -p 10086:10086 eureka:v1
访问容器
2.环境准备Harbor镜像仓库安装及使用
Harbor安装
Harbor需要安装在另一台服务器上面,需安装好docker环境。
再安装docker-compose
[root@harbor ~]# rz -E #上传docker-compose文件
[root@harbor ~]# ls
docker-compose
[root@harbor ~]# chmod +x docker-compose
[root@harbor ~]# mv docker-compose /usr/local/bin/
查看docker-compose是否安装成功
[root@harbor ~]# docker-compose -version
docker-compose version 1.21.1, build 5a3f1a3
安装Harbor,上传压缩包到linux,并解压
[root@harbor ~]# rz -E
[root@harbor ~]# tar -xzvf harbor-offline-installer-v1.9.2.tgz -C /opt/
修改Harbor的配置文件
[root@harbor ~]# cd /opt/harbor/
hostname: 192.168.100.133 #修改本地地址
port: 85 #默认端口易冲突,改个不常用的端口,这里设置的85
安装Harbor,(docker处于启动状态)
[root@harbor harbor]# ./prepare
[root@harbor harbor]# ./install.sh
访问Harbor,默认账户密码:admin/Harbor12345
创建新项目
创建用户
找到项目,在项目中设置用户权限
把镜像上传到Harbor
在Jenkins服务器上设置
先把Harbor地址加入到Docker信任列表
[root@jenkins ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://zydiol88.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.100.133:85"]
}
[root@jenkins ~]# systemctl restart docker
再登录Harbor,再推送镜像
[root@jenkins test]# docker login -u zch -p Zch12345 192.168.100.133:85 #在Harbor创建用的账号和密码
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded #显示登录成功
给上传的镜像打上标签
[root@jenkins test]# docker tag eureka:v1 192.168.100.133:85/tensquare/eureka:v1
[root@jenkins test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.100.133:85/tensquare/eureka v1 8285b5478a5c About an hour ago
推送镜像
[root@jenkins test]# docker push 192.168.100.133:85/tensquare/eureka:v1
The push refers to repository [192.168.100.133:85/tensquare/eureka]
ae34754416a2: Pushed
ceaf9e1ebef5: Pushed
9b9b7f3d56a0: Pushed
f1b5933fe4b5: Pushed
查看Harbor仓库
从Harbor下载镜像
在另一台docker服务器上下载镜像
配置docker环境
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://zydiol88.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.100.133:85"]
}
[root@docker ~]# systemctl restart docker
登录镜像仓库
[root@docker ~]# docker login -u zch -p Zch12345 192.168.100.133:85 #指定登录用户和密码
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
下载镜像,命令可以直接在Harbor仓库有粘贴
[root@docker ~]# docker pull 192.168.100.133:85/tensquare/eureka:v1
v1: Pulling from tensquare/eureka
e7c96db7181b: Pull complete
f910a506b6cb: Pull complete
c2274a1a0e27: Pull complete
749319c144b9: Pull complete
Digest: sha256:383295e79bee0cd8912c9bbe4b876931c50834b02306bded65468f29e4467921
Status: Downloaded newer image for 192.168.100.133:85/tensquare/eureka:v1
192.168.100.133:85/tensquare/eureka:v1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.100.133:85/tensquare/eureka v1 8285b5478a5c 2 hours ago 150MB