Problems PHP MARIADB PHPMYADMIN using docker-compose - php

I've been this last two weeks searching how to link my phpmyadmin and my database, to create a PHP development workspace, through docker-compose but I am not able to connect to the database through the phpmyadmin port.
version: '3'
services:
servicio_php:
image: php:7.3-rc-apache
volumes:
- ./web/:/var/www/html
expose:
- "80"
ports:
- "80:80"
links:
- db
db:
image: mariadb:latest
restart: always
volumes:
- ./Volumenes/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: user
MYSQL_PASSWORD: user123
MYSQL_DATABASE: bbdd1
expose:
- "3306"
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
environment:
PMA_ARBITRARY: 1
MYSQL_ROOT_PASSWORD: root123
MYSQL_USERNAME: user
PMA_HOST: db
MYSQL_PASSWORD: user123
restart: always
ports:
- "8080:80"
volumes:
- ./sessions:/sessions/
depends_on:
- db
links:
- db
I am using windows 10 version of dockers 18.06.1-ce and also when i run docker-compose up i see there are some probles with Innodb but ive read is not a problem.
I am not able to see whats wrong and probably I am not the only one with this problem.

Related

How to mirror production server using docker-compose for Wordpress

I have the following docker-compose file for running a Wordpress website.
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
- ./mysql-dump:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: admin123
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8081:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: admin123
networks:
- wpsite
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8001:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
networks:
- wpsite
volumes:
- "../../blogs:/var/www/html"
- "../../blogs/plugins:/var/www/html/wp-content/plugins"
networks:
wpsite:
volumes:
db_data: {}
Here I am using the official wordpress:latest image, but my production server has a different configuration. I would like to use the same PHP version and other configurations. How can I do it ?

Is it possible to use the same docker container for multiple websites

until a new version of php or mysql comes out I generally always use the same setup, so at the moment I create 3 containers for every site I create:-
Site 1:-
version: '3.1'
services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
image: site1
ports:
- 8000:80
restart: always
volumes:
- .:/var/www/html
networks:
- site1
mysql:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: site1
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
networks:
- larastock
phpmyadmin:
depends_on:
- site1
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- site1
networks:
site1:
volumes:
db_data:
Site 2:-
version: '3.1'
services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
image: site2
ports:
- 8000:80
restart: always
volumes:
- .:/var/www/html
networks:
- site2
mysql:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: site1
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
networks:
- larastock
phpmyadmin:
depends_on:
- site2
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- site2
networks:
site2:
volumes:
db_data:
They are identical except for the network but this means I have 6 containers when really I only need 3. Is it possible to tell site 2 to use the services that site 1 created or does this go against the whole point of docker?
So what I would like is to change:-
Site 1 uses php, mysql and phpmyadmin. Site 2 uses php1, mysql1, phpmyadmin1.
to:
both site 1 and site 2 using the same container's but having their own database:-
Site 1 uses php, mysql (database site1) and phpmyadmin. Site 2 uses php, mysql (database site2) and phpmyadmin

How to make API calls within docker environment?

