How do I connect to a MySQL container from my PHP container so I can run my wp-cli command?

Question

I am using wp-cli and trying to run the wp command wp config create --dbname=wp_database --dbuser=wordpress-user --dbpass=password-example --locale=en_GB

I use a similar Docker environment for Laravel and never have a problem running MySQL commands from the PHP container. My DockerFile looks like:

version: '3.2'
networks:
  wordpress-local:
    driver: bridge
services:
  php:
    build: build/php
    environment:
      APP_VERSION: 1
      ACF_PRO_KEY: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    volumes:
      - $WORDPRESS_ROOT:/var/www/wordpress-app
      #- $WORDPRESS_VENDOR
    depends_on:
      - mysql
    links:
      - redis
    networks:
      - wordpress-local

  mysql:
    image: mysql:5.7
    restart: always
    user: "1000:50"
    ports:
      - 3307:3306
    environment:
      MYSQL_DATABASE: wp_database
      MYSQL_USER: wordpress-user
      MYSQL_PASSWORD: password-example
      MYSQL_ROOT_PASSWORD: password-example
    volumes:
      - $WORDPRESS_DOCKER_MYSQL:/var/lib/mysql
    healthcheck:
      test: "/usr/bin/mysql --user=root --password=$MYSQL_ROOT_PASSWORD --execute "SHOW DATABASES;""
      timeout: 20s
      retries: 5
    networks:
      - wordpress-local
  nginx:
    build: build/nginx
    restart: always
    ports:
      - 443:443
    depends_on:
      - php
      - mysql
    volumes:
      - $WORDPRESS_ROOT:/var/www/wordpress-app
    networks:
      - wordpress-local
  redis:
    image: redis
    ports:
      - 6379:6379
    networks:
      - wordpress-local
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: wordpress_pma
    restart: always
    environment:
      PMA_HOST: mysql
      PMA_USER: root
      PMA_PASSWORD: password
    ports:
      - "8080:80"
    networks:
      - wordpress-local

When I try running the wp config command I get the error message Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

0
NickC 2 months 0 Answers 14 views 0

Leave an answer