Docker For Mac Connect Container By Ip

Docker For Mac 一直存在一个问题,就是无法通过 ip 访问容器 。Detail
之前使用过以下方法

  1. 使用 docker-machine create vm ,虚拟机 static ip 来访问对应的容器。(容器 Port 映射 vm-machine Port)
  2. 使用 openvpn 连接容器
  3. 以下使用新的方法

Docker-composer File Config

version: '3'

networks:
  laravel_share_network:
    ipam:
      config:
        - subnet: 192.168.240.0/24

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx-beauty
    restart: always
    ports:
      - "9090:80"
    volumes:
      - ../beauty:/var/www/html/beauty
      - ./docker/nginx/beauty.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
    networks:
       laravel_share_network:
          ipv4_address: 192.168.240.5

  php:
    image: biyongyao/php-fpm-dev:7.4
    restart: always
    container_name: php-beauty
    volumes:
      - "./docker/php/config/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini"
      - "../beauty:/var/www/html/beauty"
    networks:
       laravel_share_network:
          ipv4_address: 192.168.240.2

Using docker-connector

Install by brew

bash > brew install wenjunxiao/brew/docker-connector

Alter Config

bash > vi /usr/local/etc/docker-connector.conf

# route 192.168.240.0/24 expose

Start docker-connector

# start service 
bash > sudo brew services start docker-connector

# check whether start
bash > brew services list

Start Docker to Create Virtual Network Card

bash > docker run -it -d --restart always --net host --cap-add NET_ADMIN --name connector wenjunxiao/mac-docker-connector

Share With other Mac User

  1. alter config

    # alter config 
    bash > vi /usr/local/etc/docker-connector.conf
  2. text content

    # text content
    route 192.168.240.0/24 expose 
    token user1 192.168.240.100
    token user2 192.168.240.110
  3. install in other mac

    bash > brew install wenjunxiao/brew/docker-accessor
    bash > sudo docker-accessor -remote 192.168.240.100 -token user1
Last modification:November 26th, 2020 at 11:17 am
小编很用心去写文章的,大家伙们给点支持呗。