I am quite new to docker and have been battling two days to get my system going. My compose file creates nginx, php-fpm, mysql and phpmyadmin containers. I can ping all containers from each other. But when I try installing anything from my mounted public_html volume into the database (like installing an app that needs to create tables in database), the app simply cannot connect to the database. I have made sure over and over credentials are correct. For the life of me I don't know where I'm going wrong. Any help would be greatly appreciated. I include my compose file below:
version: '3'
services:
www:
container_name: site1-www
image: nginx:1.13.7-alpine
expose:
- "80"
ports:
- 8001:80
networks:
datacentre:
aliases:
- site1www
env_file:
- ".env"
environment:
- NGINX_HOST=localhost
volumes:
- /mnt/www/site1.test/public_html:/usr/share/nginx/html/
-
/mnt/www/site1.test/conf/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- db
links
- db
phpfpm:
build: ./phpfpm
container_name: site1-phpfpm
networks:
datacentre:
aliases:
- site1php
volumes:
- /mnt/www/site1.test/public_html:/usr/share/nginx/html
- /mnt/www/site1.test/conf/php.ini:/usr/local/etc/php/php.ini
depends_on:
- db
links
- db
db:
container_name: site1-db
image: mariadb:10.3.2
ports:
- 3400:3306
env_file:
- ".env"
environment:
- MYSQL_DATABASE=test1db
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=test1user
- MYSQL_PASSWORD=password
networks:
datacentre:
aliases:
- site1db
volumes:
- /mnt/dbdata:/var/lib/mysql
dbadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: site1-dbadmin
ports:
- 8000:80
networks:
datacentre:
aliases:
- site1dbadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOST=mysql
depends_on:
- db
links
- db
networks:
datacentre:
external:
name: datacentre_net
The compose setup looks a bit complex for a first shot. Remove all the names and aliases and use service names for connections. Then build from there.
For the www service, nginx should configure phpfpm:9000 as the FastCGI worker.
Your PHP application and phpMyAdmin should be configured to connect to db:3306
version: '3'
services:
www:
image: nginx:1.13.7-alpine
ports:
- 8001:80
env_file:
- ".env"
environment:
- NGINX_HOST=localhost
volumes:
- /mnt/www/site1.test/public_html:/usr/share/nginx/html/
- /mnt/www/site1.test/conf/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- phpfpm
phpfpm:
build: ./phpfpm
volumes:
- /mnt/www/site1.test/public_html:/usr/share/nginx/html
- /mnt/www/site1.test/conf/php.ini:/usr/local/etc/php/php.ini
depends_on:
- db
db:
image: mariadb:10.3.2
ports:
- 3400:3306
env_file:
- ".env"
environment:
- MYSQL_DATABASE=test1db
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=test1user
- MYSQL_PASSWORD=password
volumes:
- /mnt/dbdata:/var/lib/mysql
dbadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 8000:80
environment:
- PMA_ARBITRARY=1
- PMA_HOST=db
depends_on:
- db
links are not required to access services on a user defined network.
Related
This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 5 months ago.
How can a Docker container communicate with a local application that is not in Docker?
I have a Symfony container (PHP) and I want to communicate with a nodejs application for socket (so I need input and output for the 1337 port).
How can I make a communication between these applications?
My docker-file:
version: "3.7"
volumes:
db-data:
networks:
dev:
driver: bridge
services:
mariadb:
container_name: symfony_mariadb
image: mariadb:10.9.3-jammy
restart: always
environment:
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_DATABASE: symfony
MYSQL_ROOT_PASSWORD: root
volumes:
- db-data:/var/lib/mysql
expose:
- 3306
ports:
- "3306:3306"
phpmyadmin:
container_name: phpmyadmin
depends_on:
- mariadb
restart: always
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: mariadb
PMA_USER: root
PMA_PASSWORD: root
ports:
- "${PHPMYADMIN_PORT:-8081}:80"
redis:
container_name: redis
image: redis:7.0.5-alpine3.16
ports:
- "6379:6379"
volumes:
- ./data/redis:/data/redis
nginx:
build:
context: .docker/nginx
restart: on-failure
volumes:
- ./:/var/www/server:cached
- ./.docker/nginx/server.conf:/etc/nginx/conf.d/server.conf:cached
ports:
- "${NGINX_PORT:-8000}:80"
depends_on:
- php
- mariadb
- redis
php:
build:
context: .docker/php
restart: on-failure
ports:
- 5000:8000
volumes:
- ./:/var/www/server:cached
- ./:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
- ./.docker/php/php.ini:/usr/local/etc/php/php.ini:ro
depends_on:
- mariadb
- redis
user: "${ID_USER:-1001}:${ID_USER:-1001}"
Normally yo can see host machine port from docker, you try using host IP ?
Note: if this host IP is in a different network segment of internals IPs form dockers, like 192.168.1.28
In the image above the extra folders in the public folder only appears when ever i set up a dev environment with docker, the logs go to these folders instead of the /storage/logs/.
Another thing i experience when i use docker dev environment is i always have to reload cache (artisan optimize) before my config or route changes can be applied in the container.
THIS IS MY Docker-Compose FILE
version: "3"
networks:
laravel:
services:
nginx:
container_name: testproj-nginx
image: nginx:stable-alpine
ports:
- "2022:80"
volumes:
- ./:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8
container_name: testproj-mysql
restart: unless-stopped
tty: true
ports:
- "2306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: testproj
MYSQL_USER: root_user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build: ./
container_name: testproj-php
volumes:
- ./:/var/www/html
networks:
- laravel
mailhog:
image: "mailhog/mailhog:latest"
container_name: testproj-mailhog
ports:
- "${FORWARD_MAILHOG_PORT:-2025}:1025"
- "${FORWARD_MAILHOG_DASHBOARD_PORT:-9025}:8025"
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: testproj-pma
links:
- mysql
environment:
PMA_HOST: testproj-mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8083:80
networks:
- laravel
depends_on:
- php
- mysql
# ngrok:
# image: wernight/ngrok:latest
# container_name: testproj-ngrok
# ports:
# - 4041:4040
# environment:
# NGROK_AUTH: ${NGROK_AUTH_KEY}
# NGROK_REGION: eu
# NGROK_PROTOCOL: http
# NGROK_PORT: testproj-nginx:80
# networks:
# - laravel
# depends_on:
# - nginx
I am dockerizing an app that has several different services and I have used the Docker Compose file to achieve this Single docker-compose.yml file for all the services and one Dockerfile for each service.
Frontend(angular)[http://localhost:4200]
Backend(PHP)[http://localhost:80]
Backend Database(Mysql)
Integration Service(NodeJS)[http://localhost:4433]
Integration Service Database(MongoDB)
Authentication Layer(NodeJS)[http://localhost:4454]
Authentication Layer Database(Mysql)
So far I have successfully containerized all these services but when my "Backend(PHP)" container tries to call API from "Integration Service(NodeJS)" it throws the error which is cURL error 7: Failed to connect to localhost port 4433: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html).
services:
angular-service:
container_name: wms_frontend
build: ../frontend/.
ports:
- "4200:80"
php:
build:
context: .
image: wms-backend
networks:
- frontend
- backend
environment:
- MYSQL_HOST=wms-backend-mysql-app
- MYSQL_USER=wmsroot
- MYSQL_PASSWORD=pass
- MYSQL_DB=dbname
volumes:
- ./:/var/www/html/wms/backend
ports:
- "80:80"
container_name: wms-backend-php-app
mysql:
image: mysql:5.7
networks:
- backend
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_USER=wmsroot
- MYSQL_PASSWORD=pass
- MYSQL_DATABASE=
volumes:
- ./dump:/docker-entrypoint-initdb.d
container_name: wms-backend-mysql-app
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.7
depends_on:
- mysql
networks:
- backend
ports:
- "40002:80"
environment:
- PMA_HOST=wms-backend-mysql-app
- PMA_PORT= 3306
volumes:
- /sessions
container_name: wms-backend-phpmyadmin-app
app:
container_name: wms_auth
restart: always
build: ../../wms_auth/.
networks:
- backend
volumes:
- ../../auth/./:/usr/src/app
- /usr/src/app/node_modules
ports:
- "4454:4454"
links:
- db
depends_on:
db:
condition: service_healthy
db:
image: mariadb
restart: always
ports:
- "3308:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= YES
- MYSQL_DATABASE=auth
- MYSQL_USER= root
- MYSQL_PASSWORD=
volumes:
- ../../auth/dump/:/docker-entrypoint-initdb.d
networks:
- backend
healthcheck:
test: ["CMD", "mysql", "-h", "db","-u","root", "mysql", "-e", "select 1"]
interval: 1s
retries: 20
wms_integration:
container_name: integration
restart: always
build: ../../wmsIntegeration/.
volumes:
- ../../wmsIntegeration/./:/usr/src/app
- /usr/src/app/node_modules
ports:
- "4433:4433"
networks:
- backend
links:
- db
depends_on:
db:
condition: service_healthy
wms_integration_db:
image: mongo
restart: always
networks:
- backend
ports:
- "27019:27017"
volumes:
- ../../wmsIntegeration/mongodb:/data/db
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo db:27017/config --quiet
interval: 10s
timeout: 10s
retries: 5
networks:
local:
driver: bridge
networks:
frontend:
backend:
Bridge all the networks as follows :
networks:
local:
driver: bridge
frontend:
driver: bridge
backend:
driver: bridge
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
I am trying to run a Shopware Image locally. When I execute the init.sh file from the dnhsoft repo the command stucks at
Waiting for mysql server...
I already tried different images. It just wont work for me.
I used this docker-compose.yml:
version: "2"
services:
shop:
image: dnhsoft/shopware:5.6.9
links:
- mysql:db
- mailhog
ports:
- "8000:80"
volumes:
- "./../mtsTheme:/shopware/themes/Frontend/mtsTheme"
- "./../Local:/shopware/engine/Shopware/Plugins/Local"
mysql:
image: mysql:5.7
ports:
- "3306"
volumes:
## this makes the mysql shopware friendly, otherwise we have some utf8 issues
# - "./docker/mysql/mysqld_charset.cnf:/etc/mysql/conf.d/mysqld_charset.cnf"
- "./volumes/mysql:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: 123456
TZ: Europe/Berlin
pma:
image: phpmyadmin/phpmyadmin:4.8
links:
- mysql:db
ports:
- "8001:80"
environment:
PMA_USER: root
PMA_PASSWORD: 123456
TZ: Europe/Berlin
mailhog:
image: mailhog/mailhog
expose:
- "1025"
- "8025"
ports:
- "8025:8025"
environment:
TZ: Europe/Berlin