Link between symfony and docker - php

I'm on a symfony project and i'm using docker here's my docker-compose.yml :
version: '3.7'
services:
db:
image: mysql:latest
container_name: ruakh_db
restart: always
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
networks:
- dev
phpmyadmin:
image: phpmyadmin:latest
container_name: ruakh_phpmyadmin
restart: always
depends_on:
- db
ports:
- 8080:80
environment:
PMA_HOST: db
networks:
- dev
maildev:
image: maildev/maildev
container_name: ruakh_mail_dev
restart: always
command: bin/maildev --web 80 --smtp 25 --hide-exetensions STARTTLS
ports:
- 8081:80
networks:
- dev
apache:
build: php
container_name: ruakh_www
ports:
- 8088:80
volumes:
- ./php/vhosts:/etc/apache2/sites-enabled
- ./:/var/www
restart: always
networks:
- dev
networks:
dev:
volumes:
db-data:`
here's the database url used in my symfony project :
DATABASE_URL=mysql://root:root#ruakh_db/ruakh
I'm trying to run a php bin/console make:migration however when i'm running I get this error :
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
I'm assuming that the database url is incorrect, when I changed it to :
DATABASE_URL=mysql://root:root#127.0.0.1:8080/ruakh
This error is thrown:
An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away
How can I manage to connect my symfony project to my docker database ?

There is a couple things wrong with your DATABASE_URL.
DATABASE_URL=mysql://root:root#ruakh_db/ruakh
You don't set the password in your service config
You have the wrong host name
You don't specify the database
You are using native authentication, which must be enabled when using MySQL 8
(optional) your database port is not exposed
Password
In your docker-compose.yml. you do not specify the root password and instead allow for an empty password. You can set the root password using an environment variable:
services:
db:
...
environment:
MYSQL_ROOT_PASSWORD: example
Database name
In order to create a default database named ruakh you need to provide another environment variable MYSQL_DATABASE.
Host
The next issue is the host name in your DATABASE_URL: ruakh_db. This is the name of the container, but not the name of the service (i.e. the host in the virtual network that docker-compose sets up). This should be db instead.
Port
You will not be able to access your mysql database from outside the docker container, because you do not expose any ports. That is not necessarily an issue, but you should be aware of it. Looking at your other database url DATABASE_URL=mysql://root:root#127.0.0.1:8080/ruakh this will not work, because the port 8080 is for the web interface provided by phpmyadmin. It is not the port of the actual database. Also, the port for the database is not exposed. You probably want to add:
services:
db:
...
ports:
- 3306:3306
Authentication
Another issue, you will face is using the image mysql:latest this will use MySQL 8.0 which does not allow the authentication mechanism you want to use by default. You will have to change the command executed when running the container or downgrade to MySQL 5.7. If you want to keep MySQL 8, you should add this:
services:
db:
...
command:
- 'mysqld'
- '--character-set-server=utf8mb4'
- '--collation-server=utf8mb4_unicode_ci'
- '--default-authentication-plugin=mysql_native_password'
Summary
This is roughly what your db service should look like to work with your DATABASE_URL:
db:
image: mysql:latest
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: ruakh
MYSQL_ROOT_PASSWORD: 'root'
command:
- 'mysqld'
- '--character-set-server=utf8mb4'
- '--collation-server=utf8mb4_unicode_ci'
- '--default-authentication-plugin=mysql_native_password'

Related

How to set prestashop database server from docker-compose?

I'm moving my prestashop installation to docker containers. I have a container with prestashop, mariadb and phpmyadmin on Ubuntu. I've set the environment variables as suggested here: https://githubmemory.com/repo/PrestaShop/docker. Here is my docker-compose:
version: "2"
services:
prestashop:
image: prestashop/prestashop
networks:
mycustomnetwork:
ports:
- 82:80
links:
- mariadb:mariadb
depends_on:
- mariadb
volumes:
- ./src:/var/www/html
- ./src/modules:/var/www/html/modules
- ./src/themes:/var/www/html/themes
- ./src/override:/var/www/html/override
environment:
- PS_DEV_MODE=1
- DB_SERVER=mariadb
- DB_USER=user_8
- DB_PASSWD=password_8
- DB_NAME=db_8
- PS_INSTALL_AUTO=0
mariadb:
image: mariadb
networks:
mycustomnetwork:
volumes:
- db_data:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=db_8
- MYSQL_USER=user_8
- MYSQL_PASSWORD=password_8
phpmyadmin:
image: phpmyadmin/phpmyadmin
networks:
mycustomnetwork:
links:
- mariadb:mariadb
ports:
- 1235:80
depends_on:
- mariadb
environment:
- PMA_HOST=mariadb
- PMA_USER=user_8
- PMA_PASSWORD=password_8
volumes:
db_data:
networks:
mycustomnetwork:
The database works, I can connect to it via phpmyadmin. However in the prestashop logs I get this error:
Link to database cannot be established: SQLSTATE[HY000] [2002] No such file or directory at line 127 in file classes/db/DbPDO.php
I modified the code, which threw this error so it prints the variables it uses to try to establish the connection ($this->server, $this->user, $this->password, $this->database) and I got this: localhost user password db. These values are from app/config/parameters.php and not the environment variables I set in the docker-compose. What should I do so it uses the values I provided in docker-compose instead of the ones from parameters.php?
I think Prestashop is reading app/config/parameters.phhp file before parsing env vars.
To bypass this, you should force PShop to read conf from env vars.
Use getenv(<ENV_NAME>)
'database_host' => getenv('DB_SERVER')

Cannot connect Wordpress on external dbgetaddrinfo failed: Name or service not known

I would like to run my Wordpress site on Docker, and I want to connect the Wordpress database to another container which have only the databases of all my sites.
For doing so, I've created a LAMP container using the following docker-compose.yml:
version: "3"
services:
web:
image: webdevops/php-apache:alpine-php7
ports:
- "4500:80"
volumes:
- ./www:/app
- ./uploads.ini:/opt/docker/etc/php/php.ini
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "8088:80"
as you can see I've installed Apache as service using the webdevops image, this return the following:
Then, I've created a new container which have the Wordpress instance:
version: '3'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wp/wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: lamp_db_1:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
volumes:
dbdata:
wp-content:
as you can see I mount the wp-content folder since I already have a Wordpress installation with plugins and media... then I tried to connect this container to lamp_db_1 container but when I run this using:
docker-compose up --build
I get:
MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
what I did wrong?
How can I connect the wordpress container to the LAMP container?
You can specify a custom network in your LAMP stack and have your Wordpress stack and other containers defined in other Compose files use this network by using network.external parameter in Compose.
Being on the same network, you'll be able to join your lamp_db container using it's container name as hostname.
A complete example:
docker-compose.yml for LAMP stack:
version: '3.5'
services:
db:
# This will be the hostname for your DB container on the network
container_name: lamp_db
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
# Have db container join our db network
networks:
- db_network
# Ensure our custom network is managed by this stack
networks:
db_network:
name: db_network
docker-compose.yml for Wordpress stack:
version: '3.5'
services:
wordpress:
image: wordpress:latest
ports:
- "8000:80"
environment:
# Name of the db container
WORDPRESS_DB_HOST: lamp_db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
# Have wordpress container join our db network
networks:
- db_network
# Declare db_network as external with it's name
# Network won't be created by this stack but must exists before running
# See https://docs.docker.com/compose/compose-file/#external-1
networks:
db_network:
external: true
name: db_network
And a generic example any service that would need to use our database:
version: '3.5'
services:
some_container:
image: some-image:latest
# Have this container join our db network
networks:
- db_network
# Note: make sure to reference db container by it's hostname lamp_db
networks:
db_network:
external: true
name: db_network
Notes:
db_network is managed via LAMP stack. When uping the LAMP stack, Docker Compose will ensure this network is created. Other stacks using this network should declare it as external: true with it's name
You'll need version: '3.5' or more to be able to use network.name config
It seems the reason you are running into this issue is due to the fact that you have two separate services. One contains your LAMP stack and the other contains your wordpress image.
When you run docker-compose it sets up a single network for your app. Thus your wordpress image isn't on the same network as your LAMP stack.
You can add your wordpress container to your LAMP docker-compose.yml file such as
version: "3"
services:
web:
image: webdevops/php-apache:alpine-php7
ports:
- "4500:80"
volumes:
- ./www:/app
- ./uploads.ini:/opt/docker/etc/php/php.ini
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "8088:80"
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wp/wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
volumes:
dbdata:
wp-content:
And unless you are doing something specific with your php-apache image you can actually remove it at this point.
Another approach you could take if you do not want everything in the same docker-compose file is that you can manually define the networks and get your containers to join them. The answers on this question show how to do that fairly easily.

Mysql - Docker: Connection issue

My docker-compose.yml looks like this:
version: '3.3'
services:
frontend:
build: frontend
container_name: yii-frontend
ports:
- 20080:80
volumes:
# Re-use local composer cache via host-volume
- ~/.composer-docker/cache:/root/.composer/cache:delegated
# Mount source-code for development
- ./:/app
networks:
- my-marian-net
backend:
build: backend
container_name: yii-backend
ports:
- 21080:80
volumes:
# Re-use local composer cache via host-volume
- ~/.composer-docker/cache:/root/.composer/cache:delegated
# Mount source-code for development
- ./:/app
networks:
- my-marian-net
db:
image: mysql:8.0
container_name: mysql8
command: --user=root --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=verysecret
- MYSQL_DATABASE=yii2advanced
- MYSQL_USER=yii2advanced
- MYSQL_PASSWORD=secret
ports:
- 6033:3306
networks:
- my-marian-net
networks:
my-marian-net:
driver: bridge
I get an error message:
'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:
nodename nor servname provided, or not known'
Solutions I have tried:
Ping from backend and frontend container to mysql8: docker exec -ti yii-frontend ping mysql8 (It works!)
Manually connect each container to the same network (my-marian-net) docker network connect my-marian-net mysql8 (same for each container)
I have connected into the mysql container and test connection there, it works.
I have connected from containers to mysql8 container MySQL service and it works.
So far no luck getting connected. In my code, I am using "db" as hostname since I am using bridge mode.
After trying different solutions, I have isolated the issue to a connection from
outside docker, Any suggestion?
I am using macOS Mojave. Docker version 19.03
The issue was simpler than I thought, from outside container "db" service does not exist, so no possibility to get connected.
In the future when I run console commands from local, I will change host in database connection to use localhost instead of "db" or get connected to the container itself and run them from inside.
Thanks to #Smankusors for the help.

PHP app cannot connect to docker mysql container at 127.0.0.1

Mysql is in it's own docker-compose.yml as I want a mysql server up and running that any other php application can connect to. So I do not have php and mysql in the same docker-compose.yml. From the php application, I can connect to mysql if I use the mysql container's gateway ip address by looking it up and then hard coding it into the php application. docker inspect mysql-db. But docker will change that 172... ip address each time mysql restarts so that is not ideal for development.
I can connect to mysql via mysql -h 127.0.0.1 no problem, but from the php application if I try to use 127.0.0.1 I get connection refused. I can only connect if I use the 172... gateway ip address.
How do I get the mysql container listening for connections from the host to 127.0.0.1?
docker-compose.yml for mysql
version: "3"
services:
mysql:
container_name: mysql-db
image: mysql
build:
dockerfile: Dockerfile
context: ./server/mysql
environment:
- MYSQL_ROOT_PASSWORD=admin
volumes:
- ./data/mysql:/var/lib/mysql
ports:
- 3306:3306
docker-compose.yml for php
version: "3"
services:
nginx:
container_name: nginx_myapp
image: nginx
build:
dockerfile: Dockerfile
context: ./server/nginx
ports:
- 80:80
- 443:443
volumes:
- ./app:/var/www/html
networks:
- myapp
php:
container_name: php_myapp
image: php:7.3-fpm
build:
dockerfile: Dockerfile
context: ./server/php-fpm
environment:
CI_ENV: development
volumes:
- ./app:/var/www/html
networks:
- myapp
networks:
myapp:
127.0.0.1 is the loopback address. It points to localhost. In the context of docker, localhost is the container itself. There is no db running on your php container so the connection will never succeed.
What you need to do is to configure the default network in you mysql compose file so that you will predictably control its name for later convenience (else it will be calculated from your compose project name which could change if you rename the containing folder...):
Important note: for the below to work, you need to use compose file version >= 3.5
---
version: '3.7'
#...
networks:
default:
name: shared_mysql
You can now use that shared_mysql network as external from any other compose project.
version: '3.7'
services:
nginx:
#...
networks:
- myapp
php:
#...
networks:
- myapp
- database
networks:
myapp:
database:
external: true
name: shared_mysql
You can then connect to mysql from your php container using the service name mysql (e.g. mysql -h mysql -u user -p)
Reference: https://docs.docker.com/compose/networking/
Few solutions for you
1) you can duplicate mysql section in each file using same volume path, in that case when you start project you will have same databases
project1
version: "3.2"
services:
mysql:
volumes:
- /var/mysql:/var/lib/mysql
php:
build:
context: './php/'
project2
version: "3.2"
services:
mysql:
volumes:
- /var/mysql:/var/lib/mysql
php:
build:
context: './php/'
2) you can connect using host.docker.internal or macos (docker.for.mac.localhost) directly to your host machine more information here From inside of a Docker container, how do I connect to the localhost of the machine?

Docker SQLSTATE[08006] for pgsql service only from php service

Not sure if my title is accurate, but here's my issue. I am running a basic laravel site on Docker and cannot get the site itself to connect to the PostgreSQL service. I will post my docker-compose.yml below. When i run php artisan migrate i get no errors and it all works. I can even use my Postico PostgreSQL client to connect to the DB and run queries. But, when i try and connect to the DB from the site, it errors out saying this:
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 5433?
Here are my PostgreSQL client settings (which DO work):
Host: 127.0.0.1
Port: 5433
User: homestead
Password: homestead
Database: homestead
I have been messing around with different settings and things so here is my docker-compose.yml, although i'm sure there are things i don't need in there:
version: '2'
services:
php:
image: jguyomard/laravel-php:7.2
build:
context: .
dockerfile: infrastructure/php/Dockerfile
volumes:
- ./:/var/www/
- $HOME/.composer/:$HOME/.composer/
networks:
- default
links:
- postgresql
- redis
nginx:
image: jguyomard/laravel-nginx:1.13
build:
context: .
dockerfile: infrastructure/nginx/Dockerfile
ports:
- 81:80
networks:
- default
links:
- postgresql
- redis
postgresql:
image: postgres:9.6-alpine
volumes:
- pgsqldata:/var/lib/postgresql/data
environment:
- "POSTGRES_DB=homestead"
- "POSTGRES_USER=homestead"
- "POSTGRES_PASSWORD=homestead"
ports:
- "5433:5432"
networks:
- default
redis:
image: redis:4.0-alpine
command: redis-server --appendonly yes
ports:
- "6379:6379"
networks:
- default
# elastic:
# image: elasticsearch:5.5-alpine
# ports:
# - "9200:9200"
volumes:
pgsqldata:
networks:
default:
Any thoughts on why the site can't connect to the DB?
My docker network ls output:
NETWORK ID NAME DRIVER SCOPE
2bf85424f466 bridge bridge local
c29d413f768e host host local
0bdf9db30cd8 none null local
f3d9cb028ae3 my-app_default bridge local
The error message ask Is the server running on host "127.0.0.1" but in your case PostgreSQL is running on a different docker container which is not 127.0.0.1 reference to php app so, change the server host to postgresql inside your php application.
And for the modified error, it is because that you have used port 5433 inside the php application which is the port of host machine which is for use outside the docker container (for host machine, that's why your Postico PostgreSQL client worked). But the port you have to use inside the docker network is 5432 change the server port to 5432 inside your php application.
And you have made the compose file complex by defining network in each host as default network. (You can follow this link for more details) If you don't have a requirement for that you don't need to do that as docker-compose will deploy all containers in single network.
And you don't need to use links they are deprecated. When multiple containers are in one docker-compose.yml file they are automatically deployed in a same single network.
So this simplified compose file will be recommended.
version: '2'
services:
php:
image: jguyomard/laravel-php:7.2
build:
context: .
dockerfile: infrastructure/php/Dockerfile
volumes:
- ./:/var/www/
- $HOME/.composer/:$HOME/.composer/
nginx:
image: jguyomard/laravel-nginx:1.13
build:
context: .
dockerfile: infrastructure/nginx/Dockerfile
ports:
- 81:80
postgresql:
image: postgres:9.6-alpine
volumes:
- pgsqldata:/var/lib/postgresql/data
environment:
- "POSTGRES_DB=homestead"
- "POSTGRES_USER=homestead"
- "POSTGRES_PASSWORD=homestead"
ports:
- "5433:5432"
redis:
image: redis:4.0-alpine
command: redis-server --appendonly yes
ports:
- "6379:6379"
# elastic:
# image: elasticsearch:5.5-alpine
# ports:
# - "9200:9200"
volumes:
pgsqldata:

Categories