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
version: '2' services: haproxy: image: haproxy:latest ports: - "80:80" - "443:443" volumes: - ${PWD}/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg restart: always networks: - widgets logging: driver: json-file options: max-size: "100m" max-file: "3" labels: "haproxy" mariadb: image: mariadb:latest volumes: - /vol/appdata/mariadb:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=secret-pw restart: always networks: - widgets logging: driver: json-file options: max-size: "100m" max-file: "3" labels: "mariadb" app_orangella: image: apache:1.0 restart: always ports: - "81:80" networks: - widgets logging: driver: json-file options: max-size: "50m" max-file: "3" labels: "app_orangella" networks: widgets: driver: bridge ipam: config: - subnet: 172.10.0.0/16 gateway: 172.10.5.254 aux_addresses: haproxy: 172.10.1.2 mariadb: 172.10.1.3 app_orangella: 172.10.1.4
hope this will get you rolling with Docker compose 🙂
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?
Hey Daniel,
Could you post results of ‘docker ps’ ? Did you get any erros during startup ?
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 🙂
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
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
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 )
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.
Hey Kevin 😉 This was post more focused on functionality but ur right.
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!
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 ?