docker容器使用不同IP


工作記錄又一篇

此需求的目的是解決一個奇怪的bug:多個docker搭載php服務在同時收到請求,並去請求同一個數據庫時(只有查操作),查數據庫的tcp請求會變成串行,即一個請求完了之后第二個請求才開始發起,雖然這個問題還是沒解決,不過給容器配置不同IP這個教程也比較少,所以記錄一下;另:如有人碰到上述問題希望可以指點一下解決方案😂

回歸正文,docker默認的bridge在外部訪問時,會直接訪問host宿主機IP,所以如果想將docker配置另外IP,則需要network使用macvlan模式

docker-compose.yml

version: "3"

services:
  clnmp:
    image: test_ns:latest
    container_name: test
    networks:
      web:
        ipv4_address: "192.168.0.2"
      db:
        ipv4_address: "10.10.10.2"
    environment:
      TZ: "Asia/Shanghai"
      LANG: "C.UTF-8"
    volumes:
      - "/home/conf/php.ini:/usr/local/php/etc/php.ini"
    extra_hosts:
      - "www.testdomain.com:127.0.0.1"
    restart: always
    privileged: true
    command: /bin/bash -c "/etc/init.d/php-fpm start && /etc/init.d/nginx start && tail -f /dev/null"

networks:
    web:
        driver: "macvlan"
        driver_opts:
          parent: "eth0"
        ipam:
          config:
            - subnet: "192.168.0.0/24"
            #- ip_range: "192.168.0.0/24"
            #- gateway: "192.168.0.1"
    db:
       driver: "macvlan"
       driver_opts:
         parent: "eth1"
       ipam:
         config:
         - subnet: "10.10.10.0/24"

上面配置了雙網卡macvlan,通過192.168.0.X網段進行訪問,通過10.10.10.X網段進行數據庫讀寫

附docker安裝過程(包含docker-compose),以便平時使用

yum -y remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine;
systemctl disable --now firewalld;
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config;
yum install -y yum-utils;
yum install -y epel-release;
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo;
yum clean all;
yum install -y docker-ce ;
systemctl start docker;
systemctl enable --now docker;

#yum install -y docker-compose;
yum -y install python3-pip;
pip3 install pip -U -i https://pypi.douban.com/simple;
pip3 install docker-compose -i https://pypi.douban.com/simple;
#------------修改docker root-----------
cat << EOF > /etc/docker/daemon.json 
{
"data-root": "/home/docker-data",
"registry-mirrors": ["https://1rqfztnd.mirror.aliyuncs.com"]

}
EOF
systemctl restart docker;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM