Docker网络模式

1. Docker网络模式
   Docker在创建容器时有四种网络模式:bridge/host/container,bridge为默认不需要用--net去指定,
   其他三种模式需要在创建容器时使用--net去指定

    ##  单机模式
   1.bridge模式(默认模式)
     docker run时使用--net=bridge,这种模式会为每个容器分配一个独立的Network Namespace,
     同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的

     注1:bridge为默认模式,不需要使用参数--net去指定,使用了--net参数反而无效
     注2:bridge模式无法指定容器IP(但非绝对,还是可以修改滴,具体操作可参考资料/10)

   2.host模式
     docker run时使用--net=host,容器将不会虚拟出IP/端口,而是使用宿主机的IP和端口

     docker run -itd --net=host 961769676411

     注1:host模式不能使用端口映射和自定义路由规则,这些都与主机一致,-p 与-icc 参数是无效的

   3.container模式(略)

   4.none模式(略)

   ## 集群模式
   5.跨主机通信(略)
     以上四种均未跨主机,也就是说容器均运行在一台宿主机上,但实际生产环境不可能只用一台来跑。
     肯定会用到多台,那么多台主机之间的容器如何通信 
     1.使用路由机制打通网络
     2.使用Open vSwitch(OVS)打通网络
     3.使用flannel来打通网络
     4.使用Quagga来实现自动学习路由  
     注1:详情见资料/01


docker inspect bridge:查看网络模式信息

2. 外部访问docker容器
   1.bridge模式
     docker run -itd -p 7001:7001 镜像ID
     ## -p参数可以出现多次,绑定多个端口号
     docker run -itd -p 8080:8080 -p 8088:8088 镜像ID

     当使用 -P 标记时,Docker 会随机映射一个 49000~49900 的端口到内部容器开放的网络端口

   2.host模式
     docker run -itd --net=host 镜像ID

     注1:不需要添加-p参数,因为它使用的就是主机的IP和端口,添加-p参数后,反而会出现以下警告:
          WARNING: Published ports are discarded when using host network mode
     注2:宿主机的ip路由转发功能一定要打开,否则所创建的容器无法联网! 
          echo 1 > /proc/sys/net/ipv4/ip_forward

   3.相关命令
     #停止并删除所有容器
     docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

   4.网桥查看工具bridge-utils 
     apt install bridge-utils
     brctl show


3. 创建自定义网络:(设置固定IP)
   1.创建自定义网络
     docker network create --subnet=172.18.0.0/16 mynetwork
     # 查看网络配置 
     docker network ls

   2.创建Docker容器
     docker run -itd --name networkTest1 --net mynetwork --ip 172.18.0.2 centos:latest /bin/bash

     firewall-cmd --zone=public --add-port=8080/tcp --permanent
     firewall-cmd --reload && firewall-cmd --list-port

   localhost:8080
   虚拟机的ip地址:8080

附录一:eth0 eth0:1 eth0.1 的区别
eth0 eth0:1 和eth0.1三者的关系对应于物理网卡、子网卡、虚拟VLAN网卡

附录二:veth是什么?
Linux container 中用到一个叫做veth的东西,这是一种新的设备,专门为 container 所建。veth 从名字上来看是 
Virtual ETHernet(虚拟网络设备)的缩写,它的作用很简单,就是要把从一个 network namespace 发出的数据包转发到另一个 namespace。
veth 设备是成对的,一个是 container 之中,另一个在 container 之外,即在真实机器上能看到的

附录三:停止并删除所有容器
#1停止所有容器
docker stop $(docker ps -aq)
#2删除所有的容器
docker rm $(docker ps -aq)
#4停止并删除所有容器
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

附录四:docker run时参数-P和-p的区别
-P 标记时,Docker 会随机映射一个 49000~49900 的端口到内部容器开放的网络端口

附录五:ngix镜像

附录六:docker参数--restart=always的作用,使容器自启动
创建容器时没有添加参数  --restart=always ,导致的后果是:当 Docker 重启时,容器未能自动启动。

docekr ps 查看下容器id:
docker update --restart=always 81bcb97c4f5e(自己docker产生的id)


附录六:idea项目打jar包报错

https://blog.csdn.net/a1406075864/article/details/81000930
这个过程中可能会出现下边这种错误,在pom文件中添加build 里边的片段就好

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.463 s <<< FAILURE! - in com.example.demo.SinosoftApplicationTests
[ERROR] initializationError(com.example.demo.SinosoftApplicationTests)  Time elapsed: 0.006 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

打包时添加一个新的插件
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin> 
        </plugins>
    </build>


附录七:SpringBoot多Module打包无法找到类
问题描述:springBoot多模块在idea中可以正常启动,打包时报错找不到类(即common-vo模块的类找不到)
步骤一: 给被依赖的模块pom.xml中增加如下: 打包成非可执行的jar

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--添加自己的启动类路径!-->
                    <!--<mainClass>com.zking.commonvo.CommonVoApplication</mainClass>-->
                    <classifier>exec</classifier>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


步骤二:将测试启动的类及方法的修改为public
修改前:
@SpringBootTest
class EurekaProviderApplicationTests {

    @Test
    void contextLoads() {
    }

}
修改后
@SpringBootTest
public class EurekaProviderApplicationTests {

    @Test
    public void contextLoads() {
    }

}


 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值