首页 > 其他分享 >利用Dockerfile文件和docker 命令 生成docker镜像

利用Dockerfile文件和docker 命令 生成docker镜像

时间:2022-09-22 18:47:03浏览次数:56  
标签:spring org boot springframework import 镜像 docker Dockerfile

 

一、sprinboot部分

1、springboottest 的springboot项目,写了controller测试

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

import java.util.Date;

/*
 *
 *@Description TODO
 *@Author chenzhian
 *@Date 2022/9/21 16:55
 */
@RestController()
public class indexController {


    @Value("${spring.profiles.active:dev}")
    private String active;

    @Value("${smx:dev}")
    private String smx;

    @RequestMapping(method = RequestMethod.GET,value = "/getdate")
    public String getdate() {
        return "active is "+active+" ,"+ new Date().toString();
    }
}

2、SpringBootApplication启动类要注释不检查数据库

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/*
 *
 *@Description 启动类
 *@Author chenzhian
 *@Date 2021/11/11 17:36
 */
@SpringBootApplication(scanBasePackages = {"com.zhianchen.mysqlremark.springboottest"},exclude = DataSourceAutoConfiguration.class)  //
@Slf4j
public class SpringbootTestApplication  {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestApplication.class,args);
    }

}

 

3、pom.xml

<properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
        <spring-boot.version>2.3.9.RELEASE</spring-boot.version>
        <mybatis.version>1.3.2</mybatis.version>
        <mysql.version>8.0.18</mysql.version>
        <lombok.version>1.16.20</lombok.version>
        <fastjson.version>1.2.62</fastjson.version>
        <commons-lang3.version>3.9</commons-lang3.version>

    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
         <!--   <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            &lt;!&ndash;fastjson&ndash;&gt;
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>-->
        </dependencies>
    </dependencyManagement>

 

<dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>

        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.11.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- mybatis代码生成插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>

            </plugin>
    </plugins>
    </build>

 

4、application.yml

server:
  port: 9021
spring:
  application:
    name: com-zhianchen-mysqlremark-springboottest

5、maven 打包成jar

二、docker

1、Docker文件内容

FROM docker.io/ascdc/jdk8
MAINTAINER docker_maven [email protected]
WORKDIR /ROOT
ADD com-zhianchen-mysqlremark-springboottest.jar /ROOT/
["java", "-jar", "-Xms128m", "-Xmx256m", "-XX:+PrintGCDetails", "-XX:+PrintGCTimeStamps", "-XX:+PrintGCTimeStamps", "-XX:+HeapDumpOnOutOfMemoryError", "-XX:HeapDumpPath=/opt/infosec/cssp","-Dspring.profiles.active=test", "com-zhianchen-mysqlremark-springboottest.jar"]
CMD ["java", "-version"]

 

然后把Dockerfile文件和 jar都放在同一个目录,执行

 

docker build -f dockerfile文件路径 -t 镜像名称:版本号 .
例如 docker build -f Dockerfile -t springboottest2:1.0.0 .

 

docker images 查看镜像

 

执行启动,因为我这里指定了版本,所以启动的时候要指定版本,不然会去远程拉last

]# docker run -d -m 512m -p 9022:9021 springboottest2:1.0.0 

因为是我上面写了-d,进行异步执行,所以要看下查看是否运行成功

]# docker ps

网页查看效果

 

标签:spring,org,boot,springframework,import,镜像,docker,Dockerfile
From: https://www.cnblogs.com/zhian/p/16720465.html

相关文章

  • 将镜像上传到 Docker Hub
    登录到DockerHub将映像上传到DockerHub的下一步是登录DockerHub。在命令提示符处,输入下列内容:dockerlogin将映像上传到DockerHub输入以下代码以重新标......
  • Windows中使用Docker安装Redis
    1、拉取Redis以管理员身份运行CMD,执行如下命令拉取Redisdockerpullredis2、在D盘新建目录D:\Net_Program\Net_Docker\Redis 在D盘新建D:\Net_Program\Net_Doc......
  • 使用Watchtower实现Docker容器自动更新
    前言:通常情况下我们手动更新容器的步骤比较繁琐,需要四个步骤:1.停止容器2.删除容器3.检查镜像更新情况,更新镜像4.重新启动容器容器少还无所谓,但要是需要更新大量的......
  • 获取阿里云docker加速器地址
    1.登录阿里云官网https://www.aliyun.com/2.注册账号登录  3.产品-->容器与中间件-->容器镜像服务  4、管理控制台  5、镜像工具-->镜像加速器 ......
  • 服务器里面运行一个项目用docker容器
    先建一个文件docker-compose.yml#使用说明V3.3.0#1.使用docker-compose宿主机不需要配置host来发现#2.无需修改源码,根目录docker-composeup即可#3.静静等......
  • Docker安装Jenkins
    Docker安装Jenkins目录Docker安装Jenkins准备工作下载Jenkins镜像开始安装创建需要挂载的本地文件夹创建并启动Container打开jenkens安装插件创建用户准备工作下载Jenk......
  • 在Linux环境docker推送镜像到harbor仓库
    比如要把nginx这个镜像推送到hardor仓库[root@test23:00:14~/docker]#dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEnginx......
  • Docker常用命令总结
    Docker常用命令重启docker命令systemctlrestartdocker进入镜像dockerexec-itmysql/bin/bash移除指定容器dockerrm-f<containerid>删除未启动成......
  • Docker更新macvlan网段地址
    需求公司网络升级,原有网段发生改变,独立ip的macvlan容器无法访问。解决无需重建容器,按步骤更新网络即可。#断开连接dockernetworkdisconnectmacnetmy_container#......
  • 用Alpine 3.14镜像docker build 时出现make: /bin/sh: Operation not permitted 问题
    docker构建镜像时发现了如下的问题,翻了翻github,原因是docker版本低了/usr/src/lua-5.1.5#makelinuxcdsrc&&makelinuxmake:/bin/sh:Operationnotpermittedma......