Cannot access laravel debugging - laravel

I'm having a hard time connecting to the development webserver inside the container (from safari on my host machine).
Since I am using network_mode: host I did not expect there to be any errors.
In my .env APP_URL is set to localhost.com - I added an entry to /etc/hosts accordingly.
Ping localhost.com referes to 127.0.0.1 so that should not be the issue.
version: '3'
services:
setup:
image: mariadb:latest
command:
- mysql -u root -proot -e "CREATE DATABASE central;"
web:
image: waryor/laravel-8.0:latest
volumes:
- '..:/var/www/html'
depends_on:
- mariadb
- redis
stdin_open: true
tty: true
network_mode: host
mariadb:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_DATABASE: 'central'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'root'
network_mode: host
restart: always
redis:
image: arm64v8/redis:alpine
restart: always
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
network_mode: host
Starting the webserver works without any errors
root#docker-desktop:/var/www/html# php artisan serve
Starting Laravel development server: http://127.0.0.1:8000
[Wed Oct 6 00:20:54 2021] PHP 8.0.11 Development Server (http://127.0.0.1:8000) started
I've even tried using the --host 0.0.0.0 option without any success.

You need to expose the ports. You don't have to use network_mode. Change your file to:
web:
image: waryor/laravel-8.0:latest
volumes:
- '..:/var/www/html'
ports:
- 80:8000
depends_on:
- mariadb
- redis
stdin_open: true
tty: true
network_mode: host
By doing - 80:8000, you are exposing internal 8000 to external 80, so you can go to localhost and done.

Related

Docker redirect automatically from port 8000 to 8080 - Docker Bug

I've a problem with my Docker.
I use Docker Desktop version 20.10.2, build 2291f61 and docker-compose version 1.27.4, build 40524192 on Windows 10 Pro.
Since a few hours, when I launch any docker-compose and I'm going on my browser to contact localhost:8000, I'm automatically redirect to localhost:8080.
I've delete all my Docker data (images, containers, networks...), I've also reset Docker to factory defaults but that didn't solve anything... I don't know what is going on !
Here, an example of one of my docker-compose.yml :
version: "3.7"
services:
wordpress:
image: wordpress:php7.4-fpm
container_name: e2i-scollado-cours-wordpress
restart: unless-stopped
ports:
- 8000:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
volumes:
- ./storage/wordpress:/var/www/html
networks:
- e2i-scollado-cours
db:
image: mariadb:10.5
container_name: e2i-scollado-cours-database
restart: unless-stopped
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: password
volumes:
- ./storage/database:/var/lib/mysql
networks:
- e2i-scollado-cours
phpmyadmin:
image: phpmyadmin
container_name: e2i-scollado-cours-phpmyadmin
restart: unless-stopped
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
- PMA_HOSTS=database
- PMA_USER=wordpress
- PMA_PASSWORD=password
networks:
- e2i-scollado-cours
networks:
e2i-scollado-cours:
driver:
bridge
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e6c54623675 wordpress:php7.4-fpm "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 9000/tcp, 0.0.0.0:8000->80/tcp e2i-scollado-cours-wordpress
bbfb12fa4c14 mariadb:10.5 "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 0.0.0.0:3306->3306/tcp e2i-scollado-cours-database
243fc759179c phpmyadmin "/docker-entrypoint.…" 17 minutes ago Up 17 minutes 0.0.0.0:8080->80/tcp e2i-scollado-cours-phpmyadmin
If I want to see Wordpress on my browser, I'm automatically redirect to PHPMyAdmin in this case.
But, even if I don't had a service on the port 8080, I'm still redirect to the port 8080.
Also, I've this problem with all of my docker-compose, here, it's just an example...
Please, if anyone have an answer, help me please ^^
Thank's

Access to local database denied through docker container

I am having a problem connecting my compiled Spring-Boot app to the database that I have running on another container on my server.
I have tried different configurations, changing from localhost to the IP address of my server for the connection. I also double checked that the credentials matched by logging in via Adminer. Finally, I did a rebuild of the compose and image files several times to ensure that I have all the latest versions.
Compose file:
version: '3.1'
services:
db:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: mypassword
MYSQL_DATABASE: marketingappdb
ports:
- "3306:3306"
expose:
- 3306
volumes:
- ./mariadbvolume:/var/lib/mariadb
networks:
- marketingapp
adminer:
image: adminer
restart: always
ports:
- "8086:8080"
expose:
- 8086
depends_on:
- db
networks:
- marketingapp
springserver:
image: marketingapp
restart: always
ports:
- "8091:8091"
expose:
- 8091
depends_on:
- db
networks:
- marketingapp
networks:
marketingapp:
Spring Server Image:
FROM openjdk:latest
COPY /marketing-app-final.jar .
EXPOSE 8091
ENTRYPOINT ["java", "-jar", "marketing-app-final.jar"]
Application properties for Spring:
server.port = 8091
spring.datasource.url=jdbc:mariadb://0.0.0.0:3306/marketingappdb
spring.datasource.username=root
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
I can connect from my PC to the database using the app from the remote same configuration (obviously replacing localhost with the IP) and don't see why I shouldn't be able to do the same from the actual server. Thanks in advance for any help!
Use the docker dns to connect your spring App to the mariabd:
jdbc:mariadb://db:3306/marketingappdb
Just a few other hints: you don't need to expose port 3306, you already bind it to 3306 on Host (if you just want to use it from within the docker Services you don't need to bind/expose it at all). And the mariabd persistent storage is var/lib/mysql and not var/lib/mariadb

