简体   繁体   中英

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM