美文网首页Docker
Docker将JAVA程序制作成镜像

Docker将JAVA程序制作成镜像

作者: WebGiser | 来源:发表于2020-04-22 21:44 被阅读0次

在IDEA中编写的JAVA程序,maven打包后,部署到docker,主要有以下几种方式:
1、maven打成jar包,上传服务器。编写Dockerfile后,使用命令制作镜像
2、IDEA使用docker插件和dockerfile-maven-plugin插件打包镜像

1、dockfile制作jar包镜像

1、在IDEA中编写java程序,并maven打成jar包。注意application.properties中变量的写法。
image.png image.png
2、将jar包上传服务器,并在同级目录下编写Dockfile文件

其中gmaslowski/jdk:8是docker images里已有的一个jdk镜像


image.png
FROM gmaslowski/jdk:8
CMD ["/bin/sh","-c","mkdir","-p","/opt/wzf"]
ADD hello.jar /opt/wzf/hello.jar
ENTRYPOINT ["java","-jar","/opt/wzf/hello.jar"]
3、docker构建镜像,并运行容器
//-t指定镜像名称和tag
docker  build  -t  hello:1.0  .
//运行容器
docker run -d -e NAME=WZF -e AGE=100 -p 8081:8080 hello:latest
4、接口访问测试
image.png

2、docker插件打包镜像

1、IDEA安装插件

打开IDEA,点击file->settings->输入plugins,在搜索框里输入docker,下载安装docker插件


image.png
2、虚拟机开放docker的2375端口

打开已经安装了docker的虚拟机centos7,执行

vi  /lib/systemd/system/docker.service
image.png

在ExecStart后面增加

-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

重启docker

systemctl daemon-reload
service docker restart | systemctl restart docker
3、配置docker服务

在IDEA的settings窗口搜索栏输入docker,Engine API URL配置的是你远程或者本地docker服务器的ip地址和端口号,配置完成后下方会出现connection successful提示,如果没出现,一般是Engine API URL没有配置好,自己解决


image.png
4、编写SpringBoot程序和Dockfile文件
image.png

pom.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hello</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!--docker仓库地址-->
        <docker.repostory>192.168.43.178:2375</docker.repostory>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <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.3.7</version>
<!--                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>-->
                <configuration>
                    <repository>${docker.repostory}/${project.artifactId}</repository>
                    <tag>latest</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.properties代码:

name=${NAME:aaa}
age=${AGE:20}

HelloController.java代码:

package com.hello.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Value("${name}")
    private String name;

    @Value("${age}")
    private int age;

    @GetMapping("/print")
    public String print(){
        return "名字:"+name +",年龄:"+ age;
    }
}

Dockerfile代码

FROM gmaslowski/jdk:8
ARG JAR_FILE
ADD ${JAR_FILE} hello.jar
ENTRYPOINT ["java","-jar","/hello.jar"]
5、程序打包镜像并部署容器

在windows环境变量中设置环境变量DOCKER_HOST:


image.png

在maven的settings.xml中配置白名单


image.png

maven打包命令:

mvn clean package

dockerfile制作镜像命令:

mvn dockfile:build

或者是


image.png

相关文章

  • Docker将JAVA程序制作成镜像

    在IDEA中编写的JAVA程序,maven打包后,部署到docker,主要有以下几种方式:1、maven打成jar...

  • Docker

    构建镜像仓库 Docker运行java程序 1.1.6 搭建docker私有仓库 新建私有仓库 1.2.1 数据挂...

  • Docker常规命令

    镜像常用命令 搜索镜像 docker search java 下载镜像 docker pull java 列举已经...

  • Docker-容器

    应用程序打包生产镜像后,上传到Docker仓库,再从Docker仓库将镜像下载到本地,以镜像为模板可以创建容器,容...

  • Docker笔记

    Docker笔记 Docker 镜像常用命令 搜索镜像 docker search java 下载镜像 docke...

  • Docker部署Rstudio Server【四】:订制自己的个

    订制自己专属镜像,保存、复制自己程序的运行环境。 使用docker commit将容器保存为镜像 这个命令可以将d...

  • centos 安装 docker register

    docker register 是docker的镜像仓库,可以将 docker 镜像存放到其中。register ...

  • Docker-镜像

    1.镜像是什么 简单来说,Docker镜像就是将环境依赖或者应用程序以Docker的规范和形式打包后的文件,可以用...

  • Docker笔记2-In-Container Java Dev

    概述 这里将创建一个简单的SpringBoot项目,制作成Docker镜像,并实现对容器内项目的远程调试。 创建S...

  • Docker 部署 Spring Boot 应用程序

    文章简述 通过 Dockerfile 文件将 Spring Boot 程序构建为 Docker 镜像文件,并通过命...

网友评论

    本文标题:Docker将JAVA程序制作成镜像

    本文链接:https://www.haomeiwen.com/subject/rkjgihtx.html