简体   繁体   中英

wordpress container cannot connect to mariadb container

This is my Docker Compose file:

version: '2'
services:
  wordpress:
    image: wordpress
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: *****
      WORDPRESS_DB_NAME: wordpress
    links:
      - db:mysql
    ports:
      - 8000:80
    #network_mode: "none"
    restart: always

  db:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: *****
      MYSQL_DATABASE: wordpress
      MYSQL_USER: root
      MYSQL_PASSWORD: *****
    ports:
      - 8001:3306
    volumes:
      - ./configs/etc/mysql/my.cnf:/etc/mysql/my.cnf
    restart: always

And this is the error I got in docker logs:

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10

Line 10 is the "links" one.

I also got a 502 Bad Gateway error on the front page ( I installed Nginx as a proxy in my host os to forward port 80 to 8000)

Note that the nginx proxy access logs are all returned as a 302 not 502.

Ps: I am using the official Docker images

You don't need to open MariaDB port publicly, just expose port 3306 to other containers.

So in db replace:

ports:
  - 8001:3306

with:

expose:
  - "3306"

I believe that default database port in Wordpress is 3306, which is also default port in MariaDB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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