I was trying to create a database in my app using command:
php bin/console doctrine:database:create
After that I got an error:
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
My docker-compose file looks like that:
version: '3'
services:
php:
container_name: symfony_php
build:
context: ./php
volumes:
- ./symfony/:/var/www/symfony/
depends_on:
- database
networks:
- symfony
database:
container_name: symfony_postgres
image: postgres
restart: always
hostname: symfony_postgres
environment:
POSTGRES_DB: symfony_db
POSTGRES_USER: root
POSTGRES_PASSWORD: root
ports:
- "5432:5432"
volumes:
- ./postgres:/var/lib/postgres
networks:
- symfony
pgadmin:
container_name: symfony_pgadmin
image: dpage/pgadmin4
restart: always
ports:
- "5555:80"
depends_on:
- database
links:
- database
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: root
networks:
- symfony
nginx:
container_name: symfony_nginx
image: nginx:stable-alpine
build:
context: ./nginx
dockerfile: Dockerfile-nginx
volumes:
- ./symfony:/var/www/symfony
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
- symfony
networks:
symfony:
I'm not using any postgres config file, according to Symfony documentation, there's a config line in .env:
DATABASE_URL="postgresql://root:root#127.0.0.1:5432/symfony_db?serverVersion=11&charset=utf8"
I run netstat command and I got:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
I think there's a problem with binding my localhost address where my app's server is running to postgres. Does anyone know, how I can fix it?
In docker-compose all services can be reached by their names.
You need to change DB host address to database - as a service name in docker-compose
DATABASE_URL="postgresql://root:root#database:5432/symfony_db?serverVersion=11&charset=utf8"
Related
I'm quite bad at creating docker-compose and I currently have a problem when I try to build a new project.
After the build I can't access my mariadb server within my php app container, I've got this error:
PDO::__construct(): php_network_getaddresses: getaddrinfo for mariadb
failed: Name does not resolve
So I know something is misconfigured but I don't know what, yet. I tried many changes but nothing that worked.
Here is my current docker-compose.yml:
version: "3.8"
networks:
# used by some services (php) to communicate with other docker-compose.yaml
censored.com:
external:
name: censored.com
services:
app:
build:
context: .
target: symfony_php
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s
environment:
APP_ENV: dev
HOST: www.censored.lan
networks:
- default
- censored.com
volumes:
- ./:/srv/app:rw,cached
- ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.ini
- ${HOME}/.composer/auth.json:/root/.composer/auth.json
# If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
- ./var/cache:/srv/app/cache:rw
- ./var/log:/srv/app/logs:rw
depends_on:
- mariadb
extra_hosts:
- www.censored.lan:127.0.0.1
nginx:
build:
context: .
target: symfony_nginx
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
depends_on:
- app
environment:
NGINX_DOMAIN: www.censored.lan
ports:
- 8001:80
volumes:
- ./docker/nginx/templates/dev.conf.template:/etc/nginx/templates/default.conf.template:ro
- ./docker/nginx/rules/rules.dev.conf:/etc/nginx/rules.conf:ro
- ./public:/srv/app/public:ro
- ./src:/srv/app/src:ro
mariadb:
image: mariadb:10.7
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: changeme
networks:
- default
- censored.com
ports:
- '3307:3306'
restart: on-failure
volumes:
- db_data:/var/lib/mysql
volumes:
db_data: {}
Can someone help me to fix the issue please?
Thanks !
The problem wasn't related to my docker-compose configuration file.
My app is a PHP Symfony app and it was doing a "cache:clear" after the initial "composer install" during the build. The "cache:clear" was triggering calls to the database which wasn't ready yet.
To solve this I just had to set my mariadb version to my "DATABASE_URL" parameter in my Symfony app, to avoid useless database queries.
I have a WP project with following docker-compose configuration. When I try to connect my http://localhost to access the installation, I get 500 and I read this in the log:
PHP Fatal error: Uncaught mysqli_sql_exception: Connection refused in /var/www/html/web/wp/wp-includes/wp-db.php
I assume there is something wrong with the ip or the host but the weird thing is that phpmyadmin is working fine, and it connects with mysql without issues.
version: '3.9'
services:
nginx:
image: nginx:latest
container_name: ${APP_NAME}-nginx
ports:
- '80:80'
volumes:
- "./nginx/:/etc/nginx/templates/"
- ./src:/var/www/html:rw,cached
- ./certs:/etc/certs
environment:
- "NGINX_ENVSUBST_TEMPLATE_SUFFIX=.conf"
- "DOMAIN=${DOMAIN}"
depends_on:
- wordpress
restart: always
mysql:
image: mariadb:latest
container_name: ${APP_NAME}-mysql
command: --lower_case_table_names=2
volumes:
- './data/db:/var/lib/mysql:delegated'
environment:
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
restart: always
ports:
- '3307:3306'
wordpress:
build:
context: .
dockerfile: Dockerfile
container_name: ${APP_NAME}-wordpress
volumes:
- ./src:/var/www/html:rw,cached
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
XDEBUG_ENABLED: 1
XDEBUG_CONFIG: remote_host=host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
env_file:
- src/.env
depends_on:
- mysql
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: "${DB_HOST}"
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
ports:
- '8083:80'
links:
- mysql:mysql
mailhog:
container_name: ${APP_NAME}-mailhog
image: mailhog/mailhog
ports:
- "8025:8025"
- "1025:1025"
composer:
image: composer
container_name: ${APP_NAME}-composer
working_dir: /var/www/html
restart: 'no'
volumes:
- ./src:/var/www/html:rw,cached
My wordpress .env, among the other settings, set the host like the following:
DB_HOST="mysql:3307"
But I also tried
DB_HOST="mysql:3306"
or simply
DB_HOST="mysql"
Do you have any suggestions?
Thanks.
your Wordpress Instance and MySQL Instance isn't connected. Try adding networks on the docker-compose.yml
nginx:
...
networks:
- your-network-name
mysql:
...
networks:
- your-network-name
wordpress:
...
networks:
- your-network-name
and on the bottom of the file add:
networks:
your-network-name:
driver: bridge
this is the way to configure docker to be connected each other
This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(41 answers)
Closed 2 years ago.
I am a bit new to docker. I am developing a laravel application, I have my app container running.
Inside app container I want to connect to my locally hosted mongo db server, which is generally localhost:27017.
This is my docker-compose.yml file
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: bns_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker_files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: bns_web
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker_files/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: bns_db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: 12345
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./docker_files/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#REDIS Service
redis:
build:
context: ./docker_files/redis
dockerfile: Dockerfile
container_name: bns_redis
volumes:
- ./docker_files/redis:/data
ports:
- "6379:6379"
networks:
- app-network
# Laravel Echo Server
laravel-echo-server:
build:
context: ./docker_files/laravel-echo-server
dockerfile: Dockerfile
container_name: bns_echo_server
volumes:
- ./laravel-echo-server.json:/var/www/laravel-echo-server.json:ro
ports:
- "6001:6001"
links:
- redis
networks:
- app-network
# PHP-WORKER
php-worker:
build:
context: ./docker_files/php-worker
dockerfile: Dockerfile
container_name: bns_worker
volumes:
- ./php-worker/supervisord.d:/etc/supervisord.d
depends_on:
- app
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
So inside my app container when I run artisan commands such as docker-compose exec app php artisan migrate:fresh --seed mysql tables are migrated successfully but mongo db (which is not in any container but it is in host machine only) documents are not migrate/seeded and I get error :
No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '172.18.0.3:27017']
at /var/www/vendor/mongodb/mongodb/src/functions.php:431
427| // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
428| $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
429| }
430|
> 431| return $manager->selectServer($readPreference);
432| }
433|
Exception trace:
1 MongoDB\Driver\Manager::selectServer(Object(MongoDB\Driver\ReadPreference))
/var/www/vendor/mongodb/mongodb/src/functions.php:431
2 MongoDB\select_server(Object(MongoDB\Driver\Manager), [])
/var/www/vendor/mongodb/mongodb/src/Database.php:419
Please use the argument -v to see more details.
This is my ENV file:
MONGO_DATABASE=exchange
MONGO_HOST=172.18.0.3 # << This is the IP address of my app container <<
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=
Please do help me. Where am I going wrong ?
I don't know how but using static ip as 172.17.0.1 worked for me.
Now my ENV file is:
MONGO_DATABASE=exchange
MONGO_HOST=172.17.0.1
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=
Got reference from here
It must be well documented.
version: '3'
services:
db:
image: mysql:8.0.20
command: --default-authentication-plugin=mysql_native_password
volumes:
- "db_app:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- 3306:3306
networks:
- symfony
php:
build:
context: .
dockerfile: docker/php/Dockerfile
args:
TIMEZONE: ${TIMEZONE}
volumes:
- ./symfony/:/Users/admin/Downloads/symfony-docker-master/symfony
networks:
- symfony
nginx:
build:
context: .
dockerfile: docker/nginx/Dockerfile
volumes:
- ./symfony/:/Users/admin/Downloads/symfony-docker-master/symfony
ports:
- 8050:8050
networks:
- symfony
volumes:
db_app:
networks:
symfony:
The application starts, connects to the database via
DATABASE_URL=mysql://user:123#db:3306/db
But when performing migrations through the console, I get the error
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr
esses: getaddrinfo failed: nodename nor servname provided, or not known
As I understand it, due to the fact that the address is specified in the connection db.
How to make migrations?
You need to run console commands from inside of your php container
docker-compose exec php bin/console doctrine:migrations:migrate
You should connect to the container name.
These can be found by running docker container ls on your console.
It's easier when you specify the name in your compose file like so:
services:
db:
container_name: db # This can be anything
image: mysql:8.0.20
I Created a docker configuration for LEMP local server. I Tried to connect the Symfony app with MySQL database version 8 but the Connection is refused.
Error: SQLSTATE[HY000] [2002] Connection refused.
The problem is with DATABASE_URL in .env file required by Symfony 5.
What is the correct value for DATABASE_URL for a given docker-compose.yml configuration ? I run this on Docker for Windows. I was Able to connect to mysql bash with username root and root password.
docker-compose.yml:
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: sampleapp-memcached
mailhog:
image: mailhog/mailhog:latest
container_name: sampleapp-mailhog
ports:
- "8001:8025"
redis:
image: redis:alpine
container_name: sampleapp-redis
mysql:
image: mysql:8.0
container_name: sampleapp-mysql
working_dir: /app
volumes:
- .:/app
environment:
- MYSQL_ROOT_PASSWORD=Ur7HJWzZ2QK9
- MYSQL_DATABASE=maindatabase
- MYSQL_USER=sampleuser
- MYSQL_PASSWORD=FxGBWfJ86ykq
ports:
- "8002:3306"
elasticsearch:
image: elasticsearch:6.5.4
container_name: sampleapp-elasticsearch
webserver:
image: nginx:alpine
container_name: sampleapp-webserver
working_dir: /app
volumes:
- .:/app
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
php-fpm:
build: phpdocker/php-fpm
container_name: sampleapp-php-fpm
working_dir: /app
volumes:
- .:/app
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.4/fpm/conf.d/99-overrides.ini
Usually the DATABASE_URL string is a standard one:
mysql://user:pwd#host:port/db
In this case:
DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq#127.0.0.1:8002/maindatabase
Put it in your .env.local file and it should work. Note however that I work on unix and I do not have a Windows machine nearby.
Update
Following Alexander's suggestion, if you are running your Symfony app in a container (I didn't notice your webserver container, sorry!), then you should change the string to
DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq#mysql:3306/maindatabase
Note, however, that for container to container communications you should use the default port (or expose a new one).
As regards setting the serverVersion, you can use the query parameter or (IMHO a better approach) set it in your doctrine.yaml config file. In addition, note that your server version should match the one you've specified in your docker-compose file.