Automation Ninja's Dojo

Docker compose v2 – using static network addresses

Docker compose is a really great piece of code 🙂 that will allow you to build better orchestration with your containers. Recent breaking releases introduced a lot of features. While looking at some of them I was wondering about situations in which you build more (or a bit less ) complex containers based environment and do not have service discovery. In some instances you would just like to have static IP addresses.

Now this is perfectly easy to be done when running containers with cli … but how do you do that with compose ? After looking at the documentation I managed to come out with the following

And this is allowing me to specify static IP addresses for my containers using the compose file. For reference you can find the snippet of full file below

 

hope this will get you rolling with Docker compose 🙂

 

11 thoughts on “Docker compose v2 – using static network addresses

  1. Hey rafpe,

    Thank you for the information, but I can’t seem to get this running properly.
    As Maks has stated before, the ip is not reachable when using a browser and inserting the ip address (192.168.100.102 in my case).

    I also made sure to use a private range:
    subnet: 192.168.100.0/24
    gateway: 192.168.100.100
    aux_addresses:
    node-ip: 192.168.100.102

    Am I missing something?

      1. Hello,

        Here are the docker ps results:
        4ec61ffed4c2 imagephpapachenetworktest_app “docker-php-entryp…” 8 seconds ago Up 6 seconds 0.0.0.0:8080->80/tcp app

        58bb84336d6e mysql:5.7.17 “docker-entrypoint…” 9 seconds ago Up 7 seconds 0.0.0.0:3306->3306/tcp mysql

        And here’s what I have inside my docker compose:
        version: ‘2’

        services:

        app:
        build: .
        container_name: app
        ports:
        – “8080:80”
        volumes:
        – ./www/app:/var/www/app
        working_dir: /var/www/app
        restart: always
        links:
        – mysql
        networks:
        – ipcfg

        mysql:
        image: mysql:5.7.17
        container_name: mysql
        environment:
        MYSQL_ROOT_PASSWORD: root123
        MYSQL_DATABASE: db01
        MYSQL_USER: usr01
        MYSQL_PASSWORD: pwd01
        ports:
        – “3306:3306”
        volumes:
        – ./services/mysql/app/source:/var/lib/mysql
        restart: always
        networks:
        – ipcfg

        networks:
        ipcfg:
        driver: bridge
        ipam:
        config:
        – subnet: 192.168.50.0/24
        gateway: 192.168.50.1
        aux_addresses:
        app: 192.168.50.2
        mysql: 192.168.50.3

        I’m new to this, so any feedback is always welcome 🙂

        1. Hey Daniel,

          So I used the file you provided and changed it a bit and it all works 🙂


          ➜ /tmp docker-compose up -d
          Creating mysql
          Creating app
          ➜ /tmp docker ps
          CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
          d5b69d57d15b httpd:2.4 "httpd-foreground" 5 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp app
          4ce91d6d685c mysql:5.7.17 "docker-entrypoint..." 7 seconds ago Up 5 seconds 0.0.0.0:3306->3306/tcp mysql
          7149d84c6658 httpd:2.4 "httpd-foreground" 2 days ago Up 2 days 0.0.0.0:32768->80/tcp my-apache-app
          ➜ /tmp curl localhost:8080

          It works!

          And for the file I have used this one http://pastebin.com/gK9efCyk

          1. Hey rafpe

            Maybe I’m not understanding the whole concept of using static ip’s with Docker just yet. But shouldn’t I be able to use the ip within my browser? The configuration does work tho, I can use localhost and it connects flawlessly.

            grtz
            Daniel

          2. Idea behind static ip addresses is useful when you don’t have service discovery and need to reference containers between each other. Like some web apps and mysql ( without using “LINK” in favour of networks )

  2. Thank you for this, but… don’t use 172.10.x.x/16!!! That’s not part of the private address space, those addresses belong to someone! The private space is 172.16.0.0/12 i.e. 172.16.0.0-172.31.255.255.

  3. Hi,

    Thank you for the article. Everything seems to be easy to understand, but or some reason trying to reach 172.10.1.4 to request a server gives no result at all, server just not found. Is there any way to find a solution for the problem? A basically don’t know where to start the research.

    Thank you!

    1. Hey Maks,

      Could you elaborate a bit on your setup ? Are you running docker in VM ?( Docker toolbox ) / docker engine on server machine ? Which docker version are you using ?

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: