简体   繁体   English

Docker 容器,连接彼此的数据库,Nginx 代理和 WordPress

[英]Docker container, connect to each others databases, Nginx proxy and WordPress

I trying to figure out how docker network and that kind of stuff works.我试图弄清楚 docker 网络和那种东西是如何工作的。 So here is my setup, I have 2 docker containers with WordPress and mysql.所以这是我的设置,我有 2 个 docker 容器,其中包含 WordPress 和 mysql。 And theese two needs to be able to talk to each other thru direct database connections.这两个需要能够通过直接的数据库连接相互交谈。 And I have domain pointed to them also.我也有指向它们的域。 So I tried to set it up with an docker nginx proxy, but when i'm trying to visit that site thru the domain i'm getting 502 bat gateway.因此,我尝试使用 docker nginx 代理进行设置,但是当我尝试通过域访问该站点时,我得到了 502 bat 网关。

So my docker ngxinx proxy compose file looks like this, but with different ports:所以我的 docker ngxinx 代理撰写文件看起来像这样,但端口不同:

version: "3.1"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./certs:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: unless-stopped
networks:
  default:
    external:
      name: nginx-proxy

And both of my WordPress containers look like this:我的两个 WordPress 容器看起来像这样:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
      VIRTUAL_HOST: wp1.local
      VIRTUAL_PORT: 3000
    volumes:
      - ./wp:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    expose:
      - 3000

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    ports:
      - "8086:3306"

  mailhog:
    image: mailhog/mailhog
    ports:
      - "1025:1025" # smtp server
      - "8025:8025" # web ui

networks:
  default:
    external:
      name: nginx-proxy

volumes:
  db:

And I have added 127.0.0.1 wp1 to /etc/hosts我已将127.0.0.1 wp1添加到 /etc/hosts

But when I start the proxy and one of the WP containers I'm getting bad gateway.但是当我启动代理和其中一个 WP 容器时,我的网关很糟糕。 And I have no clue how to move forward.我不知道如何前进。 And maybe this is not even the right way todo it.也许这甚至不是正确的方法。 Becuase the problem i'm trying to solve is that wp2 needs to be able to connect to wp1 database thru custom pdo connection.因为我要解决的问题是 wp2 需要能够通过自定义 pdo 连接连接到 wp1 数据库。

I tried to explain as good as I can, but this is a new area for me when it comes to docker networks.我尽量解释清楚,但这对我来说是 docker 网络的新领域。 Also i'm running docker for windows with wsl2此外,我正在使用 wsl2 为 windows 运行 docker

You should add你应该添加

networks: 
 - default

to each service definition in docker-compose. docker-compose 中的每个服务定义。

version: '3.1'    

services:

  wordpress:
    image: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
      VIRTUAL_HOST: wp1.local
      VIRTUAL_PORT: 3000
    volumes:
      - ./wp:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    expose:
      - 3000
    networks:
      - default

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    ports:
      - "8086:3306"
    networks:
      -default

  mailhog:
    image: mailhog/mailhog
    ports:
      - "1025:1025" # smtp server
      - "8025:8025" # web ui
    networks:
      - default

networks:
  default:
    external:
      name: nginx-proxy

volumes:
  db:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM