docker-compose将lan ip分配给服务 [英] docker-compose assign lan ip to service

查看:237
本文介绍了docker-compose将lan ip分配给服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个与docker-compose连接的容器,它们全部都在docker内部网络中.但是我想通过为其分配一个LAN IP来暴露其中一个容器.

I have three containers connected with docker-compose, all together in a docker internal network. But I would like to expose one of the containers by assigning to it a LAN IP.

因此,我的主机指向了IP:192.168.220.33,我想为gitlab容器分配IP:192.168.220.220.

So, I have the host pointed by the IP: 192.168.220.33 and I would like to assign to the gitlab container the IP: 192.168.220.220.

我现在的问题是出现此错误:

My problem right now is that I am getting this error:

错误:对于gitlab无法启动服务gitlab:无效的链接本地IP地址:192.168.220.220

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

我正在使用docker-compose 1.11.2,并且具有以下docker-compose.yml文件:

I am using docker-compose 1.11.2 and I have the following docker-compose.yml file:

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

此配置可以构建和运行容器,并且所有内容都可以从localhost访问,但是我无法对所需的ip(192.168.220.220)进行ping操作.主机也不能不在主机之外.

This configuration can build and run the containers and everything is accessible from localhost, but I cannot do ping to the desired ip (192.168.220.220). Nor by the host machine neither outside the host machine.

PING 192.168.220.220(192.168.220.220):56个数据字节
要求icmp_seq 0超时
ping:sendto:没有路由到主机
要求icmp_seq 1超时
ping:sendto:没有路由到主机
要求icmp_seq 2超时
ping:sendto:没有路由到主机
要求icmp_seq 3超时
ping:sendto:没有路由到主机

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.

I would like to know how to assign the gitlab container the IP for being accesible through this IP instead of the host IP and the exposed ports.

更新我希望容器和主机位于网络中的同一级别,因此两个IP均以192.168.220.x开头.

Update I would like that the container and the host are at the same level in the network so both IPs begin by: 192.168.220.x

也许我必须使用 macvlan ipvlan ?

预先感谢您的每次回复!

Thank you in advance for each response!

推荐答案

最后,我找到了适合我的解决方案.

Finally I found a solution that works for me.

docker-compose.yml

docker-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地址:

And then it is necessary to set the IP address with ifconfig:

sudo ifconfig eht0.10 192.168.220.220 netmask 255.255.255.0 up

然后我可以通过调用分配的IP来访问docker容器.

Then I have access to the docker container by calling the assigned IP.

这篇关于docker-compose将lan ip分配给服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