springboot构建docker镜像并推送到阿里云

1.构建springboot项目

工程目录如下

springboot构建docker镜像并推送到阿里云_第1张图片

欢迎关注个人公众号【好好学技术】交流学习

UserController

package com.fandf.test.controller;  
  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
  
/**  
* @author fandongfeng  
*/  
@RestController  
@RequestMapping("/api/v1/user")  
public class UserController {  
  
  
    @GetMapping("name")  
    public String getName() {  
        return "zhangsan";  
    }  
  
  
}

application.yml

server:  
  port: 9001

pom.xml

  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <parent>  
        <artifactId>SpringCloudLearninartifactId>  
        <groupId>com.fandfgroupId>  
        <version>1.0.0version>  
    parent>  
    <modelVersion>4.0.0modelVersion>  

    <artifactId>fdf-testartifactId>  

    <properties>  
        <maven.compiler.source>8maven.compiler.source>  
        <maven.compiler.target>8maven.compiler.target>  
    properties>  

    <dependencies>  
        <dependency>  
            <groupId>org.springframework.bootgroupId>  
            <artifactId>spring-boot-starter-webartifactId>  
        dependency>  
    dependencies>  


    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.bootgroupId>  
                <artifactId>spring-boot-maven-pluginartifactId>  
                <executions>  
                    <execution>  
                        <goals>  
                            <goal>repackagegoal>  
                        goals>  
                    execution>  
                executions>  
                <configuration>  
                      
                    <mainClass>com.fandf.test.ApplicationmainClass>  
                configuration>  
            plugin>  
            <plugin>  
                <groupId>io.fabric8groupId>  
                <artifactId>docker-maven-pluginartifactId>  
                <configuration>  
                    <images>  
                        <image>  
                              
                            <name>fandf/${project.name}:${project.version}name>  
                            <alias>${project.name}alias>  
                            <build>  
                              
                                <dockerFile>${project.basedir}/DockerfiledockerFile>  
                                <buildOptions>  
                                      
                                    <network>hostnetwork>  
                                buildOptions>  
                            build>  
                        image>  
                    images>  
                configuration>  
                <executions>  
                    <execution>  
                        <id>docker-execid>  
                          
                        <phase>deployphase>  
                        <goals>  
                            <goal>buildgoal>  
                          
                        goals>  
                    execution>  
                executions>  
            plugin>  
        plugins>  
    build>  
  
project>

插件如果无法下载,可引入依赖


    io.fabric8
    docker-maven-plugin
    0.40.1

2.编写Dockerfile

# 基础镜像使用Java
FROM java:8
# 作者
MAINTAINER fandongfeng
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为app.jar
# 此处可以把具体的jar包名称写出来,我这里直接用*号代替了
ADD target/*.jar app.jar
# 指定容器需要映射到主机的端口
EXPOSE 9091
ENTRYPOINT ["java","-jar","app.jar"]

项目maven plugins会有docker插件,点击docekr build

springboot构建docker镜像并推送到阿里云_第2张图片

镜像已被上传到本地

C:\Users\Administrator>docker images
REPOSITORY                           TAG                                                     IMAGE ID       CREATED             SIZE
fandf/fdf-test                       1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
java                                 8                                                       d23bdf5b1b1b   6 years ago         643MB

3.推送镜像到阿里云

登录阿里云管控台 https://cr.console.aliyun.com/cn-chengdu/instances
选择容器镜像服务

springboot构建docker镜像并推送到阿里云_第3张图片

新建镜像仓库

springboot构建docker镜像并推送到阿里云_第4张图片
springboot构建docker镜像并推送到阿里云_第5张图片

按照提示上传镜像

C:\Users\Administrator>docker login --username=17602117026 registry.cn-chengdu.aliyuncs.com
Password:
Login Succeeded

Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/

C:\Users\Administrator>docker images
REPOSITORY                           TAG                                                     IMAGE ID       CREATED             SIZE
fandf/fdf-test                       1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
java                                 8                                                       d23bdf5b1b1b   6 years ago         643MB

C:\Users\Administrator>docker tag 1a4b935b4d57 registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0

C:\Users\Administrator>docker push registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
The push refers to repository [registry.cn-chengdu.aliyuncs.com/fandf/k8s-test]
83d067b86ae9: Pushed
35c20f26d188: Pushed
c3fe59dd9556: Pushed
6ed1a81ba5b6: Pushed
a3483ce177ce: Pushed
ce6c8756685b: Pushed
30339f20ced0: Pushed
0eb22bfb707d: Pushed
a2ae92ffcd29: Pushed
1.0.0: digest: sha256:44a137e54a48e52252ff9ce64714de8311d206f1ffaf0f6ad39dd01ab1fe0455 size: 2212


C:\Users\Administrator>docker images
REPOSITORY                                        TAG                                                     IMAGE ID       CREATED             SIZE
fandf/fdf-test                                    1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test   1.0.0                                                   1a4b935b4d57   About an hour ago   684MB
java                                              8                                                       d23bdf5b1b1b   6 years ago         643MB

C:\Users\Administrator>

删除掉本地镜像,从阿里云拉取镜像并运行

C:\Users\Administrator>docker images
REPOSITORY                                        TAG                                                     IMAGE ID       CREATED         SIZE
fandf/fdf-test                                    1.0.0                                                   1a4b935b4d57   2 hours ago     684MB
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test   1.0.0                                                   1a4b935b4d57   2 hours ago     684MB
java                                              8                                                       d23bdf5b1b1b   6 years ago     643MB

C:\Users\Administrator>docker rmi registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
Untagged: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
Untagged: registry.cn-chengdu.aliyuncs.com/fandf/k8s-test@sha256:44a137e54a48e52252ff9ce64714de8311d206f1ffaf0f6ad39dd01ab1fe0455

C:\Users\Administrator>docker images
REPOSITORY                           TAG                                                     IMAGE ID       CREATED         SIZE
fandf/fdf-test                       1.0.0                                                   1a4b935b4d57   3 hours ago     684MB
java                                 8                                                       d23bdf5b1b1b   6 years ago     643MB

C:\Users\Administrator>docker pull registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
1.0.0: Pulling from fandf/k8s-test
Digest: sha256:44a137e54a48e52252ff9ce64714de8311d206f1ffaf0f6ad39dd01ab1fe0455
Status: Downloaded newer image for registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0

C:\Users\Administrator>docker images
REPOSITORY                                        TAG                                                     IMAGE ID       CREATED         SIZE
fandf/fdf-test                                    1.0.0                                                   1a4b935b4d57   3 hours ago     684MB
registry.cn-chengdu.aliyuncs.com/fandf/k8s-test   1.0.0                                                   1a4b935b4d57   3 hours ago     684MB
java                                              8                                                       d23bdf5b1b1b   6 years ago     643MB

C:\Users\Administrator>docker run -d -p 9001:9001 registry.cn-chengdu.aliyuncs.com/fandf/k8s-test:1.0.0
495b53c2b820bccd7ec34737a58604838cdbe8e0030b9eed57c63d5f81404bd2

C:\Users\Administrator>curl 127.0.0.1:9001/api/v1/user/name
zhangsan

你可能感兴趣的:(java,docker,spring,boot,阿里云)