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.
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
It seems that when i run Docker compose up, docker is not reading from docker-compose.yml.
It seems like it is loading images from cache or i don't think where is finding them.
Bellow is my docker-compose.yml
version: '3'
services:
httpd:
image: httpd:latest
user: root
ports:
- "80:80" # Default Apache port (Default on PHP 7.4)
- "8073:8073" # PHP 7.3 Apache port
- "8074:8074" # PHP 7.4 Apache port
- "8081:8081" # PHP 8.1 Apache port
volumes:
- ./:/var/www/html/myApp/:rw
- ./dev/Docker/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
restart: on-failure
container_name: httpd
networks:
- mb-frontend
php8.1-fpm:
build: ./dev/Docker/php-fpm/8.1
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php8.1-fpm
networks:
- mb-frontend
- mb-backend
php7.4-fpm:
build: ./dev/Docker/php-fpm/7.4
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.4-fpm
networks:
- mb-frontend
- mb-backend
php7.3-fpm:
build: ./dev/Docker/php-fpm/7.3
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.3-fpm
networks:
- mb-frontend
- mb-backend
db:
image: mariadb:10.3.5
environment:
MYSQL_ROOT_PASSWORD: myPassword
MYSQL_USER: dev
MYSQL_PASSWORD: myPassword
ports:
- "3306:3306"
volumes:
- /root/Bureau/mysql:/var/lib/mysql/:rw
- ./dev/Docker/mariadb/conf.d/:/etc/mysql/conf.d/:rw
- ./dev/Docker/mariadb/config/init.sql:/docker-entrypoint-initdb.d/init.sql
restart: on-failure
container_name: db
networks:
- mb-backend
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
volumes:
- /root/Bureau/phpmyadmin:/var/lib/mysql/
networks:
- mb-backend
depends_on:
- db
redis:
image: redis:6.2
container_name: redis
ports:
- "6379:6379"
networks:
- mb-backend
networks:
mb-frontend:
driver: bridge
mb-backend:
driver: bridge
I commented some images on docker-compose.yml but when i tape the command Docker compose up on terminal, all images even commented images are Up.
Can anyone help me how i force docker to read images from the edited docker-compose.yml
Good practice would be do use:
docker compose down
and then
docker compose up
UPDATE:
Next I would suggest to clean up your containers:
List all containers:
docker ps -a
Remove those you don't want because they might still be in the system
docker rm <CONTAINER ID/NAME>
How to Stop & Remove a running container by ID or Name?
The command is actually docker compose up.
The command docker-compose has been deprecated as of latest version. We can now use docker compose without the hyphen(-).
You can use docker-compose -f <path-to-compose-file> to pass in the compose file.
Example:
docker compose -f docker-compose.yml up
Reference documentation: https://docs.docker.com/compose/reference/
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 have laravel project and i want to create docker for it
and i want to automation it
i want to run this commends:
{
php artisan key:generate
php artisan migrate
php artisan queue:work --daemon
}
and its my docker-compose.yml file
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: xxxx/lumen:Library
container_name: Library
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: LibraryWebserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: Librarydb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: library
MYSQL_ROOT_PASSWORD: Library!23
MYSQL_USER: root
MYSQL_PASSWORD: Library!23
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
i try to use command: php artisan migrate and its do nothing
and also use
command: bash -c "php artisan migrate"
command: 'php artisan migrate'
You can use a command like the below example. It will run the command once the container is up and running.
version: "3.0"
services:
app:
image: busybox
command: top
networks:
app_net:
link_local_ips:
- 57.123.22.11