Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

reza305's avatar

deploying laravel on ubuntu with docker

I'm new to docker and trying to deploy a laravel project on ubuntu 20. all the containers are up and i can see the welcome page in my browser, but I can't migrate the tables and it gives me the following error:

Illuminate\Database\QueryException

SQLSTATE[HY000] [1045] Access denied for user 'laravel_admin'@'app.my-project_app-network' (using password: NO) (SQL: select * from information_schema.tables where table_schema = my_database and table_name = migrations and table_type = 'BASE TABLE')

my-project is the project folder name. my_database is the database name that i created in mysql container using root user. app-network is the network in docker-compose.yml file. Here is my .env configurion for database:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=laravel_admin
DB_PASSWORD=somethigComplicated
DB_ROOT_PASSWORD=somethigComplicated

Here is my docker-compose.yml file:

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    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:latest
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql-files
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

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


#Volumes
volumes:
  dbdata:
    driver: local

Here is my Dockerfile:

FROM php:8.1-fpm

# 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 \
    libonig-dev \
    locales \
    libzip-dev \
    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
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 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

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

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

# Change current user to www
USER www

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

By the way, I am using https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose instructions. What have I done wrong? What is that 'app.my-project_app-network' in the error? I have two root users with '%' and 'localhost' host names.

0 likes
7 replies
Sinnbeck's avatar

What is the exact command you run when you get the error?

This?

docker-compose exec app php artisan migrate 
Sinnbeck's avatar

Did you at some point cache your config? The error isn't the same as you have in you env file

Error

laravel_admin'@'app.my-project_app-network

Env

DB_HOST=db

Or did you change the username/password in the env file at any point?

reza305's avatar

@Sinnbeck No, I didn't change it. I created a user in mysql as instructions in the link says. like this:

GRANT ALL ON my_database.* TO 'laravel_admin'@'%' IDENTIFIED BY 'somethingComplicated';

I think the reason it can't verify the user has something to do with the host: 'app.my-project_app-network';

reza305's avatar

I actually made it work by creating a user with 'app.my-project_app-network' host name. like this:

GRANT ALL ON my_database.* TO 'laravel_admin'@'app.my-project_app-network' IDENTIFIED BY 'somethingComplicated';

But despite I have created a volume for mysql image to persist the data, it only persists the user and database i create directly in mysql bash. It doesn't persist the data that is being inserted by laravel, like migrations.

why does laravel use 'app.my-project_app-network' host? why it hasn't been mentioned in that link instructions and is what i've done above, the correct solution?

Sinnbeck's avatar

@reza305 I am using docker and laravel for several projects, with a setup similar to that guide, and I have never experienced a problem like that. Maybe you should try removing the mysql container + volume and build it from scratch

reza305's avatar

@Sinnbeck I even tried that... but my main questions is about 'app.my-project_app-network' host. When I run this command in mysql bash:

select user,host from mysql.user;

i get the following result:

| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |

and when I create a user with '%' host, like the link instructions says, It gives me the access denied error. Then I run the following command in mysql bash:

create user 'laravel_admin'@'app.my-project_app-network' identified with mysql_native_password by 'somethingComplicated';
grant all privileges on my_database.* to 'laravel_admin'@'app.my-project_app-network';
select user,host from mysql.user;

It shows:

| user             | host                   |
+------------------+------------------------+
| root             | %                      |
| 7modir_admin     | app.7modir_app-network |
| mysql.infoschema | localhost              |
| mysql.session    | localhost              |
| mysql.sys        | localhost              |
| root             | localhost              |

And now it works. but still does not persist the data. I still can't figure out why that host name solved the problem.

Please or to participate in this conversation.