Search code examples
network-programmingdocker-composelan

docker-compose assign lan ip to service


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.

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:

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

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
        ...

I have also tried this configuration:

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

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 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

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.

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

Maybe I have to use macvlan or ipvlan?

Thank you in advance for each response!


Solution

  • Finally I found a solution that works for me.

    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
    

    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
    

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