docker从零开始-springboot整合Dockerfile

Springboot整合Dockerfile

创建springboot项目
新建maven项目

image-20221220180625033

image-20221220180738202

maven配置springboot依赖
<?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.ohb.springboot</groupId>
    <artifactId>springboot-dockerfile</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.lombok.version>1.18.24</project.lombok.version>
    </properties>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.5</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${project.lombok.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--定义springboot的maven打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
创建springboot项目代码
  1. springboot项目结构

    image-20221220184953840

  2. 启动类

    package com.ohb.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class);
        }
    }
    
  3. dto

    package com.ohb.springboot.dto;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import java.util.Date;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class HttpResp {
    
        private int code;
        private String msg;
        private Object results;
        private Date date;
    }
    
  4. controller

    package com.ohb.springboot.controller;
    
    import com.ohb.springboot.dto.HttpResp;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    
    @RestController
    @RequestMapping("docker")
    public class DockerController {
    
        @GetMapping("/hello")
        public HttpResp hello() {
            return new HttpResp(2001, "success", "使用dockerfile构建springboot项目", new Date());
        }
    }
    
  5. 资源(resource)

    配置文件confg/ application-dev.yml

    server:
      port: 8990
    

    配置文件config/application.yml

    spring:
      profiles:
        active: dev
    

    静态资源 static/index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>springboot-Dockerfile</title>
    </head>
    <body>
        <h1>springboot整合dockerfile文件</h1>
        <a href="docker/hello">访问java后台</a>
        <img src="https://logos-world.net/wp-content/uploads/2021/02/Docker-Logo.png"/>
    </body>
    </html>
    
运行项目

image-20221220190202942

image-20221220190437142

image-20221220190550615


打包springboot项目

image-20221220213903777

image-20221220214339551

image-20221220214414481

idea配置docker
配置docker remote api

配置docker.service 让linux开发Docker Remote API。

  1. 查询配置文件位置

    systemctl show --property=FragmentPath docker

    image-20221220191800500

  2. 使用vim编辑此文件

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service

修改语句

ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375

image-20221220204123051

image-20221220204241498

  1. 重载配置文件
