I am following The Docker Tutorial by Andrew Schmelyun on laracasts. All was going well I could run migrations using artisan but only if I am using sqlite db. when I change to mysql db I get this error.
Thank you ahead of time for any help.
I know sail works well but can you deploy sail to production?
/Users/henrylemmon/Sites/laravel-docker.test > docker-compose run --rm artisan migrate:fresh
[+] Creating 1/0
✔ Container laravel-dockertest-mysql-1 Running 0.0s
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'172.18.0.4' (using password: YES) (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'laravel' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:813
809▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
810▕ );
811▕ }
812▕
➜ 813▕ throw new QueryException(
814▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
815▕ );
816▕ }
817▕ }
+33 vendor frames
34 artisan:13
Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))
Here are the files for docker and I am using laravel 11.
.env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
docker-compose.yml
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
ports:
- 80:80
volumes:
- ./src:/var/www/html
depends_on:
- mysql
- php
mysql:
image: mysql:latest
ports:
- 3306:3306
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
volumes:
- ./mysql:/var/lib/mysql
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./src:/var/www/html
composer:
build:
context: .
dockerfile: composer.dockerfile
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
npm:
image: node:current-alpine
volumes:
- ./src:/var/www/html
entrypoint: ["npm"]
working_dir: /var/www/html
artisan:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- mysql
entrypoint: ["php", "/var/www/html/artisan"]
composer.dockerfile
FROM composer:2
ENV COMPOSERUSER=laravel
ENV COMPOSERGROUP=laravel
RUN adduser -g ${COMPOSERGROUP} -s /bin/sh -D ${COMPOSERUSER}
nginx.dockerfile
FROM nginx:stable-alpine
ENV NGINXUSER=laravel
ENV NGINXGROUP=laravel
RUN mkdir -p /var/www/html/public
ADD nginx/default.conf /etc/nginx/conf.d/default.conf
RUN sed -i "s/user www-data/user ${NGINXUSER}/g" /etc/nginx/nginx.conf
RUN adduser -g ${NGINXGROUP} -s /bin/sh -D ${NGINXUSER}
php.dockerfile
FROM php:8-fpm-alpine
ENV PHPGROUP=laravel
ENV PHPUSER=laravel
RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}
RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf
RUN mkdir -p /var/www/html/public
RUN docker-php-ext-install pdo pdo_mysql
CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]
here are the mysql logs
/Users/henrylemmon > cd Sites/laravel-docker.test
/Users/henrylemmon/Sites/laravel-docker.test > docker-compose logs mysql
mysql-1 | 2024-06-30 05:31:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.4.0-1.el9 started.
mysql-1 | 2024-06-30 05:31:51+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql-1 | 2024-06-30 05:31:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.4.0-1.el9 started.
mysql-1 | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
mysql-1 | 2024-06-30T05:31:51.870589Z 0 [System] [MY-015015] [Server] MySQL Server - start.
mysql-1 | 2024-06-30T05:31:51.999942Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.4.0) starting as process 1
mysql-1 | 2024-06-30T05:31:52.001025Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
mysql-1 | 2024-06-30T05:31:52.003463Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql-1 | 2024-06-30T05:31:52.062876Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql-1 | 2024-06-30T05:31:52.166437Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql-1 | 2024-06-30T05:31:52.166489Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql-1 | 2024-06-30T05:31:52.168812Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql-1 | 2024-06-30T05:31:52.178376Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.4.0' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
mysql-1 | 2024-06-30T05:31:52.435588Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock