php - Issue connecting mysql database with docker container
Get the solution ↓↓↓I'm trying to set up a container for laravel, nginx and mariadb. Containers are running but my laravel application cannot access db container.
SQLSTATE[HY000] [1045] Access denied for user 'root'@'app.app-network' (using password: NO) (SQL: select * from information_schema.tables where table_schema = appdb and table_name = migrations and table_type = 'BASE TABLE')
Here is my docker-compose.yml
version: "3.7"
services:
app-db:
image: mariadb:10.5.6
container_name: app-db
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD : root
MYSQL_DATABASE: appdb
MYSQL_USER: root
MYSQL_PASSWORD: root
volumes:
- mariadbdata:/var/lib/mysql
- ./.dockerfile/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
app-api:
build:
context: .
dockerfile: Dockerfile
image: 'app-api'
container_name: 'app-api'
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
- .:/var/www
- ./.dockerfile/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
webserver:
image: nginx:alpine
container_name: 'app-webserver'
restart: unless-stopped
tty: true
ports:
- "8100:80"
- "8143:443"
volumes:
- ./:/var/www
- ./.dockerfile/nginx/conf.d:/etc/nginx/conf.d/
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
mariadbdata:
driver: local
My Dockerfile
FROM php:7.3-fpm
COPY composer.lock composer.json /var/www/
WORKDIR /var/www
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libzip-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libonig-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
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
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 . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
RUN chown -R www-data:www-data /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
I'm able to access my application onhttp://localhost:8100/
. But everytime i try to migrate I get the above error.
I'm using container bash for migrating.
My Database env for laravel
DB_CONNECTION=mysql
DB_HOST=app-db
DB_PORT=33060
DB_DATABASE=appdb
DB_USERNAME=root
DB_PASSWORD=password
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: unable to determine current zabbix database version: the table "dbversion" was not found.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.