How to make API calls within docker environment? - php

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

Related

What is wrong with my Laravel docker compose File?

Can anybody help me with the following? I have a docker-compose file with the following services PHP, Mysql, Redis, and Nginx. The docker-compose file builds without any issues but when I try the URL its not working. The file is down below
version: '3.8'
services:
php:
build: ./docker-compose/php-81/build
image: php-81
container_name: cumax-php-db
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
- ./docker-files/storage/logs/php:/var/www/default/logs
networks:
- onboarding
db:
image: mysql:8.0
container_name: cumax-mysql-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql8.0/storage:/var/lib/mysql
networks:
- onboarding
nginx:
image: nginx:1.17-alpine
container_name: cumax-nginx
restart: unless-stopped
ports:
- "8070:80"
volumes:
- ./:/var/www
- ./docker-compose/nginx/conf.d:/etc/nginx/conf.d
networks:
- onboarding
redis:
image: "redis:alpine"
ports:
- "6379:6379"
networks:
- onboarding
networks:
onboarding:
On php you don't publish the port.
On the Nginx you only publish to port 8070 on the host.
What is the URL you are using? It should be http://< host >:8070/
And then the nginx should proxy onwards to php.

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

Docker on Windows: how to run multiple websites (LAMP stack)?

I am trying to set up a local environment for web development (LAMP stack) using Docker.
All websites live in folder called Sites with this structure:
/Sites
-- site1.local
-- www
-- site2.local
-- www
For every website I need its own version of PHP and MySQL.
So far I was able to run one local website with this docker-compose.yml (uses php:7.1-apache):
version: "3"
services:
webserver:
build:
context: ./bin/webserver
container_name: 'sp-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
mysql:
build: ./bin/mysql
container_name: 'sp-mysql'
restart: 'always'
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: sp-demo
MYSQL_USER: sp-demo
MYSQL_PASSWORD: sp-demo
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'rb-phpmyadmin'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- '8080:80'
volumes:
- /sessions
redis:
container_name: 'rb-redis'
image: redis:latest
ports:
- "6379:6379"
The website is available at http://localhost:80
Questions:
1) How do I make it accesible by domain domain, for example, http://site1.local instead of http://localhost:80? I heard I need some Nginx Proxy for this (https://github.com/jwilder/nginx-proxy), but I can't understand how to set it up with Apache in my case.
2) How to set up the second website (http://site2.local) the same way to run it simultaneously? As far as I understand, I would need to change all ports (80, 443 and 3306), otherwise I will have a conflict when I run docker-compose up -d? Is it possible without changing ports?
Thanks for the answers!

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

Problems PHP MARIADB PHPMYADMIN using docker-compose

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.

Categories