Can't manage to get mysql working on docker-compose project

Hi All,

I am new to Docker and am trying to build up a Laravel project with nginx, mysql and php through docker-compose. Before you say the question is duplicate, yes - it is. However, I can’t get it to work and need help from you guys.

Issue: database is refusing my root password.
Dockerfile:


# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
#RUN groupadd -g 1001 www-data
#RUN useradd -u 1001 -ms /bin/bash -g www-data www-data

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www

# Change current user to www
USER www-data

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

docker-compose.yml:

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
       - ./:/var/www
       - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: enmu
      MYSQL_ROOT_PASSWORD: *root_pass*
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf

    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local

.env

----
APP_NAME=Laravel
APP_ENV=local
APP_KEY=*mykey*
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=enmu
DB_USERNAME=enmu
DB_PASSWORD=*my_password*
----

Everything is linked, all is working fine but when I get to connect to mysql with mysql -u root -p root_pass, it is returning ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
In browser, when I try to connect to my Voyager admin panel(/admin), I am receiving this message SQLSTATE[HY000] [1045] Access denied for user 'laraveluser'@'app.laravel_app-network' (using password: YES)

My permissions are all set to www-data as the owner of the project, 755 for directories and 644 for files.

I would be grateful if you help me, guys. Thanks in advance!

Cheers

I kept having issues with the MySQL images. So I switched to MariaDB and it’s working fine. Unless you have to use MySQL for some very specific reason, try using MariaDB (works the exact same way as MySQL)