亲宝软件园·资讯

展开

docker compose自定义网络实现固定容器ip地址

人气:5

由于默认的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

说明:

上面配置中我们的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
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论