Provide static IP to docker containers via docker-compose - docker

I'm trying to provide static IP address to containers. I understand that I have to create a custom network. I create it and the bridge interface is up on the host machine (Ubuntu 16.x). The containers get IP from this subnet but not the static I provided.
Here is my docker-compose.yml:
version: '2'
services:
mysql:
container_name: mysql
image: mysql:latest
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
networks:
- vpcbr
apigw-tomcat:
container_name: apigw-tomcat
build: tomcat/.
ports:
- "8080:8080"
- "8009:8009"
networks:
- vpcbr
depends_on:
- mysql
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1
aux_addresses:
mysql: 10.5.0.5
apigw-tomcat: 10.5.0.6
The containers get 10.5.0.2 and 10.5.0.3, instead of 5 and 6.

Note that I don't recommend a fixed IP for containers in Docker unless you're doing something that allows routing from outside to the inside of your container network (e.g. macvlan). DNS is already there for service discovery inside of the container network and supports container scaling. And outside the container network, you should use exposed ports on the host. With that disclaimer, here's the compose file you want:
version: '2'
services:
mysql:
container_name: mysql
image: mysql:latest
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
networks:
vpcbr:
ipv4_address: 10.5.0.5
apigw-tomcat:
container_name: apigw-tomcat
build: tomcat/.
ports:
- "8080:8080"
- "8009:8009"
networks:
vpcbr:
ipv4_address: 10.5.0.6
depends_on:
- mysql
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1

I was facing some difficulties with an environment variable that is with custom name (not with container name /port convention for KAPACITOR_BASE_URL and KAPACITOR_ALERTS_ENDPOINT). If we give service name in this case it wouldn't resolve the ip as
KAPACITOR_BASE_URL: http://kapacitor:9092
In above http://[**kapacitor**]:9092 would not resolve to http://172.20.0.2:9092
I resolved the static IPs issues using subnetting configurations.
version: "3.3"
networks:
frontend:
ipam:
config:
- subnet: 172.20.0.0/24
services:
db:
image: postgres:9.4.4
networks:
frontend:
ipv4_address: 172.20.0.5
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:latest
networks:
frontend:
ipv4_address: 172.20.0.6
ports:
- "6379"
influxdb:
image: influxdb:latest
ports:
- "8086:8086"
- "8083:8083"
volumes:
- ../influxdb/influxdb.conf:/etc/influxdb/influxdb.conf
- ../influxdb/inxdb:/var/lib/influxdb
networks:
frontend:
ipv4_address: 172.20.0.4
environment:
INFLUXDB_HTTP_AUTH_ENABLED: "false"
INFLUXDB_ADMIN_ENABLED: "true"
INFLUXDB_USERNAME: "db_username"
INFLUXDB_PASSWORD: "12345678"
INFLUXDB_DB: db_customers
kapacitor:
image: kapacitor:latest
ports:
- "9092:9092"
networks:
frontend:
ipv4_address: 172.20.0.2
depends_on:
- influxdb
volumes:
- ../kapacitor/kapacitor.conf:/etc/kapacitor/kapacitor.conf
- ../kapacitor/kapdb:/var/lib/kapacitor
environment:
KAPACITOR_INFLUXDB_0_URLS_0: http://influxdb:8086
web:
build: .
environment:
RAILS_ENV: $RAILS_ENV
command: bundle exec rails s -b 0.0.0.0
ports:
- "3000:3000"
networks:
frontend:
ipv4_address: 172.20.0.3
links:
- db
- kapacitor
depends_on:
- db
volumes:
- .:/var/app/current
environment:
DATABASE_URL: postgres://postgres#db
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
INFLUX_URL: http://influxdb:8086
INFLUX_USER: db_username
INFLUX_PWD: 12345678
KAPACITOR_BASE_URL: http://172.20.0.2:9092
KAPACITOR_ALERTS_ENDPOINT: http://172.20.0.3:3000
volumes:
postgres_data:

