2017-03-04 84 views
2

我有三個與docker-compose相連的容器,所有這些都連接在一個docker內部網絡中。但我想通過給它分配一個局域網IP來暴露其中一個容器。docker-compose將lan ip分配給服務

所以,我有IP指向的主機:192.168.220.33,我想分配給gitlab容器IP:192.168.220.220。

現在我的問題是,我收到此錯誤:

ERROR: for gitlab Cannot start service gitlab: invalid link local IP address: 192.168.220.220

我使用泊塢窗,撰寫1.11.2和我有以下幾個搬運工,compose.yml文件:

version: '2.1' 

networks: 
    front: 
     driver: bridge 

services: 
    redis: 
     image: sameersbn/redis:latest 
     volumes: 
      - /tmp/gitlab/redis:/var/lib/redis:Z 
     networks: 
      - default 
     ... 
    postgresql: 
     image: sameersbn/postgresql:latest 
     volumes: 
      - /tmp/gitlab/postgresql:/var/lib/postgresql:Z 
     networks: 
      - default 
     ... 
    gitlab: 
     image: sameersbn/gitlab:latest 
     depends_on: 
      - redis 
      - postgresql 
     ports: 
      - "22:22" 
      - "80:80" 
      - "443:443" 
     networks: 
      default: {} 
      outside: 
       link_local_ips: 
        - 192.168.220.220 
     ... 

我自己也嘗試這種配置:

version: '2.1' 

networks: 
    front: 
     driver: bridge 
     ipam: 
      config: 
       - subnet: 192.168.220.0/24 

services: 
    redis: 
     networks: 
      - default 
     ... 
    postgresql: 
     networks: 
      - default 
     ... 
    gitlab: 
     ... 
     networks: 
      default: {} 
      outside: 
       ipv4_address: 192.168.220.220 

此配置可以構建和運行容器,並且所有內容都可以從本地主機訪問,但我無法ping到所需的IP(192.168.220.220)。主機也不在主機之外。

PING 192.168.220.220 (192.168.220.220): 56 data bytes
Request timeout for icmp_seq 0
ping: sendto: No route to host
Request timeout for icmp_seq 1
ping: sendto: No route to host
Request timeout for icmp_seq 2
ping: sendto: No route to host
Request timeout for icmp_seq 3
ping: sendto: No route to host

我想知道如何gitlab容器分配IP爲是通過這個IP而不是主機IP和暴露的端口入店。

更新我想,容器和主機是在網絡中同一水平,使雙方的IP首先:192.168.220.x

也許我不得不使用macvlanipvlan

預先感謝您的每一個迴應!

回答

0

最後我找到了適合我的解決方案。

泊塢窗,compose.yml

version: '2' 

networks: 
    front: 
     driver: macvlan 
     driver_opts: 
      parent: eth0.10 
     ipam: 
      config: 
       - subnet: 192.168.220.0/24 
        gateway: 192.168.220.1 

services: 
    redis: 
     networks: 
      - default 
     ... 
    postgresql: 
     networks: 
      - default 
     ... 
    gitlab: 
     ... 
     networks: 
      default: {} 
      outside: 
       ipv4_address: 192.168.220.220 

然後有必要使用ifconfig設置IP地址:

sudo ifconfig eht0.10 192.168.220.220 netmask 255.255.255.0 up 

然後我可以訪問泊塢窗容器調用分配的IP。

1

這是一個完整的工作docker-compose.yml你試圖實現的。

version: '2.1' 

networks: 
    outside: 
    driver: bridge 
    ipam: 
     driver: default 
     config: 
     - subnet: 192.168.220.0/24 

services: 
    redis: 
    image: sameersbn/redis:latest 
    restart: always 
    command: 
    - --loglevel warning 
    networks: 
     - default 
    postgresql: 
    restart: always 
    image: sameersbn/postgresql:latest 
    environment: 
    - DB_USER=gitlab 
    - DB_PASS=password 
    - DB_NAME=gitlabhq_production 
    - DB_EXTENSION=pg_trgm 
    networks: 
     - default 
    gitlab: 
    restart: always 
    image: sameersbn/gitlab:latest 
    depends_on: 
     - redis 
     - postgresql 
    networks: 
     default: 
     outside: 
     ipv4_address: 192.168.220.220 
    environment: 
    - DEBUG=false 

    - DB_ADAPTER=postgresql 
    - DB_HOST=postgresql 
    - DB_PORT=5432 
    - DB_USER=gitlab 
    - DB_PASS=password 
    - DB_NAME=gitlabhq_production 

    - REDIS_HOST=redis 
    - REDIS_PORT=6379 

    - GITLAB_HTTPS=false 
    - SSL_SELF_SIGNED=false 

    - GITLAB_HOST=192.168.220.220 
    - GITLAB_PORT=80 
    - GITLAB_SSH_PORT=22 
    - GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alphanumeric-string 
    - GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alphanumeric-string 
    - GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alphanumeric-string 

    - GITLAB_ROOT_PASSWORD=password 
    - GITLAB_ROOT_EMAIL= 

做完docker-compose up之後,您將可以訪問容器公開的端口。但是,通過這些設置,您將無法通過外部主機訪問gitlab泊塢窗。

+0

謝謝你的迴應尼古拉斯!我認爲這個設置是正確的,當主機和容器有不同的IP時,我的意思是它們不共享192.168.220.X.在我的情況下,我希望主機和容器都處於網絡中的同一級別,而不是主機子網中的容器。 – cdalvaro