I am building an app wrapped in docker, that consist of PHP backend ("API") and NODE frontend, they are united by NGINX, where php app is served by the means of php-fpm and my node app is served by the reverse proxy. NGINX exposes phpMyAdmin app (phpmyadmin.test) and "API" (api.php.test) for dev purposes and NODE api (nodeapp.test).
NODE apps SSR ("Server-Side Rendering") needs to fetch some data from an API within docker the network, and because domains such as api.php.test can't be recognized from within docker I have to make calls to NGINX container which serves 3 different domains mentioned above, so I either need to fake 'HOST' header to get appropriate response from an API via NGINX which leads problems. Such as: Refused to set unsafe header "Host", Error: unable to verify the first certificate in nodejs etc.
Do I have to spin up Nginx container for each endpoint to avoid these issues? Or is there a better way to go around this?
Here is an example of my docker-compose.yml to give you a better idea of what happens in my app.
version: "3.7"
services:
workspace:
build:
context: workspace
args:
WORKSPACE_USER: ${WORKSPACE_USER}
volumes:
- api:/var/www/api
- site:/var/www/site
ports:
- "2222:22"
environment:
S3_KEY: ${S3_KEY}
S3_SECRET: ${S3_SECRET}
S3_BUCKET: ${S3_BUCKET}
DB_CONNECTION: ${DB_CONNECTION}
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MEDIA_LIBRARY_ENDPOINT_TYPE: ${MEDIA_LIBRARY_ENDPOINT_TYPE}
MEDIA_LIBRARY_IMAGE_SERVICE: ${MEDIA_LIBRARY_IMAGE_SERVICE}
tty: true
php-fpm:
build:
context: ./php-fpm
depends_on:
- nodejs
volumes:
- api:/var/www/api
- ./certs:/certs
environment:
S3_KEY: ${S3_KEY}
S3_SECRET: ${S3_SECRET}
S3_BUCKET: ${S3_BUCKET}
DB_CONNECTION: ${DB_CONNECTION}
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MEDIA_LIBRARY_ENDPOINT_TYPE: ${MEDIA_LIBRARY_ENDPOINT_TYPE}
MEDIA_LIBRARY_IMAGE_SERVICE: ${MEDIA_LIBRARY_IMAGE_SERVICE}
nodejs:
build:
context: ./nodejs
args:
NODEJS_SITE_PATH: ${NODEJS_SITE_PATH}
NODEJS_VER: ${NODEJS_VER}
volumes:
- site:${NODEJS_SITE_PATH}
- ./certs:/certs
environment:
NODEJS_ENV: ${NODEJS_ENV}
ports:
- 3000:3000
- 3001:3001
nginx:
build:
context: nginx
depends_on:
- php-fpm
- mariadb
restart: always
volumes:
- api:/var/www/api
- site:/var/www/site
- ./nginx/global:/etc/nginx/global
- ./nginx/sites:/etc/nginx/sites-available
- ./nginx/logs:/var/log/nginx
- ./certs:/certs
ports:
- 80:80
- 443:443
mariadb:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- mariadb
restart: always
environment:
PMA_HOST: ${MYSQL_HOST}
PMA_USER: root
PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
UPLOAD_LIMIT: 2048M
volumes:
phpmyadmin:
db:
site:
external: true
api:
external: true

Three container docker-composition : PHP can't connect to Maria DB

My docker-compose.yml :
version: '3'
services:
php-apache:
build:
context: ./php-apache
ports:
- 80:80
volumes:
- ./DocumentRoot:/var/www/html
links:
- 'db'
networks:
- default
db:
image: mariadb:10.1
volumes:
- ./db:/var/lib/mysql
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
ports:
- "3306:3306"
environment:
TZ: "Europe/London"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- 'db'
ports:
- 8000:80
environment:
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_ROOT_PASSWORD: 'rootpwd'
PMA_HOST: db
PMA_PORT: 3306
This is based on a tutorial I found online.
When I run it, I can see that the PHP container is up. I can see that the db and php-mydmin are up. In fact I can successfully access the db from php-myadmin using the testuser / testpassword credentials.
But when I try to access the db from the main PHP application using
$conn = mysqli_connect("localhost","testuser",'testpassword','testdb');
I get
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/index.php on line 8
Is this a problem with the docker config? Can anyone see anything missing?

Unable to connect phpmyadmin to database using docker compose

May be this question asked few times before but I did't get a valid answer which can solve my problem.
I am trying to run phpmyadmin in docker on different container using docker-compose but It always through the following error:
#2002 - Connection refused — The server is not responding (or the local server's socket is not correctly configured).
My docker compose file contains the following code:
version: "2"
services:
web:
build: .
ports:
- "80:80"
networks:
- web
volumes:
- .:/code
restart: always
db:
image: "mysql:5"
volumes:
- ./mysql:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: toor
MYSQL_DATABASE: phpapp
networks:
- web
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8000:80"
restart: always
networks:
- web
networks:
web:
driver: bridge
In web container I am trying to connect with database and it works fine, but the problem occur with phpmyadmin connection
Any help would be appreciated. :)
Interestng enough, I have your compose-file running and phpmyadmin is accessible
from host.
Had to change port 8000 to 8004 though (port 8000 is occupied on my host).
In case your db-container does not start fast enough for phpmyadmin to connect, I suggest adding depends_on into phpmyadmin service. Makes sure db starts before phpmyadmin.
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8004:80"
restart: always
depends_on:
- db
networks:
- web
Please show logs from docker-compose up if problem persists.
Now you need to add command to mysql service for connecting to phpmyadmin.
command: --default-authentication-plugin=mysql_native_password
version: "2"
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: drupal
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- /var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
PMA_HOST: db
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test

Categories