If you are never seeing the static IP address set, perhaps it could be because you are using "docker compose up". Try using "docker-compose up".
When I use "docker-compose up" (with the hyphen) I now see the static IPs assigned.
networks:
hfnet:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.55.0/24
gateway: 192.168.55.1
services:
web:
image: 'mycompany/webserver:latest'
hostname: www
domainname: mycompany.com
stdin_open: true # docker run -i
tty: true # docker run -t
networks:
hfnet:
ipv4_address: 192.168.55.10
ports:
- '80:80'
- '443:443'
volumes:
- '../honeyfund:/var/www/html'
I wasted a lot of time to figure that one out. :(

I realized, that the more convenient and meaningful way is to give the container a container-name.
You can use the name in the same docker network as source.
This helped me because the docker-containers had changing IPs and by this I can communicate with another container with a static name that I can use in config-files.

Related

Wrong ip assignment docker-compose

I am building a website and trying to deploy the whole stack using docker compose.
The website needs a database, an api, a middleware translation layer and a frontend that are all able to communicate with eachother.
I have understood that I might need a network. So I made one.
Problem is that all containers get random ip adresses within the ip range.
it seems aux_adresses does not do the thing I thought it did...
services:
db:
image: neo4j:community
restart: unless-stopped
volumes:
- ./conf:/conf
- ./data:/data
- ./import:/import
- ./logs:/logs
- ./plugins:/plugins
environment:
# Raise memory limits
- NEO4J_AUTH=neo4j/password
- NEO4J_dbms_memory_pagecache_size=1G
- NEO4J_dbms.memory.heap.initial_size=1G
- NEO4J_dbms_memory_heap_max__size=1G
ports:
- 7474:7474
- 7687:7687
networks:
- matrix-network
api:
build: ./api/.
restart: unless-stopped
ports:
- 8000:8000
networks:
- matrix-network
middleware:
build:
./database-middleware/.
restart: unless-stopped
ports:
- 4000:4000
networks:
- matrix-network
web:
build:
./.
restart: unless-stopped
ports:
- "80:80"
networks:
- matrix-network
networks:
matrix-network:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
ip_range: 172.28.5.0/24
gateway: 172.28.5.254
aux_addresses:
api: 172.28.1.5
db: 172.28.1.6
middleware: 172.28.1.7
web: 172.28.1.8
just use the service name instead of an ip.
so for example: http://middleware:4000 instead of the ip.
thx #tkausl

How to open phpmyadmin as domain in localhost?

I have one docker-compose file that have nginx, mysql and phpmyadmin.
phpmyadmin is open in port 8080 in this address mydomain.com:8080 as well.
How can I convert this address to http://phpmyadmin.mydomain.com or http://pma.mydomain.com in my server?
I have used this docker-compose file:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./Dockerfile/Dockerfile
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./Beh:/var/www
- ./Config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
# - "443:443"
volumes:
- ./Beh:/var/www
- ./Config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
database:
image: mariadb:latest
container_name: database
environment:
- "MYSQL_USERNAME=root"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "3306:3306"
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
- "MYSQL_USERNAME=root"
- "MYSQL_ROOT_PASSWORD=secret"
- "PMA_HOST=database"
links:
- database
ports:
- "8080:80"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I don't know how to configure nginx file to achieve this goal.
You can use the repo -> https://github.com/jwilder/nginx-proxy
This is what I'm using to create new local domain name for each project.
You have to add "VIRTUAL_HOST" to the environment, expose the ports ("expose: 80") and also add the new domain in your /etc/hosts.

Share Docker container through local network and access to it from an another host

I try to share a container through my local network, to access this container from an another machine on the same network. I have follow tihs tutorial (section "With macvlan devices") and I succeeded to share a simple web container and access from an another host.
But the container that I want to share is a little more sophisticated, because he comminicate with other containers on the host through an internal network on the host.
I try to bind my existing container created in my docker-compose but I can't access to it. Can you help me, or tell me where I'm wrong if so please ?
This is my docker-compose :
version: "2"
services:
baseimage:
container_name: baseimage
image: base
build:
context: ./
dockerfile: Dockerfile.base
web:
container_name: web
image: web
env_file:
- .env
context: ./
dockerfile: Dockerfile.web
extra_hosts:
- dev.api.exemple.com:127.0.0.1
- dev.admin.exemple.com:127.0.0.1
- dev.www.exemple.com:127.0.0.1
ports:
- 80:80
- 443:443
volumes:
- ./code:/ass
- /var/run/docker.sock:/var/run/docker.sock
tty: true
dns:
- 8.8.8.8
- 8.8.4.4
links:
- mysql
- redis
- elasticsearch
- baseimage
networks:
devbox:
ipv4_address: 172.20.0.2
cron:
container_name: cron
image: cron
build:
context: ./
dockerfile: Dockerfile.cron
volumes:
- ./code:/ass
tty: true
dns:
- 8.8.8.8
- 8.8.4.4
links:
- web:dev.api.exemple.com
- mysql
- redis
- elasticsearch
- baseimage
networks:
devbox:
ipv4_address: 172.20.0.3
mysql:
container_name: mysql
image: mysql:5.6
ports:
- 3306:3306
networks:
devbox:
ipv4_address: 172.20.0.4
redis:
container_name: redis
image: redis:3.2.4
ports:
- 6379:6379
networks:
devbox:
ipv4_address: 172.20.0.5
elasticsearch:
container_name: elastic
image: elasticsearch:2.3.4
environment:
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- ./es_data:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
devbox:
ipv4_address: 172.20.0.6
chromedriver:
container_name: chromedriver
image: robcherry/docker-chromedriver:latest
privileged: true
ports:
- 4444:4444
environment:
- CHROMEDRIVER_WHITELISTED_IPS='172.20.0.2'
- CHROMEDRIVER_URL_BASE='wd/hub'
- CHROMEDRIVER_EXTRA_ARGS='--ignore-certificate-errors'
networks:
devbox:
ipv4_address: 172.20.0.7
links:
- web:dev.www.exemple.com
networks:
devbox:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
- subnet: 172.20.0.0/16
gateway: 172.20.0.1
Create an external network assign the external network and devbox network to web. Web would then be publicly accessible via the external network public ip address and communicate with the internal services using the devbox network.
Will post working example asap

Docker-Compose v3 - static ip

I am trying to set a static IP on my docker-compose v3 file but.. i can't.
Each time I set it, I can't connect to the webpage anymore.
I am getting ERR_ADDRESS_UNREACHABLE
Here is my config:
# docker-compose.yml
version: '3'
services:
web:
build: ./etc/nginx
ports:
- "90:80"
volumes:
- "./etc/ssl:/etc/ssl"
depends_on:
- php
- database
php:
build: ./etc/php
ports:
- 9000:9000
links:
- database:mysqldb
volumes:
- "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- ${APP_PATH}:/var/www/symfony
database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- 3300:3306
volumes:
- "./data/db/mysql:/var/lib/mysql"
and
# docker-compose.override.yml
version: '3'
services:
web:
networks:
test:
ipv4_address: '10.1.0.100'
networks:
test:
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
It should be like this:
services:
web:
networks:
test:
ipv4_address: 10.1.0.100
networks:
test:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
And in my case I placed networks section before services.
UPDATE:
eventually I've ended up using external network for like this:
docker network create --subnet 10.5.0.0/24 local_network_dev
and then in any docker-compose you can just use it like this:
version: '3.2'
networks:
default:
external:
name: local_network_dev
and inside of image:
web:
networks:
default:
ipv4_address: 10.5.0.11
# Use root/example as user/password credentials
version: '3.3'
networks:
netBackEnd:
ipam:
driver: default
config:
- subnet: 192.168.0.0/24
services:
mongo-db:
image: mongo
container_name: cnt_mongo
restart: always
environment:
MONGO_INITDB_DATABASE: dbArland
MONGO_INITDB_ROOT_USERNAME: maguilarac
MONGO_INITDB_ROOT_PASSWORD: pwdmaguilarac
ports:
- 27017:27017
volumes:
- ./script1_creacion_usuario.js:/docker-entrypoint-initdb.d/script1_creacion_usuario.js:ro
- ./script2_creacion_coleccion.js:/docker-entrypoint-initdb.d/script2_creacion_coleccion.js:ro
- ./script4_carga_productos.js:/docker-entrypoint-initdb.d/script4_carga_productos.js:ro
- ./productos_inicial.json:/docker-entrypoint-initdb.d/productos_inicial.json:ro
- ./mongo-volume:/data/db
networks:
netBackEnd:
ipv4_address: 192.168.0.4
mongo-express:
image: mongo-express
container_name: cnt_mongo-express
restart: always
ports:
- 9081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: maguilarac
ME_CONFIG_MONGODB_ADMINPASSWORD: pwdmaguilarac
networks:
netBackEnd:
ipv4_address: 192.168.0.6
Just a very important note that should all future users know.
If you are trying to edit already existing network, you will most likely get error
Cannot start service xxx: Invalid address xxx.xxx.xxx.xxx: It does not belong to any of this network's subnets
I have been struggling for about 2 hours with this problem. The solution is to set a different name for the network or propably use docker-compose down.
Networks settings is not much different between versions 2 and 3, but here is a good link to official doc (v3): https://docs.docker.com/compose/compose-file/compose-file-v3/#ipv4_address-ipv6_address

Set specify IP for a Docker container from docker-compose.yml

I have the docker-compose.yml file
web_server:
build: web_server/
ports:
- "8000:8000"
links:
- mongo
tty: true
environment:
SYMFONY__MONGO_ADDRESS: mongo
SYMFONY__MONGO_PORT: 27017
networks:
app_net:
ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10
networks:
app_net:
driver: bridge
enable_ipv6: true
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24
-
subnet: 2001:3984:3989::/64
mongo:
image: mongo:3.0
container_name: mongo
command: mongod --smallfiles
expose:
- 27017
I want to have a specifiy IP for my web_server for pass this in another applications.
But when I call command docker-compose up I recive the error:
ERROR: The Compose file '.\docker-compose.yml' is invalid because:
Unsupported config option for networks: 'app_net'
Unsupported config option for web_server: 'networks'
What is wrong?
I dont't known why you have this error but you can try to fix some points:
webserver networks must refer the networks declared below
you simplify your network configuration and start with ipv4 only
Here is a working configuration:
version: '2'
services:
webssl:
image: nginx:1.11.4-alpine
ports:
- "443:443"
- "80:80"
volumes:
- /data/nginx/webroot:/usr/share/nginx/html:ro
networks:
- dmz
networks:
dmz:
ipam:
driver: default
config:
- subnet: 172.77.0.1/24
ip_range: 172.77.0.0/24
gateway: 172.77.0.1

Resources