When I try to run the command php artisan migrate, an error occurs:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = docker and table_name = migrations and table_type = 'BASE TABLE')
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql mysqli
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8088:80"
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8.0
container_name: db
ports:
- "3307:3306"
expose:
- "3306"
volumes:
- ./cache/mysql:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: docker
MYSQL_USER: root
MYSQL_PASSWORD: root
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
depends_on:
- nginx
- php
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
.env:
DB_CONNECTION=mysql
DB_HOST="db"
DB_PORT=3306
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=root
I try too:
DB_HOST=127.0.0.1
DB_HOST=localhost
thank you very good
In docker-compose.yml
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
php:
..
..
environment:
DB_HOST: mysql
DB_NAME: db_name
DB_USERNAME: root
DB_PASSWORD: root
networks:
- laravel
// map volume
volumes:
db_mysql:
In MySQL Dockerfile
FROM mysql:8.0
please chnage the ports in docker-compose.yml File , it works for me
ports:
- 8889:3306
----My-> docker-compose.yml File ---
version: '3.8'
services:
Web Server Service
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- app
- db
#Database Service
db:
image : mysql
container_name : mysql
restart: unless-stopped
tty: true
ports:
- 8889:3306
volumes:
# - ./mysql/data:/var/lib/mysql
environment:
# MYSQL_DATABASE: laravel_blogs
## MYSQL_ROOT_PASSWORD: password
# MYSQL_ROOT_HOST: localhost
# SERVICE_NAME: mysql
# SERVICE_TAGS: dev
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ~/mysql:/var/lib/mysql
app:
container_name: app
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
working_dir: /var/www
phpMyAdmin:
# phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
- "3400:80"
depends_on:
- db
links:
- db
#pgadmin:
#image: dpage/pgadmin4
#container_name: pgAdmin
#ports:
# - "5050:80"
#depends_on:
# - db
#environment:
# PGADMIN_DEFAULT_EMAIL: hanieasemi76#gmail.com
PGADMIN_DEFAULT_PASSWORD: password
Related
I'm new in Docker and JS technologies so don't judje too hard. I'm trying to run npm run dev from node.js container (called npm). It goes right and shows that css and scripts are available at localhost:3009, but they're not.
Am I right that the problem is with node.js container, which is trying to restart all the time and doesn't exposes any ports? If yes, how can I fix it? Thank you in advance.
Here is my docker-compose.yml and vite.config.js
version: '3'
networks:
laravel:
services:
site:
build:
context: ./dockerfiles
dockerfile: nginx.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: nginx
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- redis
- mysql
- mailhog
networks:
- laravel
mysql:
image: mariadb:10.6
container_name: mysql
restart: unless-stopped
tty: true
volumes:
- ./db-data:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: php
restart: unless-stopped
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
redis:
image: redis:alpine
container_name: redis
restart: unless-stopped
ports:
- 6379:6379
networks:
- laravel
composer:
build:
context: ./dockerfiles
dockerfile: composer.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
restart: always
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
- 3009:3009
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- laravel
vite.config.js
import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
server: {
port: 3009
},
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
});
You should use the "--host" key starting your Vite
I'm struggling for a few hours now and cannot proprly set webpack-mix browserSync functionality for docker development enviroment.
I'd be glad to get some help!
Thanks in advace.
My docker-compose.yml:
version: '3.7'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: vokl
container_name: vokl-app
ports:
- 80:80
- 3000:3000
- 3001:3001
volumes:
- ./:/var/www/html
networks:
- vokl
depends_on:
- mysql
mysql:
image: 'mariadb:latest'
container_name: vokl-db
restart: unless-stopped
ports:
- 3306:3306
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- ./.docker/dbdata:/var/lib/mysql
networks:
- vokl
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: vokl-phpmyadmin
environment:
- PMA_HOST=mysql
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=root
- MYSQL_PASSWORD=root
depends_on:
- mysql
ports:
- 8765:80
networks:
- vokl
networks:
vokl:
driver: bridge
webpack-mix:
mix.browserSync({
host: 'vokl-app.test',
proxy: 'app',
notify: false,
open: 'external'
});
I'm trying to dockerize existing Laravel + Vue.js application, however, when trying to access database getting an error:
Database hosts array is empty.
It looks like that connection fails from PHP to MYSQL (but MySQL image is running and I can connect to it from terminal). Not sure how to proceed with this. It doesn't look to me a misconfiguration. Is there a way to debug it?
Here is what I have:
docker-compose.yml:
version: '3'
networks:
backoffice:
services:
backoffice_nginx:
image: nginx:stable
container_name: backoffice_nginx
restart: unless-stopped
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_mysql:
image: mysql:8.0.13
container_name: backoffice_mysql
restart: unless-stopped
tty: true
ports:
- "33067:3306"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password']
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: dbuser
MYSQL_PASSWORD: mypass
MYSQL_ROOT_PASSWORD: myrootpass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql/db:/var/lib/mysql
- ./mysql/conf.d:/etc/mysql/conf.d
networks:
- backoffice
backoffice_php:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_php
restart: unless-stopped
volumes:
- ./src:/var/www/html
networks:
- backoffice
backoffice_composer:
image: composer:latest
container_name: backoffice_composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- backoffice
php.dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
RUN docker-php-ext-install bcmath pdo pdo_mysql
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
DB configs part in the .env:
DB_CONNECTION=mysql
DB_HOST=backoffice_mysql
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=myrootpass
Here are running containers:
Any ideas?
Thanks.
You are not sharing your .env file with your container. Map your whole . Volume to the container instead of only your ./src directory.
I'm using docker with laravel project but im struggling to it to connect to the laravel container
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
redis:
image: redis:alpine
container_name: my-asset-management-redis
mysql:
image: mysql:8.0
container_name: my-asset-management-mysql
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=app
- MYSQL_USER=user
- MYSQL_PASSWORD=pass
ports:
- "8085:3306"
webserver:
image: nginx:alpine
container_name: my-asset-management-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8083:80"
php-fpm:
build: phpdocker/php-fpm
container_name: my-asset-management-php-fpm
working_dir: /application
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-extras.ini:/etc/php/7.4/fpm/conf.d/99-extras.ini
And I have this as my REDIS_HOST=my-asset-management-redis in my env
But i keep getting this: Predis\Connection\ConnectionException : php_network_getaddresses: getaddrinfo failed: No such host is known. [tcp://my-asset-management-redis:6379]
I have the redis password set as NULL for redis in env as well.
I'm getting PDOException::("SQLSTATE[HY000] [2002] Connection refused") when running docker-compose exec app php artisan migrate on the terminal
here's my db config on .env file
DB_CONNECTION=mysql
DB_HOST=172.20.0.4
DB_PORT=3306
DB_DATABASE=queue_db
DB_USERNAME=root
DB_PASSWORD=root
I also inspect the container ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' worker-db
Output: 172.20.0.4
here's my docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/Dockerfile
image: digitalocean.com/php
container_name: worker-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: worker-webserver
restart: unless-stopped
tty: true
ports:
- "8090:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql
container_name: worker-db
# restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: queue_db
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
There's a weird error on the stack trace where ip is different i'm not sure why it's getting a different ip, but in my .env file i'm using the container ip which is "172.20.0.4"
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=172.20.0.3;port=3306;dbname=queue_db", "root", "root", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Thanks in advance.
ok, after i tried to run php artisan config:clear it's working now.