docker compose自定义网络实现固定容器ip地址
由于默认的bridge桥接网络,重启容器后会改变ip地址。在一些场景下我们希望固定容器IP地址。
docker-compose是docker的一个编排工具,相对于命令模式创建网络,容器等。使用配置文件相对来说更方便,可追溯问题。
直接粘贴docker-compose.yml文件
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
说明:
- gateway是网关地址
- subnet是网络号段
- extnetwork是自定义的网络名称
上面配置中我们的nginx容器固定ip为172.19.0.2
示例,自定义网络模式:
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
相关推荐
Wordpress0 2020-09-27
bluehost 2020-09-15
maccarty 2020-08-03
86520993 2020-07-28
WordPress 2020-07-04
李维山 2020-06-21
GimmeS 2020-06-14
WordPress 2020-06-10
shayuchaor 2020-06-10
liwf 2020-06-02
疯狂老司机 2020-05-26
zcl 2020-05-12
timewind 2020-05-11
86520993 2020-05-04
Lophole 2020-04-29
fraternityjava 2020-04-23
84911835 2020-04-16
maccarty 2020-04-09
WordPress 2020-04-07