docker compose Custom Network Implementation Fixed Container ip Address
- 2021-07-22 12:09:05
- OfStack
Because of the default bridge bridging network, the ip address changes when the container is restarted. In some scenarios, we want to fix the container IP address.
docker-compose is an orchestration tool for docker that creates networks, containers, etc. relative to command mode. Using configuration files is relatively more convenient and traceable to problems.
Paste docker-compose. yml files directly
version: '2'
services:
nginx:
image: nginx:1.13.12
container_name: nginx
restart: always
tty: true
networks:
extnetwork:
ipv4_address: 172.19.0.2
networks:
extnetwork:
ipam:
config:
- subnet: 172.19.0.0/16
gateway: 172.19.0.1
Description:
gateway is the gateway address subnet is the network number segment extnetwork is a custom network nameOur nginx container in the above configuration fixed ip to 172.19. 0.2
Example, custom network mode:
version: '2'
services:
nginx:
image: nginx:1.13.12
container_name: nginx
restart: always
networks:
extnetwork:
ports:
- 80:80
volumes:
- '/nginx/conf.d:/etc/nginx/conf.d'
nginx2:
image: nginx:1.13.12
container_name: nginx2
restart: always
networks:
extnetwork:
ipv4_address: 172.19.0.2
db:
image: mysql:5.7
container_name: db
volumes:
- /var/lib/mysql:/var/lib/mysql
restart: always
networks:
extnetwork:
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
image: wordpress:latest
container_name: wordpress
depends_on:
- db
ports:
- "8000:80"
restart: always
networks:
extnetwork:
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
extnetwork:
ipam:
config:
- subnet: 172.19.0.0/16
gateway: 172.19.0.1