Connection refused when running docker-compose exec app php artisan migrate

I'm getting PDOException::("SQLSTATE[HY000] [2002] Connection refused") when running docker-compose exec app php artisan migrate on the terminal
here's my db config on .env file
DB_CONNECTION=mysql
DB_HOST=172.20.0.4
DB_PORT=3306
DB_DATABASE=queue_db
DB_USERNAME=root
DB_PASSWORD=root
I also inspect the container ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' worker-db
Output: 172.20.0.4
here's my docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/Dockerfile
image: digitalocean.com/php
container_name: worker-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: worker-webserver
restart: unless-stopped
tty: true
ports:
- "8090:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql
container_name: worker-db
# restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: queue_db
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
There's a weird error on the stack trace where ip is different i'm not sure why it's getting a different ip, but in my .env file i'm using the container ip which is "172.20.0.4"
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=172.20.0.3;port=3306;dbname=queue_db", "root", "root", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Thanks in advance.
ok, after i tried to run php artisan config:clear it's working now.

Laravel on Docker: [2002] Connection refused

I am trying to put a Laravel app up on Docker, but the database container is giving me trouble.
Specifically, I am getting this error when I try to open the app in the browser:
SQLSTATE[HY000] [2002] Connection refused
But, as far as I can see, all the user credentials are correct. Perhaps I am missing something? Please see below.
docker-compose.yml:
version: '3'
services:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./yoga/:/var/www
environment:
- "DB_PORT=33061"
- "DB_HOST=database"
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
ports:
- 8080:80
database:
image: mysql:5.7
container_name: database
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=yogadb"
- "MYSQL_USER=yogi"
- "MYSQL_PASSWORD=mypasshere"
- "MYSQL_ROOT_PASSWORD="
ports:
- "33061:3306"
volumes:
dbdata:
.env:
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=yogadb
DB_USERNAME=yogi
DB_PASSWORD=mypasshere
When I run the app outside docker, everything works correctly, I just replace DB_HOST=database with DB_HOST=127.0.0.1
What can I do to fix this?
docker ps output is:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2da7283f7a65 docker_app "docker-php-entrypoi…" 19 minutes ago Up 7 seconds 9000/tcp docker_app_1
4801fe3312c1 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 7 seconds 33060/tcp, 0.0.0.0:33061->3306/tcp 4801fe3312c1_database
ab370ae1d155 docker_web "nginx -g 'daemon of…" 25 hours ago Up 7 seconds 443/tcp, 0.0.0.0:8080->80/tcp docker_web_1
As #prd mentioned, you need to create bridged network for the containers [1], then add containers to the network [2].
Hostname of container is determined by name of the service in docker-compose.yml. In your case, if app service will connect to database service at hostname database & port 3306.
So docker-compose.yml becomes:
version: '3'
services:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./yoga/:/var/www
environment:
- "DB_PORT=3306" # Port of database container is 3306
- "DB_HOST=database"
networks:
- name_of_network # [2] add container to network
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
ports:
- 8080:80
database: # Name of service, which determines hostname of container
image: mysql:5.7
container_name: database
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=yogadb"
- "MYSQL_USER=yogi"
- "MYSQL_PASSWORD=mypasshere"
- "MYSQL_ROOT_PASSWORD="
ports:
- "33061:3306"
networks:
- name_of_network # [2] add container to network
volumes:
dbdata:
networks:
name_of_network: # [1] create bridged network

