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

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

Related

Docker Container ignores port 80

Since I use macvlan network configuration and give every container a different IP, I would like to access the services via port 80 and not the costum port. But this doesn't work for me.
Currently I created the following docker-compose file:
version: '3.3'
networks:
dockervlan:
external: true
volumes:
data:
services:
uptime-kuma:
image: louislam/uptime-kuma:latest
container_name: uptime-kuma
volumes:
- data:/app/data
ports:
- "80:3001" # <Host Port>:<Container Port>
restart: unless-stopped
networks:
dockervlan:
ipv4_address: 192.168.178.194
After that I still be able to access the service via http://192.168.178.194:3001 but not via http://192.168.178.194
Same for portainer:
version: '3'
services:
portainer:
image: portainer/portainer-ce:latest
container_name: portainer
ports:
- 80:9000
- 8000:8000
security_opt:
- no-new-privileges:true
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock
- data:/data
restart: unless-stopped
networks:
dockervlan:
ipv4_address: 192.168.178.200
networks:
dockervlan:
name: dockervlan
driver: macvlan
driver_opts:
parent: eth0
ipam:
config:
- subnet: "192.168.178.0/24"
ip_range: "192.168.178.192/26"
gateway: "192.168.178.1"
volumes:
data:
I also tried 3001:80 and 9000:80, which of course didn't work euther.
Where is my mistake?

docker does not expose static ip for container

i am trying to bind container with nginx to ip 172.16.238.10 , but for some reason docker ignores settings in docker-compose
#my docker-compose file
version: "3.9"
services:
nginx:
build: nginx/
ports:
- 80:80/tcp
volumes:
./dokcer/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
./dokcer/nginx/conf/hosts:/etc/hosts
./docker/project:/var/www/project
networks:
app_net:
ipv4_address: 172.16.238.10
php-fpm:
build: php-fpm/
ports:
- 9000:9000/tcp
volumes:
./dokcer/php-fpm/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
./dokcer/php-fpm/conf/hosts:/etc/hosts
./dokcer/project:/var/www/project
networks:
app_net:
ipv4_address: 172.16.238.11
networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
after build and launch, I look at the docker_app_net network and see that the nginx container has ip 172.16.238.2 although I expected it to be 172.16.238.10.
what could be the problem? I will be grateful for every answer because I am confused :c

How do I fix the docker-compose.yml? expected <block end>, but found '<scalar>'

How do I fix the docker-compose.yml? expected , but found ''
ERROR: yaml.parser.ParserError:
while parsing a block collection
in "testnode.yml", line 11, column 7
expected <block end>, but found '<scalar>'
in "testnode.yml", line 11, column 18
How can I fix this?
version: '3'
services:
uip-dns:
container_name: uip-dns
working_dir: /build
image: "alpine"
ports:
- "26668:26668"
volumes:
- {{build}}:/build:Z
command: ./dns
networks:
nsb_net:
ipv4_address: 192.167.233.2
node:
container_name: node
image: "tendermint-nsb/node"
ports:
- "26656-26657:26656-26657"
environment:
- PORT=:27667
- DB_DIR=./data100/
- TCP_AD=tcp://0.0.0.0:27667
- ID=100
- LOG=${LOG:-tendermint.log}
- UIP_CHAIN_DNS=http://uip-dns:26668
volumes:
- {{build}}:/tendermint:Z
command: node --rpc.laddr=tcp://0.0.0.0:26657 --proxy_app=tcp://0.0.0.0:27667
networks:
nsb_net:
ipv4_address: 192.167.233.233
networks:
nsb_net:
# external: true
driver: bridge
ipam:
driver: default
config:
-
subnet: 192.167.232.0/22
The {{build}} is not valid for docker-compose.yml. That looks like a golang template that would normally be expanded before passing the file to docker-compose. You'll want to replace that with a string, or defined variable. E.g.
version: '3'
services:
uip-dns:
container_name: uip-dns
working_dir: /build
image: "alpine"
ports:
- "26668:26668"
volumes:
- ${build_dir:-./build}:/build:Z
command: ./dns
networks:
nsb_net:
ipv4_address: 192.167.233.2
node:
container_name: node
image: "tendermint-nsb/node"
ports:
- "26656-26657:26656-26657"
environment:
- PORT=:27667
- DB_DIR=./data100/
- TCP_AD=tcp://0.0.0.0:27667
- ID=100
- LOG=${LOG:-tendermint.log}
- UIP_CHAIN_DNS=http://uip-dns:26668
volumes:
- ${build_dir:-./build}:/tendermint:Z
command: node --rpc.laddr=tcp://0.0.0.0:26657 --proxy_app=tcp://0.0.0.0:27667
networks:
nsb_net:
ipv4_address: 192.167.233.233
networks:
nsb_net:
# external: true
driver: bridge
ipam:
driver: default
config:
- subnet: 192.167.232.0/22
I also strongly recommend getting rid of all the fixed IP's for the subnet and containers. Those break portability, and the ability to scale, rolling update, and various other features. Use the published port and host IP address instead, or docker's DNS between containers, if at all possible (reference).

docker-compose multi-network private and internet

I need my container (local grpc_alpine) to have internet access whilst connected to a internal network. I have tried the following yaml file:
version: '2'
services:
gr1:
image: grpc_alpine
hostname: gr1
container_name: gr1
privileged: true
stdin_open: true
tty: true
volumes:
- "${PWD}/assets/grpc:/etc/grpc"
networks:
- default
- mynet:
ipv4_address: "10.10.10.11"
environment:
- DEFAULT_GATEWAY=10.10.10.254
networks:
mynet:
ipam:
config:
- subnet: 10.10.10.0/24
gateway: 10.10.10.254
but throws this issue:
compose.config.config.find: Using configuration files:
./docker-compose.yml ERROR: compose.cli.main.main: The Compose file
'./docker-compose.yml' is invalid because: services.gr1.networks
contains {"mynet": {"ipv4_address": "10.10.10.11"}}, which is an
invalid type, it should be a string
There's a small issue with your yaml configuration. The networks option in services expects a map rather than a list. Try this:
version: '2'
services:
gr1:
image: grpc_alpine
hostname: gr1
container_name: gr1
privileged: true
stdin_open: true
tty: true
volumes:
- "${PWD}/assets/grpc:/etc/grpc"
networks:
default:
mynet:
ipv4_address: "10.10.10.11"
environment:
- DEFAULT_GATEWAY=10.10.10.254
networks:
mynet:
ipam:
config:
- subnet: 10.10.10.0/24
gateway: 10.10.10.254

Provide static IP to docker containers via docker-compose

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.

Resources