Docker Compose can't connect 1 container to another?

I am trying to set up a Docker Compose, which worked in the past, but now for some reason I get an error that my wordpress cannot connect to mysql. What am I doing wrong?

version: "3.5"

services:

  mysql:
    container_name: ${NAME}_database
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ${DB_DATA}:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: ${DB_PASSWORD}

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: ${NAME}_phpmyadmin
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always

  apache:
    container_name: ${NAME}_web
    image: repo/wordpress:latest
    links:
      - mysql
    restart: always
    volumes:
      - ${WP_DATA}:/var/www/html
    environment:
      DB_NAME: wordpress
      DB_USER: wordpress
      DB_PASS: ${DB_PASSWORD}
      DB_HOST: mysql
      WP_URL: ${WP_DOMAIN}
      WP_ADMIN: ${WP_USER}
      WP_ADMIN_EMAIL: ${WP_EMAIL}
      WP_ADMIN_PASS: ${WP_PASSWORD}

Comments (1)

j's photo

you need to define networks and connect them to each other:

as an example

services:
  app:
    container_name: "app-fpm"
    build:
      context: ./app
    volumes:
      - ./data/mysql/:/var/lib/mysql/
      - ./data/run/mysqld:/run/mysqld
      - ./data/run/php:/run/php/
      - ./data/run/clamd:/var/run/clamd/
      - ./data/tmp:/tmp
      - ./app/php/fpm/php.ini:/etc/php/7.2/fpm/php.ini
      - ./app/app-entrypoint.sh:/app-entrypoint.sh
    links:
      - "mongodb:mongodb"
      - "memcached:memcached"
    networks:
      - dbnet
    depends_on:
      - web
      - mariadb
      - mongodb
      - memcached
  mariadb:
    container_name: "app-mysql"
    build:
      context: mariadb
    image: mariadb:tag
    volumes:
      - ./mariadb/my.cnf:/etc/mysql/my.cnf
      - ./data/mysql/data:/var/lib/mysql
      - ./data/run/mysqld:/run/mysqld
      - ./mariadb/app-entrypoint.sh:/app-entrypoint.sh
    ports:
      - "53306:3306"
    networks:
      - dbnet

docs.docker.com/compose/networking