Docker 高级 Docker微服务实战

1、通过IDEA新建一个普通微服务模块

1、建Module

docker_boot

2、改POM

<?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.5.6</version>
        <relativePath/>
    </parent>

    <groupId>com.atguigu.docker</groupId>
    <artifactId>docker_boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mapper.version>4.1.5</mapper.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>


    <dependencies>
        <!--SpringBoot通用依赖模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
        </plugins>
    </build>

</project>


3、写YML

application.properties

server.port=6001

4、主启动

package com.atguigu.docker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author String-int
 * @create 2022-04-19 0:28
 */
@SpringBootApplication
public class DockerBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(DockerBootApplication.class, args);
    }

}

5、业务类

package com.atguigu.docker.controller;

/**
 * @author String-int
 * @create 2022-04-19 0:29
 */

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.UUID;

@RestController
public class OrderController {
    @Value("${server.port}")
    private String port;

    @RequestMapping("/order/docker")
    public String helloDocker() {
        return "hello docker" + "\t" + port + "\t" + UUID.randomUUID().toString();
    }

    @RequestMapping(value = "/order/index", method = RequestMethod.GET)
    public String index() {
        return "服务端口号: " + "\t" + port + "\t" + UUID.randomUUID().toString();
    }
}

把上面的项目打成jar包

新建一个文件夹

[root@630ab08a6973 /]# mkdir mydocker
[root@630ab08a6973 /]# pwd
/
[root@630ab08a6973 /]# cd mydocker
[root@630ab08a6973 mydocker]# pwd
/mydocker
[root@630ab08a6973 mydocker]# 
[root@630ab08a6973 mydocker]# ll
total 0
[root@630ab08a6973 mydocker]# 

将jar上传到mydocker文件夹下

[root@localhost mydocker]# ll
总用量 19060
-rw-r--r--. 1 root root 19514767 4月  19 00:47 docker_boot-0.0.1-SNAPSHOT.jar
[root@localhost mydocker]# 

2、通过dockerfile发布微服务部署到docker容器

1、IDEA工具里面搞定微服务jar包

docker_boot-0.0.1-SNAPSHOT.jar

2、编写Dockerfile

Dockerfile内容

# 基础镜像使用java 。FROM java:8不行得话换成 FROM openjdk:8
FROM java:8
# 作者
MAINTAINER zzyy
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为zzyy_docker.jar
ADD docker_boot-0.0.1-SNAPSHOT.jar zzyy_docker.jar  #docker_boot-0.0.1-SNAPSHOT.jar 原来的名字,改成这个名字zzyy_docker.jar
# 运行jar包
RUN bash -c 'touch /zzyy_docker.jar'
ENTRYPOINT ["java","-jar","/zzyy_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 6001
 

将微服务jar包和Dockerfile文件上传到同一个目录下/mydocker

[root@localhost mydocker]# pwd
/mydocker
[root@localhost mydocker]# ll
总用量 19060
-rw-r--r--. 1 root root 19514767 4月  19 00:47 docker_boot-0.0.1-SNAPSHOT.jar
drwxr-xr-x. 2 root root       19 4月  19 01:05 u
[root@localhost mydocker]# vim Dockerfile
[root@localhost mydocker]# 
[root@localhost mydocker]# ll
总用量 19064
-rw-r--r--. 1 root root 19514767 4月  19 00:47 docker_boot-0.0.1-SNAPSHOT.jar
-rw-r--r--. 1 root root      455 4月  19 01:14 Dockerfile
drwxr-xr-x. 2 root root       19 4月  19 01:05 u
[root@localhost mydocker]# 

3、构建镜像

docker build -t zzyy_docker:1.6 .  #镜像名字zzyy_docker 版本号是1.6
[root@localhost mydocker]# docker build -t zzyy_docker:1.6 .
Sending build context to Docker daemon  19.52MB
Step 1/7 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/7 : MAINTAINER zzyy
 ---> Running in c1d196eb32bb
Removing intermediate container c1d196eb32bb
 ---> f332e65b90b4
Step 3/7 : VOLUME /tmp
 ---> Running in fddc0c90ca6a
Removing intermediate container fddc0c90ca6a
 ---> 8ef8137c1551
Step 4/7 : ADD docker_boot-0.0.1-SNAPSHOT.jar zzyy_docker.jar
 ---> c70f0b845c53
Step 5/7 : RUN bash -c 'touch /zzyy_docker.jar'
 ---> Running in 5a9db31aa63a
Removing intermediate container 5a9db31aa63a
 ---> 4378f0b4fc9b
Step 6/7 : ENTRYPOINT ["java","-jar","/zzyy_docker.jar"]
 ---> Running in 98b516524ec7
Removing intermediate container 98b516524ec7
 ---> 416cffc8e151
Step 7/7 : EXPOSE 6001
 ---> Running in 8338723f3cdb
Removing intermediate container 8338723f3cdb
 ---> c366639d316c
Successfully built c366639d316c # Successfully 表示成功
Successfully tagged zzyy_docker:1.6
[root@localhost mydocker]# 

打包成镜像文件
在这里插入图片描述

4、运行容器

 docker run -d -p 6001:6001 zzyy_docker:1.6
[root@localhost mydocker]# docker run -d -p 6001:6001 c366639d316c

91a48ac55a211bd2c808eaa6183621cf8e2bb43328112934444a827bf5d4c5bf
[root@localhost mydocker]# 
[root@localhost mydocker]# 

在这里可能会出问题就是防火墙没关

关闭防火墙再重启docker

[root@localhost mydocker]# systemctl stop firewalld # 关闭
[root@localhost mydocker]# systemctl restart docker #重启docker
[root@localhost mydocker]# docker run -d -p 6001:6001 c366639d316c #再次执行运行容器

5、访问测试

方法一、在Windows上
http://CentOs7的IP地址:6001/order/docker

http://CentOs7的IP地址:6001/order/index

方法二、在CentOs7上
curl 127.0.0.1:6001/order/docker

curl 127.0.0.1:6001/order/index

[root@localhost mydocker]# curl 127.0.0.1:6001/order/docker
hello docker	6001	d5e9ea87-3a04-4e11-b5b0-996dbdb4e8b7[root@localhost mydocker]# 

你可能感兴趣的:(docker,docker)