docker compose自定义网络实现固定容器ip地址
(编辑:jimmy 日期: 2024/11/29 浏览:3 次 )
由于默认的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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:vitrualBox+ubuntu16.04安装python3.6最新教程及详细步骤