IDEA构建docker镜像到开发服务器

IDEA 构建docker镜像到开发服务器

centos 7 服务器环境

  1. docker 的安装和配置

    • docker 安装参考docker 安装

    • 配置阿里云加速器

      vi /etc/docker/daemon.json
      

      在这里插入图片描述

    • 开启docker远程访问

      在这里插入图片描述

    • 配置完成后执行以下命令重启docker服务

       systemctl daemon-reload
       systemcyl restart docker
      
  2. docker 仓库的安装

    docker 仓库的安装有官方提供的 registry以及harbor。本文中使用比较简单的repository.

    • 启动一个registry容器,该容器用于提供私有仓库的服务。

      docker run -d -p 5000:5000 --restart=always --name docker-registry -v /var/lib/registry:/var/lib/registry registry:latest
      
    • 通过curl -X GET http://127.0.0.1:5000/v2/_catalog 验证 。 初始返回{“repositories”:[]}说明安装成功。

IDEA 环境

  • 安装 Docker 插件

  • IDEA 中添加配置好的 docker 服务。

    在这里插入图片描述

hello-word-docker 工程

  • 创建一个spring boot web工程hello-word-docker

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.local</groupId>
        <artifactId>hello-world-docker</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.7.RELEASE</version>
            </dependency>
        </dependencies>
    	<build>
            <finalName>${project.name}</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
               <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.1.7.RELEASE</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <target>1.8</target>
                        <source>1.8</source>
                        <encoding>UTF-8</encoding>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
  • application.yml

    server:
      port: 8888
    spring:
      application:
        name: hello-world-docker
    
  • 测试接口

    package com.local.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ProjectName: hello-world-docker
     * @Author: Qiao
     * @Description:
     * @Date: 2020/3/16 20:03
     */
    @RestController
    @RequestMapping("/hello")
    public class Controller {
    
        @GetMapping("/docker")
        public String test() {
            return "hello world docker";
        }
    }
    
    
  • 构建当前项目生成jar包

    在这里插入图片描述

  • 在hello-word-docker工程同级目录下创建 Dockerfile 文件

    FROM anapsix/alpine-java:8_server-jre_unlimited
    
    MAINTAINER 1071635161@qq.com
    
    RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    RUN mkdir -p /hello-world-docker
    
    WORKDIR /hello-world-docker
    
    EXPOSE 8888
    ADD ./target/hello-world-docker.jar ./
    CMD sleep 120;java -Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom -jar hello-world-docker.jar
    
  • 创建并运行Dockerfile
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述在这里插入图片描述

  • 点击运行 hello-world-docker构建镜像到开发服务器

验证

  • docker images 查看镜像是否存在
    在这里插入图片描述
  • docker run -d --name hello-world-docker --restart always -p 8888:8888 hello-world-docker:latest
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K2pzrMex-1584365737504)(C:\Users\qzf\Desktop\container.png)]
  • 浏览器访问http:192.168.204.133:8888/hello/docker
    在这里插入图片描述

结束语

​ 有点累…

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值