I started to develop with Laravel and since version 8, the Laravel development environment is shipped with docker and a tool named Sail (docker-compose).
When I configure my .env file I need to SSH the MySQL container and get its hostname, and then SSH the Mailhog container and do the same.
Besides the above when I want to see the database from a tool like TablePlus, I couldn’t make it access the MySQL container by hostname.
I did some searches and found that I can add the hostname directive, but I'm not sure if it's not disturbing the internal container's communication.
Can I use the hostname directive safety in my case? is there a way to join the containers under one hostname and make it accessible from my host?
I'm running Docker Desktop for a MAC - v20.10.2
The docker-compose for reference
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
# - pgsql
- redis
# - selenium
# selenium:
# image: 'selenium/standalone-chrome'
# volumes:
# - '/dev/shm:/dev/shm'
# networks:
# - sail
mysql:
hostname: 'beatsy-db'
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
# pgsql:
# image: postgres:13
# ports:
# - '${FORWARD_DB_PORT:-5432}:5432'
# environment:
# PGPASSWORD: '${DB_PASSWORD:-secret}'
# POSTGRES_DB: '${DB_DATABASE}'
# POSTGRES_USER: '${DB_USERNAME}'
# POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
# volumes:
# - 'sailpostgresql:/var/lib/postgresql/data'
# networks:
# - sail
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
# memcached:
# image: 'memcached:alpine'
# ports:
# - '11211:11211'
# networks:
# - sail
mailhog:
hostname: 'beatsy-mail'
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
# sailpostgresql:
# driver: local
sailredis:
driver: local
You have several services in your docker-compose.yml:
laravel.test
mysql
redis
and so on. Each of these is a resolavble name for containers in the same network. So instead of using mysql container hostname use its service name (just mysql in your case). You shouldn't use container hostnames because they're randomly generated.
Related
I have a docker instance working with a myadmin to install phpmyadmin so I can develop a Laravel application. It's all working great, but sometimes I need to drop the database through the phpmyadmin and import a new copy from my production server. I'm not able to. I found this php setting: AllowUserDropDatabase, which, when set to true, should allow phpMyAdmin to drop a database. I tried adding this to the myadmin section of my docker-composer.yml in multiple ways:
AllowUserDropDatabase: true
ALLOW_USER_DROP_DATABASE: true
with no luck. I have used UPLOAD_LIMIT: 10000000 to change that settings, so I thought maybe that would work. Can't seem to find much info on the myadmin element of the docker composer.
My docker-composer.yml file:
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
myadmin:
image: 'phpmyadmin:latest'
ports:
- 8080:80
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
UPLOAD_LIMIT: 100000000
links:
- "mysql:db"
depends_on:
- mysql
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
I tried to set up a docker container for this project but every time I run ./vendor/bin/sail up -d, the laravel.test container exits with "127".
The rest of the containers work just fine.
At first, I thought this might've been WSL having an issue on Windows for me so I created an Ubuntu VM through VirtualBox and did the same process but the result was the exact same:
And because of this, running localhost:4000 or 192.168.99.100:4000 on a web browser displays nothing....
Other info in case it might be linked somehow:
I previously tried to run ./vendor/bin/sail up -d but I ran into this issue. And the only way I was able to bypass it and get to where I am now is changing my .env file's EOL from CRLF to LF through VS Code.
When I open Docker's Windows app's laravel.test container, it displays this.
I am pretty stumped on how to get it to work. Any help would be appreciated!
Apologies if I've made any mistakes as this is my first time with Laravel-Sail and Docker.
HERE IS MY docker-compose FILE:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${APP_PORT_OCTANE:-8080}:8080'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
- meilisearch
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:${MEILISEARCH_BUILD:-latest}'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- 'sail-meilisearch:/data.ms'
networks:
- sail
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:7700/health"]
retries: 3
timeout: 5s
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
myadmin:
image: 'phpmyadmin:latest'
ports:
- '${FORWARD_PHP_MYADMIN_PORT:-8080}:80'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
UPLOAD_LIMIT: 300M
links:
- "mysql:db"
depends_on:
- mysql
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local
sail-meilisearch:
driver: local
This configuration should solve the problem:
I have a project that I am trying to develop with a microservices approach. I prepared the APIs on two separate microservices and completed their tests. When I send a request from container A to container B, I get the error cURL error 7: Could not connect to localhost port 8100: Connection refused. After my research, I couldn't find a solution.
Request Architecture:
I'm trying to make a request from 127.0.0.1:8000 to an api address running at 127.0.0.1:8100.
What should I do to fix this problem? My question may be wrong, so I apologize in advance.
thanks
My Compose File First Container
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
container_name: MP-Main-Service-Laravel
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- 8000:8000
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 2
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
mysql:
image: 'mysql:8.0'
container_name: MP-Main-Service-MySql
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_USER: '${DB_USERNAME}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- "/var/lib/mysql"
networks:
- sail
phpmyadmin:
image: 'phpmyadmin:latest'
container_name: MP-Main-Service-PhpMyAdmin
ports:
- 8081:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_USER: '${DB_USERNAME}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
depends_on:
- mysql
networks:
- sail
redis:
image: 'redis:alpine'
container_name: MP-Main-Service-Redis
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
My Compose File Second Container
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
container_name: MP-Communication-Service-Laravel
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- 8100:8000
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
mysql:
image: 'mysql:8.0'
container_name: MP-Communication-Service-MySql
ports:
- '${FORWARD_DB_PORT:-33061}:33061'
environment:
MYSQL_ROOT_USER: '${DB_USERNAME}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- "/var/lib/mysql"
networks:
- sail
phpmyadmin:
image: 'phpmyadmin:latest'
container_name: MP-Communication-Service-PhpMyAdmin
ports:
- 8082:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_USER: '${DB_USERNAME}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
depends_on:
- mysql
networks:
- sail
redis:
image: 'redis:alpine'
container_name: MP-Communication-Service-Redis
ports:
- '${FORWARD_REDIS_PORT:-63791}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
Notice: this solution is raw and for development time:
First get container gateway ip:
❯ docker inspect {container id} | grep -i Gateway
Second Suppose it gives you 172.25.0.1
Your url pattern will look like this:
http://{containerIp}:{port}/...
or:
http://172.25.0.1:8585/...
The port is the same as the one you put in your docker-compose.yaml file.
I have setup Laravel 8 on my Mac OS. I am using Laravel Sail to setup Docker container.
My application works proper on http://localhost:8081. However, I want to make
admin.website-dev.com to work.
Here is the docker-compose.yml
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
container_name: 'bv_laravel_http'
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '8081:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
- meilisearch
- selenium
mysql:
image: 'mysql:8.0'
container_name: 'bv_laravelreact_mysql'
ports:
- '${FORWARD_DB_PORT:-3360}:3360'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6378}:6378'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7701}:7701'
volumes:
- 'sailmeilisearch:/data.ms'
networks:
- sail
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:7700/health"]
retries: 3
timeout: 5s
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1024}:1024'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8026}:8026'
networks:
- sail
selenium:
image: 'selenium/standalone-chrome'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
sailmeilisearch:
driver: local
Here is the /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 laravel.test
127.0.0.1 admin.website-dev.com
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
Any help is highly appreciated.
From the comments I see that the issue was confirmed to be related to not including the port when pointing the browser to the application in the docker by locally configured domain name.
Using
admin.website-dev.com:8081
instead of
admin.website-dev.com
seems to have fixed the issue.
My application was running smoothly until i installed laravel octane following the documentation and then localhost has refused to work. I have my installation running on the docker installation for windows.
Below is my docker-compose.yml file
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
- selenium
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- 'sailmeilisearch:/data.ms'
networks:
- sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
selenium:
image: 'selenium/standalone-chrome'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
sailmeilisearch:
driver: local
my /docker/8.0/supervisord.config file where i edited the command according to the octane doc to serve my application using swoole server instead of php development server.
[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
[program:php]
command=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan octane:start --server=swoole --host=0.0.0.0 --port=8000
user=sail
environment=LARAVEL_SAIL="1"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
I don't know what else to do, i have tried rebuilding the sail image multiple times but still it wont work on http://localhost:8000 or http://0.0.0.0:/8000
Yesterday, I was worried about it, but I solved it.
It was a simple thing!
ports:
- '${APP_PORT:-80}:80'
↓
ports:
- '${APP_PORT:-80}:8000'
Let's access to http://localhost!
I'm sure I can do it.
If your goal is to connect to the docker container on port 8000 and 80 I think this would be the correct configuration:
ports:
- '${APP_PORT:-80}:80'
- '8000:80'
This will open 80 and 8000 to the wild.
This is useful if you want to run laravel sail and make it available as an API for something like React/NestJS.