docker-compose assign lan ip to service
Asked Answered
H

2

6

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!

Heliport answered 4/3, 2017 at 17:33 Comment(0)
H
3

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.

Heliport answered 7/3, 2017 at 10:25 Comment(0)
R
3

This is a full working docker-compose.yml of what you are trying to achieve.

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=

After doing docker-compose up you'll be able to access the containers exposed ports. Still, with these setup you won't be able to reach the gitlab docker from an external host.

Repartee answered 4/3, 2017 at 20:38 Comment(1)
Thank you for your response Nicolas! I think that this settings are the correct one when the host and the container have different IPs, I mean, they don't share 192.168.220.X. In my case I want that both, the host and the container, are at the same level in the network, not the container in a subnet of the host.Lura
H
3

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.

Heliport answered 7/3, 2017 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.