Send mail from a container using a postfix container

I'm using an application hosted on a docker container.
This application executes bash scripts / instructions to send mails.
I made another container which executes Postfix as a SMTP Relay.
I want to send mails from my application container by using a bash script using my Postfix container as a relay.
I tried to connect with SSH from my application container to my Postfix container. But that doesn't seem to work.
How can i make it so a script executed in my application container can use my Postfix relay while not allowing anything outside of the docker network, or even better, to only allow some containers, to send mails from this relay.
EDIT 1 : Docker-compose files
Application docker compose :
version: "3.4"
volumes:
[...]
services:
application:
restart: always
build: ./application
depends_on:
- mariadb
container_name: application
volumes:
[...]
ports:
- "80:80"
- "443:443"
- "5669:5669"
deploy:
restart_policy:
window: 300s
links:
- mariadb
external_links:
- smtp-server
mariadb:
restart: always
image: mariadb
command: mysqld --sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
container_name: application-mariadb
volumes:
[...]
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
deploy:
restart_policy:
window: 300s
Here's my docker compose for my SMTP server :
version: "3.4"
services:
postfix:
restart: always
build: ./postfix
container_name: smtp-server
deploy:
restart_policy:
window: 300s
{a quick response, because I "cicle" in my work ... and I'm taking 10 minutes of clear my mint, I hope it serves you}
Are you using "docker-compose" ?, could you give an example of your YML file? (a little more context)
[you can not connect to by ssh to a container unless you have "supervisor" installed,which I do not recommend at all.]
from what I see, you only need to make private networks; You could use this:
https://docs.docker.com/compose/networking/
to hide everything, I also recommend using a load balancer / Inverse Proxy like TRAEFIK (if they have access to port 80 or 443 in some clear way this ...)
so you only expose 1/2 port(s) (80 + 443 for example) and everything else is protected by your reverse proxy
Watch as I separate the networks as you need the different containers.
bash have access to db and smtp
db does not have access smtp neither nginx
nginx have access to bash
nginx have access to proxy network to expose 80 and 443
no other container is exposed to the outside more than nginx
--
version: "3"
services:
bash:
####### use hostname "smtp" as SMTP server
image: bash
depends_on:
- db
networks:
- smtp_internal_network
- internal_network
- data_network
volumes:
- ../html:/var/www/html
restart: always
db:
image: percona:5.7
# ports: # for debug connections and querys
# - 3306:3306
volumes:
- ../db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
- data_network
restart: always
smtp:
image: mwader/postfix-relay
environment:
- POSTFIX_myhostname=smtp.domain.tld
networks:
- smtp_internal_network
restart: always
nginx:
image: nginx
volumes:
- ../html:/var/www/html
networks:
- external_network
- internal_network
labels:
- "traefik.backend=nginx_${COMPOSE_PROJECT_NAME}"
- "traefik.port=80"
- "traefik.frontend.rule=Host:${FRONTEND_RULE}"
- "traefik.frontend.passHostHeader=true"
- "traefik.enable=true"
- "traefik.docker.network=traefik_proxy"
restart: always
depends_on:
- db
- bash
networks:
external_network:
external:
name: traefik_proxy
internal_network:
driver: bridge
smtp_internal_network:
driver: bridge
data_network:
driver: bridge
Edit:
version: "3"
volumes:
[...]
services:
####### use hostname "smtp" as SMTP server in your application
application:
restart: always
build: ./application
depends_on:
- mariadb
volumes:
[...]
ports:
- "80:80"
- "443:443"
- "5669:5669"
deploy:
restart_policy:
window: 300s
networks:
- smtp_external_network
- data_network
mariadb:
restart: always
image: mariadb
command: mysqld --sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
networks:
- data_network
volumes:
[...]
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
deploy:
restart_policy:
window: 300s
networks:
smtp_external_network:
external:
name: [ReplaceForFolderParentNameOfSmtpYmlWithoutSquareBrackets]_smtp
data_network:
driver: bridge
--- (in your other file)
services:
smtp:
restart: always
build: ./postfix
networks:
- smtp
deploy:
restart_policy:
window: 300s
networks:
smpt:
driver: bridge

Resources