Using docker-compose to implement micro-service, how to connect the container to the redis? of the physical machine

version: "3.6"
services:
  web:
    image: cfanbo/swoole4_php7:v1
    volumes:
      - /Users/sxf/sites/msgserve:/usr/src/myapp
    command: "php src/server.php start"

A daemon developed by swoole for the container needs to use a redis database. Redis is native. How to modify it? And what should be done at the code level?

Mar.23,2021

A relatively simple solution. According to the from-docker-container-sharpanswer-31328031" rel=" nofollow noreferrer "> stackoverflow answer , execute the following command in the container to display the current routing information. The" 172.17.0.1 "(this is this on my side, which may be different on your side) is the ip of the physical machine.

$ ip route show
default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0  src 172.17.0.4 

then set the firewall on the physical machine

$ iptables -A INPUT -i docker0 -j ACCEPT

then you can use the 172.17.0.1 ip to connect to the physical machine in the container.

in addition, a better way to implement this is to build a bridge in docker-compose.yml:

version: '2'
services:
  <container_name>:
    image: <image_name>
    networks:
      - dockernet

networks:
  dockernet:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.0.0/24
          gateway: 192.168.0.1

then the ip of 192.168.0.1 is the physical machine ip.

In addition, this question has been argued for a long time in issue . You can take a look at it.

Menu