2

I'm trying to create multiple dockers (from the same image) and put them in a subnet where I can work with them as if they were many machines.

version: "3"

services:
    machine001:
        container_name: "machine001"
        build: .
        image: "test-machine"
        ports:
            - "1337"
            - "22"
        networks:
            net:
                ipv4_address: 172.20.1.1

    machine002:
        container_name: "machine002"
        build: .
        image: "test-machine"
        ports:
            - "1337"
            - "22"
        networks:
            net:
                ipv4_address: 172.20.1.2

networks:
   net:
        driver: bridge
        ipam:
            config:
                - subnet: 172.20.1.0/24

When composing, however, I get an error for the first machine and I get an error that says Address already in use (the second one works just fine), so by browsing a little I thought that maybe the address 172.20.1.1 is reserved for the gateway and I tried changhing it, following another question

services:
    # same as before...

networks:
   net:
        driver: bridge
        ipam:
            config:
                - driver: default
                  subnet: 172.20.1.0/24
                  gateway: 172.20.1.254

However, compose doesn't like that and it says

ERROR: The Compose file './docker-compose.yml' is invalid because: networks.net.ipam.config value Additional properties are not allowed ('gateway', 'driver' were unexpected)

What is the right way to change the gateway for the subnet? Would that make available the address 172.20.1.1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.