构建私有镜像仓库
私有镜像仓库
开通镜像服务
登录阿里云开通容器镜像服务。点击管理控制台-》初次使用会提示开通,然后设置密码。
点击管理
# 登录阿里云Docker Registry
docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
# 从Registry中拉取镜像(拉取)
[root@localhost ~]# docker pull registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:flask-hello-world
flask-hello-world: Pulling from abc1024/soulboy_images
[root@localhost ~]# docker run -d -p 5000:5000 registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:flask-hello-world
Digest: sha256:8c4f6441a730d5abff5838bc5faa53ae97b3212fda67d6944cb75f76af1804be
# 将镜像推送到Registry(推送)
docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
# 将本地镜像8365e912bdbd 标记为 registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:flask-hello-world
docker tag 8365e912bdbd registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:flask-hello-world
# 推送镜像到阿里云镜像仓库
docker push registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:flask-hello-world
The push refers to repository [registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images]
54d6fee42f0f: Pushed
9bcf7e016add: Pushed
ee0ebee0324c: Pushed
a30cfc27bb5d: Pushed
d22688b29911: Pushed
08127eb3eb76: Pushed
a637c551a0da: Pushed
2c8d31157b81: Pushed
7b76d801397d: Pushed
f32868cde90b: Pushed
0db06dff9d9a: Pushed
flask-hello-world: digest: sha256:8c4f6441a730d5abff5838bc5faa53ae97b3212fda67d6944cb75f76af1804be size: 2640
构建 SpringBoot 应用 docker 镜像
启动类添加 Controller 处理逻辑。http://localhost:8080/user/find
@SpringBootApplication
@Controller
public class DockerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(DockerDemoApplication.class, args);
}
@RequestMapping("/user/find")
@ResponseBody
public Object findUser(){
Map<String, String > map = new HashMap<>();
map.put("name", "soulboy");
map.put("age","29");
return map;
}
}
maven 里面添加配置 pom.xml
Spotify 的 docker-maven-plugin 插件是用 maven 插件方式构建 docker 镜像的。
${project.build.finalName} ,缺省为$ {project.artifactId}-${project.version}
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<docker.image.prefix>soulboy</docker.image.prefix>
</properties>
<build>
<!--项目打包的名称-->
<finalName>docker-demo</finalName>
<plugins>
<!-- springboot插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- docker插件:将springboot应用打包成docker镜像-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
打包 SpringCloud 镜像并上传私有仓库并部署
Dockerfile : 由一系列命令和参数构成的脚本,这些命令应用于基础镜像, 最终创建一个新的镜像
创建 Dockerfile:默认是根目录,(可以修改为 src/main/docker/Dockerfile,如果修则需要制定路径)
在项目右键创建 Dockerfile
# FROM <image>:<tag> 需要一个基础镜像,可以是公共的或者是私有的,后续构建会基于此镜像,如果同一个Dockerfile中建立多个镜像时,可以使用多个FROM指令
FROM openjdk:8-jdk-alpine
# VOLUME 配置一个具有持久化功能的目录,主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp。改步骤是可选的,如果涉及到文件系统的应用就很有必要了。/tmp目录用来持久化到 Docker 数据文件夹,因为Spring Boot 使用的内嵌 Tomcat 容器默认使用/tmp作为工作目录
VOLUME /tmp
# ARG 设置编译镜像时加入的参数, ENV 是设置容器的环境变量
ARG JAR_FILE
# COPY : 只支持将本地文件复制到容器,并重命名为app.jar,还有个ADD更强大但复杂点
COPY ${JAR_FILE} app.jar
# ENTRYPOINT 容器启动时执行的命令
ENTRYPOINT ["java","-jar","/app.jar"]
点击 Terminal
Microsoft Windows [版本 10.0.17763.107]
(c) 2018 Microsoft Corporation。保留所有权利。
F:\Project\ImageTest\docker-demo>mvn install dockerfile:build
将打包好的本地镜像 push 到远程的私有仓库(阿里云)
# 查看本地镜像发现 xdclass/docker-demo 已经打包完成
C:\Users\soulboy>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xdclass/docker-demo latest efb549968619 13 seconds ago 121MB
openjdk 8-jdk-alpine a3562aa0b991 2 months ago 105MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
# 登录鉴权
docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
# 将本地镜像打标记
docker tag efb549968619 registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:docker-demo
# 推送镜像到阿里云镜像仓库
docker push registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:docker-demo
在阿里云容器镜像管理查看
从阿里云容器镜像服务拉取镜像到本地 http://192.168.31.22/user/find
docker login --username=soulboy1990116 registry.cn-shanghai.aliyuncs.com
docker pull registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:[镜像版本号]
docker pull registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:docker-demo
docker run -d -p 80:8080 registry.cn-shanghai.aliyuncs.com/abc1024/soulboy_images:docker-demo