springboot dockerfile 构建docker镜像并上传到私有仓库

1.nexus配置:

原nexus界面创建docker仓库,创建hosted
开启远程访问接口:
AA66871E-CCF2-4F08-8741-04D2D90A9B4E.png
开启权限
AAAC11D0-FE1C-48F4-8AC7-B34959A03782.png

2.nginx配置

域名转发nexusip:6000
配置client body size 200M,我在上传镜像的时候会失败,因为超过上传大小了
B120E93F-6520-4049-B8F9-F6F4CD96CB7C.png

3.创建springboot测试项目

pom.xml添加dockerfile打包插件



<properties>
    <java.version>1.8</java.version>
    <docker.registry>docker.xx.com:80</docker.registry>
    
</properties>

<build>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.4.10</version>
            <configuration>
                <tag>${project.version}</tag>
                <repository>${docker.registry}/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
                <username>admin</username>
                <password>yjup666+</password>
            </configuration>
        </plugin>
    </plugins>
</build>

项目目录下创建Dockerfile文件:

#基础镜像,如果本地没有,会从远程仓库拉取。
FROM openjdk:8-jdk-alpine

#镜像的制作人
MAINTAINER wbyu/wbyu@xx.com

#工作目录
WORKDIR /app/

#在容器中创建挂载点,可以多个
VOLUME ["/tmp"]

#声明了容器应该打开的端口并没有实际上将它打开
EXPOSE 8080

#定义参数
ARG JAR_FILE

#拷贝本地文件到镜像中
COPY ${JAR_FILE} app.jar

#指定容器启动时要执行的命令,但如果存在CMD指令,CMD中的参数会被附加到ENTRYPOINT指令的后面
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

创建TestController.java

@RestController
public class TestController {
    @GetMapping
    public String test(){
        return "ok";
    }
}

4.本地docker配置docker私有仓库地址,支持http访问(mac环境)

AE80C247-B975-4BED-9E07-A3E351A597D8.png

5.构建

登录私有仓库docker
docker login http://docker.xx.com:80 -u admin -p "xx"
yuwb@yuwb-mac test % mvn clean package -Dmaven.test.skip=true dockerfile:build
查看本地镜像
yuwb@yuwb-mac test % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.xx.com:80/test v1.0.1 52661a136159 5 seconds ago 121MB
上传到私有仓库
yuwb@yuwb-mac test % docker push docker.xx.com:80/test:v1.0.1
The push refers to repository [docker.iflynote.com:80/test]
c29a69930367: Pushed
1dfc50151c76: Layer already exists
ceaf9e1ebef5: Layer already exists
9b9b7f3d56a0: Layer already exists
f1b5933fe4b5: Layer already exists
v1.0.1: digest: sha256:4f9f8d13c0201737d88790d1c7e749d1c506999ae16bac8c0dba30b8418810c2 size: 1365

6.拉取镜像验证

其他服务器拉取镜像:
docker pull docker.xx.com:80/test:v1.0.1
使用镜像运行容器:
yuwb@yuwb-mac ~ % docker run --name mytest -p 8080:8080 -d docker.xx.com:80/test:v1.0.1
ec06f67d5d0c08faa66936a8bed1358f29628db3be5502a8217d209f59ce7874
yuwb@yuwb-mac ~ % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec06f67d5d0c docker.xx.com:80/test:v1.0.1 "java -Djava.securit…" 24 seconds ago Up 23 seconds 0.0.0.0:8080->8080/tcp mytest

访问接口http://localhost:8080
返回接口:
71C40476-89A2-476A-8FF4-6995A732FA20.png

发表新评论