Reprint: Use the docker-maven-plugin plugin to compile the project into a docker image to a remote linux server

Reprinted: http://blog.csdn.net/lvyuan1234/article/details/69255944

 

Use idea to develop a modular maven project in win10, and then want to compile the project directly into the docker of the remote linux server, the specific method is as follows:


       First: add the following to the pom file in each module

 

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
        <imageName>${project.name}:${project.version}</imageName>
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <skipDockerBuild>false</skipDockerBuild>
        <resources>
            <resource>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

 

       Second: Create a new docker package under src/main of each module, and create a new Dockerfile and runboot.sh file under src/main/docker (the content of the file will not be posted due to different situations), the structure is as follows

 

       Third: Install docker on the linux server. My linux version is centos7. It is not recommended to use the #yum install docker method to install, because the version installed in this way is relatively old, and there are many errors when configuring the remote api. It is recommended to use Way

#curl -fsSL https://get.docker.com/ | sh或#yum install docker-engine,这里以17.0.3-ce版本为例,使用

#docker version命令即可验证是否安装成功,接下来要想在windows中操作远程linux中的docker,那前提是必须

开启docker远程API,修改docker配置文件#vi /usr/lib/systemd/system/docker.service   ,进入编辑模式后,将ExecStart这一行后面加上-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock ,改完后如下所示

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock  ,这里就写4个0,你可别改成自己的ip哦,保存后退出,重新加载配置文件#systemctl daemon-reload   ,启动docker #systemctl start docker  ,

输入#netstat -anp|grep 2375 显示docker正在监听2375端口,输入#curl 127.0.0.1:2375/info  显示一大堆信息,证明远程api就弄好了

 

       第四:在windows系统环境变量中新建DOCKER_HOST,值为tcp://10.100.74.220:2375,(你改成你自己的docker服务器ip地址)

 

       第五步:打开dos窗口,即cmd命令行,进入到你要编译的项目文件夹下,

输入mvn clean package docker:build -DskipTests ,然后慢慢等待,直到最后build成功

 

       第六步:登陆linux,输入#docker images 发现自己的项目已经被编译成镜像了,可以启动容器运行镜像了,也相当于完成了项目的云部署

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394163&siteId=291194637