精华内容
下载资源
问答
  • 查询docker宿主机IP 宿主机使用ifconfig docker0 就是宿主机IP,容器内部可以使用该IP访问宿主机 root@feifei-PC:/home/feifei# ifconfig docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet ...

    docker安装ping

    apt-get update

    apt install iputils-ping

     

    查询docker宿主机IP

     

    宿主机使用ifconfig

    docker0 就是宿主机IP,容器内部可以使用该IP访问宿主机

    root@feifei-PC:/home/feifei# ifconfig
    docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
            ether 02:42:a2:f3:15:d5  txqueuelen 0  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.30.2  netmask 255.255.255.0  broadcast 192.168.30.255
            inet6 fe80::1583:7d93:a66e:22ad  prefixlen 64  scopeid 0x20<link>
            ether 18:c0:4d:40:2d:c0  txqueuelen 1000  (Ethernet)
            RX packets 14278  bytes 14645530 (13.9 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 11244  bytes 1772447 (1.6 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 57  bytes 5513 (5.3 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 57  bytes 5513 (5.3 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    查看容器IP

    docker inspect 容器名/id

     

     

    #ping
    apt install inetutils-ping 

     

    #nslookup
    apt install dnsutils   

     

    #ifconfig 
    apt install net-tools    

     

    #ip

    apt install iproute2    

     

    #curl

    apt install curl

    展开全文
  • Docker 部署 Spring Cloud并让docker容器使用宿主机ip 使用系统、软件版本: linux 版本: [root@VM_0_17_centos docker]# uname -a Linux VM_0_17_centos 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC ...

    Docker 部署 Spring Cloud并让docker容器使用宿主机ip

    点击链接加入群聊【java菜鸟学习】:https://jq.qq.com/?_wv=1027&k=5afU7nS
    群号:124569404

    使用系统、软件版本:

    linux 版本:

    [root@VM_0_17_centos docker]# uname -a
    Linux VM_0_17_centos 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
    [root@VM_0_17_centos docker]# cat /proc/cpuinfo
    processor	: 0
    vendor_id	: GenuineIntel
    cpu family	: 6
    model		: 79
    model name	: Intel(R) Xeon(R) CPU E5-26xx v4
    stepping	: 1
    microcode	: 0x1
    cpu MHz		: 2394.446
    cache size	: 4096 KB
    physical id	: 0
    siblings	: 1
    core id		: 0
    cpu cores	: 1
    apicid		: 0
    initial apicid	: 0
    fpu		: yes
    fpu_exception	: yes
    cpuid level	: 13
    wp		: yes
    flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch bmi1 avx2 bmi2 rdseed adx xsaveopt
    bogomips	: 4788.89
    clflush size	: 64
    cache_alignment	: 64
    address sizes	: 40 bits physical, 48 bits virtual
    power management:
    
    

    docker版本:

    
    [root@VM_0_17_centos docker]# docker --version
    Docker version 1.13.1, build 8633870/1.13.1
    
    

    安装 Docker 环境

    1. yum安装
    yum install docker
    
    1. 安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:
    ervice docker start
    chkconfig docker on
    
    #LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:
    systemctl  start docker.service
    systemctl  enable docker.service
    
    1. 使用Docker 中国加速器
    vi  /etc/docker/daemon.json
    
    #添加后:
    {
        "registry-mirrors": ["https://registry.docker-cn.com"],
        "live-restore": true
    }
    
    1. 重新启动docker
    systemctl restart docker
    
    

    maven版本:

    [root@VM_0_17_centos docker]# mvn -version
    Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
    Maven home: /usr/local/maven3
    Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.10.0-514.26.2.el7.x86_64", arch: "amd64", family: "unix"
    
    

    安装MAVEN

    1. 下载:http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
    ## 解压
    tar vxf apache-maven-3.5.2-bin.tar.gz
    ## 移动
    mv apache-maven-3.5.2 /usr/local/maven3
    
    1. 修改环境变量, 在/etc/profile中添加以下几行
    MAVEN_HOME=/usr/local/maven3
    export MAVEN_HOME
    export PATH=${PATH}:${MAVEN_HOME}/bin
    
    1. 记得执行source /etc/profile使环境变量生效。

    2. 输入mvn -version 返回版本信息则安装正常。

    jdk版本:

    [root@VM_0_17_centos docker]# java -version
    openjdk version "1.8.0_191"
    OpenJDK Runtime Environment (build 1.8.0_191-b12)
    OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
    
    
    1. 安装JDK
    yum -y install java-1.8.0-openjdk*
    
    
    1. 配置环境变量
      打开 vim /etc/profile
      添加一下内容
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64 
    export PATH=$PATH:$JAVA_HOME/bin 
    
    1. 修改完成之后,使其生效
    source /etc/profile
    
    1. 输入java -version 返回版本信息则安装正常。

    1,创建Eureka服务

    目录结构如下:

    │  eureka-service.iml
    │  pom.xml
    │  README.md
    │
    └─src
        └─main
            ├─java
            │  ├─com
            │  │  └─udo
            │  │      └─eureka
            │  │              EurekaApplication.java
            │  │
            │  └─docker
            │          Dockerfile
            │
            └─resources
                    application.properties
    
    

    application.properties内容如下:

    server.port=8761
    spring.application.name=udo-eureka
    #eureka.instance.hostname=localhost
    
    #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.serviceUrl.defaultZone=http://129.204.42.203:${server.port}/eureka/
    eureka.instance.prefer-ip-address:true
    eureka.instance.ip-address: 129.204.42.203
    logging.level.root=WARN
    logging.level.org.springframework=WARN
    logging.level.com.cool=INFO
    

    pom.xml 文件加入docker build

      <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
            <docker.image.prefix>springboot</docker.image.prefix>
    
        </properties>    
    <build>
            <finalName>eureka-service</finalName>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.4.13</version>
                    <configuration>
                        <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    Dockerfile文件内容如下:

    FROM openjdk:8-jdk-alpine
    VOLUME /tmp
    ADD eureka-service.jar eureka-service.jar
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eureka-service.jar"]
    EXPOSE 8761
    
    • FROM ,表示使用 Jdk8 环境 为基础镜像,如果镜像不是本地的会从 DockerHub 进行下载
    • VOLUME ,VOLUME 指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
    • ADD ,拷贝文件并且重命名
    • ENTRYPOINT ,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT

    同样Client 也是相同的配置pom.xmlDockerfile (注意修改Dockerfile 名字和EXPOSE端口)

    Client application.properties配置如下:

    spring.application.name=udo-product
    eureka.instance.prefer-ip-address=true
    server.port=8084
    spring.mvc.favicon.enabled = false
    eureka.client.serviceUrl.defaultZone=http://129.204.42.203:8761/eureka/
    spring.cloud.inetutils.ignoredInterfaces[0]=lo
    spring.cloud.inetutils.ignoredInterfaces[1]=eth0
    spring.cloud.inetutils.ignoredInterfaces[2]=eth1
    spring.instance.instance-id: ${spring.cloud.client.ip-address}:${server.port}
    spring.instance.prefer-ip-address: true
    

    然后使用xFTP将两个文件夹上传到linux服务器

    我上传的目录是:

    [root@VM_0_17_centos ftp_down]# pwd
    /usr/local/ftp_down
    [root@VM_0_17_centos ftp_down]# ls
    apache-maven-3.6.0-bin.tar.gz  eureka-service  eureka-service-0.0.1-SNAPSHOT.jar  udo-product
    [root@VM_0_17_centos ftp_down]# 
    
    

    可以看到有eureka-service文件夹和udo-product文件夹

    先打开eureka-service ,使用mvn打包命令测试是否能够正常打包,第一次可能打包时间比较长

    mvn clean package -Dmaven.skip.test=true
    #-Dmaven.skip.test=true 跳过测试并且不构建test的包
    

    打包结果如下:

    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------< com.udo:eureka-service >-----------------------
    [INFO] Building eureka-server 1.0.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ eureka-service ---
    [INFO] Deleting /usr/local/ftp_down/eureka-service/target
    [INFO] 
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ eureka-service ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ eureka-service ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /usr/local/ftp_down/eureka-service/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ eureka-service ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /usr/local/ftp_down/eureka-service/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ eureka-service ---
    [INFO] No sources to compile
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ eureka-service ---
    [INFO] No tests to run.
    [INFO] 
    [INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ eureka-service ---
    [INFO] Building jar: /usr/local/ftp_down/eureka-service/target/eureka-service.jar
    [INFO] 
    [INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ eureka-service ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  25.078 s
    [INFO] Finished at: 2018-12-27T11:25:56+08:00
    [INFO] ------------------------------------------------------------------------
    
    

    打包正常,然后使用docker构建,第一次可能打包时间比较长

    mvn clean package docker:build
    

    构建结果如下:

    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------< com.udo:eureka-service >-----------------------
    [INFO] Building eureka-server 1.0.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ eureka-service ---
    [INFO] Copying /usr/local/ftp_down/eureka-service/target/eureka-service.jar -> /usr/local/ftp_down/eureka-service/target/docker/eureka-service.jar
    [INFO] Copying /usr/local/ftp_down/eureka-service/src/main/docker/Dockerfile -> /usr/local/ftp_down/eureka-service/target/docker/Dockerfile
    [INFO] Building image springboot/eureka-service
    Step 1/5 : FROM openjdk:8-jdk-alpine
     ---> 97bc1352afde
    Step 2/5 : VOLUME /tmp
     ---> Using cache
     ---> b2072d304eeb
    Step 3/5 : ADD eureka-service.jar eureka-service.jar
     ---> 867dd6b7e3a1
    Removing intermediate container 83785bff791a
    Step 4/5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /eureka-service.jar
     ---> Running in 34986325eecd
     ---> 07d1cf9034dd
    Removing intermediate container 34986325eecd
    Step 5/5 : EXPOSE 8761
     ---> Running in e122c6148e70
     ---> 91d01dd28847
    Removing intermediate container e122c6148e70
    Successfully built 91d01dd28847
    [INFO] Built springboot/eureka-service
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  18.564 s
    [INFO] Finished at: 2018-12-27T11:27:34+08:00
    [INFO] ------------------------------------------------------------------------
    
    

    可以看到 依次执行了5个步骤,且构建了docker image_id为91d01dd28847

    重复相同的操作构建 udo-product

    使用docker images查看镜像

    REPOSITORY                                      TAG                 IMAGE ID            CREATED              SIZE
    springboot/eureka-service                       latest              91d01dd28847        About a minute ago   149 MB
    springboot/udo-product-core                     latest              ce13df6d307c        About an hour ago    204 MB
    springboot/product-api                          latest              833022b7a675        About an hour ago    103 MB
    <none>                                          <none>              e413e1ef2c9d        12 hours ago         204 MB
    <none>                                          <none>              59f24d920431        12 hours ago         204 MB
    <none>                                          <none>              f93d372a1017        17 hours ago         149 MB
    target_docker                                   latest              0c7b92f346a0        18 hours ago         149 MB
    <none>                                          <none>              aac971ad6391        25 hours ago         147 MB
    <none>                                          <none>              d89a2d10f5d8        26 hours ago         103 MB
    docker.io/rabbitmq                              3-management        d69a5113ceae        8 weeks ago          149 MB
    docker.io/openjdk                               8-jdk-alpine        97bc1352afde        2 months ago         103 MB
    docker.elastic.co/elasticsearch/elasticsearch   6.1.1               06f0d8328d66        12 months ago        539 MB
    docker.io/gliderlabs/registrator                latest              3b59190c6c80        2 years ago          23.8 MB
    
    

    springboot/eureka-servicespringboot/udo-product-core 都在内

    删除<none>镜像

    [root@VM_0_17_centos eureka-service]# docker rmi $(docker images -f "dangling=true" -q)
    

    使用命令启动springboot/eureka-service

    docker run -di -p 8761:8761 --name=eureka springboot/eureka-service
    #-di 后台启动
    #-p 8761:8761 将宿主机的8761端口映射到docker容器的8761端口
    #--name=eureka  命名
    #springboot/eureka-service 对应 images REPOSITORY
    

    访问http://129.204.42.203:8761/

    在这里插入图片描述

    dock命令启动udo-product(此时给udo-product分配静态ip,否则注册到eureka上面是内网ip)

    docker run -d --name product_core --expose=8084 -p 8084:8084 -e "EUREKA_INSTANCE_IP-ADDRESS=129.204.42.203" -e "SERVER_PORT=8084" springboot/udo-product-core
    
    

    再次访问http://129.204.42.203:8761/,鼠标悬停在08e62989da4f:udo-product:8084,可看到左下角提示跳转地址为:http://129.204.42.203:8084/actuator/info

    在这里插入图片描述

    同理,在本地再开一个服务也可以注册到到eureka,且本地的服务可以调到docker内的udo-product服务

    展开全文
  • docker 访问宿主机

    万次阅读 2018-08-23 11:54:13
    有时候就需要在docker容器里访问宿...会看到docker0那个ip,可以使用来访问宿主机 方案二: docker 18.03 加入了一个 feature,在容器中可以通过 host.docker.internal来访问主机 Use your internal IP addres...

    有时候就需要在docker容器里访问宿主机提供的服务。
    例如容器里的应用需要访问宿主机的mysql服务。

    方案一:
    宿主机执行ifconfig
    会看到docker0那个ip,可以使用来访问宿主机

    方案二:
    docker 18.03 加入了一个 feature,在容器中可以通过 host.docker.internal来访问主机
    Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

    展开全文
  • 宿主机ip docker.for.mac.host.internal 如宿主机mysql docker.for.mac.host.internal:3306

    宿主机ip
    docker.for.mac.host.internal

    如宿主机mysql
    docker.for.mac.host.internal:3306

    展开全文
  • 使用weave实现docker宿主机连接 docker之于微服务,就好像人体的经脉之于血液。一个人充满血液但没有经脉,冲其量就是打肿脸冲胖子。经脉要互通,docker之间要互联。目前最主流的技术有Weave和Kubernetes两种,...
  • 如何获取docker宿主机ip

    万次阅读 2016-10-13 10:23:07
    获取docker宿主机ip 本文转自[记录无限:www.gluoo.cn]. 在开发调试过程中,很可能遇到类似的情况: 宿主机跑着一个DB,不管是MySQL还是MongoDB,总之跑在宿主机,docker里运行着应用程序,但程序要连...
  • docker宿主机使用-P命令随机端口绑定的docker指定端口(比如每个容器都暴露8080端口), 但是容器中无法知道宿主机的ip和映射端口,即eureka上的实例不知道暴露给外部的什么访问IP和端口(宿主机IP和宿主机port)。...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 29,389
精华内容 11,755
关键字:

docker使用宿主机ip