phpwordpressdockerdocker-composewp-cli

wp-cli, wordpress in docker-compose


I have this docker-compose file

version: '2'
services:
  php:
    image: wordpress:5.5-php7.4-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./htdocs/:/var/www/html:delegated
    ports:
      - '80:80'
    networks:
      - my-network

  wpcli:
    image: wordpress:cli-2-php7.4
    user: xfs
    depends_on:
      - db
      - php
    volumes:
      - ./htdocs:/var/www/html
    environment:
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password

  db:
    image: mysql:5.7
    restart: always
    environment:
      - DB_CONNECTION=mysql
      - DB_HOST=db:3306
      - MYSQL_DATABASE=mds
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - my-db:/var/lib/mysql
    #volumes:
    #  - ~/mysql:/var/lib/mysql
    networks:
      - my-network

  composer_installation:
    container_name: composer_installation
    image: composer:1.10.19
    command: ["composer", "install"]
    volumes:
      - ./htdocs/:/app

volumes:
  my-db:
networks:
  my-network:
    driver: bridge

and php, mysql and wordpress works fine, i can connect from my host to the db with a client not problem using 0.0.0.0:3306 but when I try to use wp-cli I can see in the logs

Error: Error establishing a database connection.

I have being changing the volumes for hours but not luck,

Any tip welcome


Solution

  • I know that this is late, but for posterity:

    The wpcli container is missing a network entry, so it cannot find the db container by name. You need to add

       networks:
          - my-network
    

    to the wpcli container section of docker-compose.yml