[root@localhost ~]# systemctl daemon-reload
  1. 重启docker

    [root@localhost ~]# systemctl  restart docker
    
  2. 测试2375端口

    [root@localhost ~]# netstat -nplt|grep 2375
    tcp6       0      0 :::2375                 :::*                    LISTEN      30524/dockerd  
    
  3. linux开发2375端口

    [root@localhost ~]# firewall-cmd --zone=public --list-ports
    8000/tcp 445/tcp 8848/tcp 8080/tcp 
    [root@localhost ~]# firewall-cmd --zone=public --add-port=2375/tcp --permanent
    success
    [root@localhost ~]# firewall-cmd --reload 
    success
    [root@localhost ~]# firewall-cmd --zone=public --list-ports
    8000/tcp 445/tcp 8848/tcp 8080/tcp 2375/tcp 
    
  4. 测试配置

    curl localhost:2375/info

    [root@localhost ~]# curl localhost:2375/info
    {"ID":"JWCS:2OUE:KEOL:OW3A:ABKR:AJYV:G3PO:PUQT:6HS2:EBMP:N3KQ:PE5S","Containers":1,"ContainersRunning":0,"ContainersPaused":0,"ContainersStopped":1,"Images":2,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","xfs"],["Supports d_type","true"],["Native Overlay Diff","true"],["userxattr","false"]],"Plugins":{"Volume":["local"],"Network":["bridge","host","ipvlan","macvlan","null","overlay"],"Authorization":null,"Log":["awslogs","fluentd","gcplogs","gelf","journald","json-file","local","logentries","splunk","syslog"]},"MemoryLimit":true,"SwapLimit":true,"KernelMemory":true,"KernelMemoryTCP":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"PidsLimit":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tables":true,"Debug":false,"NFd":27,"OomKillDisable":true,"NGoroutines":37,"SystemTime":"2022-12-20T20:55:10.32641512+08:00","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","CgroupVersion":"1","NEventsListener":0,"KernelVersion":"3.10.0-1160.76.1.el7.x86_64","OperatingSystem":"CentOS Linux 7 (Core)","OSVersion":"7","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","RegistryConfig":{"AllowNondistributableArtifactsCIDRs":[],"AllowNondistributableArtifactsHostnames":[],"InsecureRegistryCIDRs":["127.0.0.0/8"],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":["https://ung2thfc.mirror.aliyuncs.com/"],"Secure":true,"Official":true}},"Mirrors":["https://ung2thfc.mirror.aliyuncs.com/"]},"NCPU":4,"MemTotal":15540805632,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"localhost.localdomain","Labels":[],"ExperimentalBuild":false,"ServerVersion":"20.10.21","Runtimes":{"io.containerd.runc.v2":{"path":"runc"},"io.containerd.runtime.v1.linux":{"path":"runc"},"runc":{"path":"runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"a05d175400b1145e5e6a735a6710579d181e7fb0","Expected":"a05d175400b1145e5e6a735a6710579d181e7fb0"},"RuncCommit":{"ID":"v1.1.4-0-g5fd4c4d","Expected":"v1.1.4-0-g5fd4c4d"},"InitCommit":{"ID":"de40ad0","Expected":"de40ad0"},"SecurityOptions":["name=seccomp,profile=default"],"Warnings":["WARNING: API is accessible on http://0.0.0.0:2375 without encryption.\n         Access to the remote API is equivalent to root access on the host. Refer\n         to the 'Docker daemon attack surface' section in the documentation for\n         more information: https://docs.docker.com/go/attack-surface/"]}
    
    
配置idea docker插件
  1. 打开settings–>docker

    image-20221220210154180

    image-20221220211104800

    image-20221220211145843

  2. 配置docker插件

    image-20221220212051116

    在Services中会出现Docker选项。

    image-20221220212148916

    选中Docker图标,鼠标左键双击会显示容器中的内容。

    image-20221220212353008

项目中创建Dockerfile
  1. 在项目下创建Dockerfile文件

    注意: 文件名第一个字母必须大写D,位置在项目下(与src同级)

    image-20221220213203678

    image-20221220213242108

    image-20221220213342497

  2. 编辑Dockerfile文件内容

    FROM java:8
    ADD target/springboot-dockerfile-1.0-SNAPSHOT.jar /app.jar
    ENTRYPOINT ["java","-jar","/app.jar"]
    EXPOSE 8990
    
  3. 配置Dockerfile

    image-20221220225939231

    image-20221220230131790

    image-20221220230234386

    Deploying 'springboot-dockerfile-demo Dockerfile: Dockerfile'…
    Building image…
    Preparing build context archive…
    [==================================================>]31/31 files
    Done
    
    Sending build context to Docker daemon…
    [==================================================>] 17.60MB
    Done
    
    Step 1/5 : FROM java:8
     ---> d23bdf5b1b1b
    Step 2/5 : MAINTAINER ohb
     ---> Running in e0f0764f6095
    Removing intermediate container e0f0764f6095
     ---> 41b5fe818344
    Step 3/5 : ADD target/springboot-dockerfile-1.0-SNAPSHOT.jar /app.jar
     ---> b985011c3e49
    Step 4/5 : ENTRYPOINT ["java","-jar","/app.jar"]
     ---> Running in c566762d3fc6
    Removing intermediate container c566762d3fc6
     ---> f57d66188fef
    Step 5/5 : EXPOSE 8990
     ---> Running in 3430401b323a
    Removing intermediate container 3430401b323a
     ---> 9506ade75591
    
    Successfully built 9506ade75591
    Creating container…
    Container Id: 9424847664f3b580ac1ce175322bf8f18d1532b394da00f943cbe2625df5ff83
    Container name: 'springboot-dockerfile-demo'
    Starting container 'springboot-dockerfile-demo'
    'springboot-dockerfile-demo Dockerfile: Dockerfile' has been deployed successfully.
    
    
  4. 设置 host与docker容器绑定的端口号

    image-20221220231402804

  5. 查询docker容器是否已经创建

    image-20221220231525948

  6. 测试

linux服务器测试(docker容器装在此服务器)

curl url

image-20221220231802518

从外部访问容器。

image-20221220231619950

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker是一种开源的容器化平台,它可以帮助开发者将应用程序和服务以容器的形式进行打包、分发和部署。而Gluster是一个分布式存储系统,它可以将多个服务器的存储空间整合在一起,形成一个统一的、可扩展的存储池。 CentOS是一种基于Red Hat Enterprise Linux(RHEL)的开源操作系统,它提供了稳定性和安全性,并且具备广泛的应用支持。 Dockerfile是用于构建Docker镜像的脚本文件,它可以定义容器的运行环境、设置启动命令等。 那么,docker-gluster-centos Dockerfile是指在CentOS系统上构建一个包含Gluster分布式存储系统的Docker镜像的脚本文件。 在docker-gluster-centos Dockerfile中,我们可以使用CentOS的官方镜像作为基础镜像,并在此基础上安装和配置Gluster分布式存储系统。可以通过使用适当的软件包管理工具(如yum)来安装Gluster软件包和依赖项。然后,可以使用Dockerfile中的指令来启动和配置Gluster节点、创建Gluster卷以及设置其他必要的参数和选项。 通过构建docker-gluster-centos Docker镜像,我们可以在任何支持Docker的环境中快速部署和运行Gluster分布式存储系统,而无需手动安装和配置。这样可以大大简化Gluster的部署过程,提高运维效率,并且能够实现更加轻量级、可移植和可扩展的存储解决方案。 总而言之,docker-gluster-centos Dockerfile是用于在CentOS系统上构建包含Gluster分布式存储系统的Docker镜像的脚本文件,使得Gluster的部署和运行变得更加简单、可靠和可扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值