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;
posted @ 2022-01-17 17:13  seas  阅读(953)  评论(0编辑  收藏  举报