Can't workout why TablePlus refuses to connect to db with Sail 0n 3307 - laravel

I am running using Sail for my side project, and use Homestead for work.
I get conflicts with port 3306, so I moved Sail to 3307, but now for some reason I can't get TablePlus to connect to the DB.
Just to be clear, the site loads in the browser and sail tinker, sail artisan:migrate both work fine, it's JUST TablePlus that isn't working?!
It worked fine last weekend when I was using it, but after getting the issue with something using 3306 I decided to change the port to 3307
I've run docker-compose down -v to delete the volume including the db, and brought it back up, and the site works after a migration, but still nothing with TP.
The following is my .env:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=tall_events
DB_USERNAME=sail
DB_PASSWORD=password
FORWARD_DB_PORT=3307
And my docker-compose.yml file:
mysql:
image: "mysql:8.0"
ports:
- "${FORWARD_DB_PORT:-3307}:3307"
environment:
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
MYSQL_DATABASE: "${DB_DATABASE}"
MYSQL_USER: "${DB_USERNAME}"
MYSQL_PASSWORD: "${DB_PASSWORD}"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
volumes:
- "sailmysql:/var/lib/mysql"
It's showing as port 3307 on Docker Desktop:
But when I try to connect with TablePlus on 3307 I get the following:
And when I try 3306 I get

When you run sail up -d docker-compose creates a virtual network. With your existing config, mysql is still listening on port 3306 on that virtual network, but in the docker-compose.yml you're mapping port 3307 on you machine to 3307 on the mysql container.
What you need to do is map port 3307 on your machine to port 3306 on the mysql container. To do that, you can update the ports section of your docker-compose.yml like so:
mysql:
ports:
- "3307:3306"
This means you can connect to localhost:3307 with TablePlus, but your Laravel app, which is running in a docker container connected to the virtual network, can still connect to mysql:3306 on that virtual network.

Check your host in TablePlus.
In your .env file, the DB_HOST is "mysql".
The host in TablePlus looks like it's set to "127.0.0.1"
Also, check the database name. It looks like it's set to "da"-something, when it should be "tall_events"

Related

How to connect to my local machine domain from docker container?

I have local server with domain mydomain.com it is just alias to localhost:80
And I want to allow make requests to mydomain.com from my running docker-container.
When I'm trying to request to it I see
cURL error 7: Failed to connect to mydomain.com port 80: Connection refused
My docker-compose.yml
version: '3.8'
services:
nginx:
container_name: project-nginx
image: nginx:1.23.1-alpine
volumes:
- ./docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./src:/app
ports:
- ${NGINX_PORT:-81}:80
depends_on:
- project
server:
container_name: project
build:
context: ./
environment:
NODE_MODE: service
APP_ENV: local
APP_DEBUG: 1
ALLOWED_ORIGINS: ${ALLOWED_ORIGINS:-null}
volumes:
- ./src:/app
I'm using docker desktop for Windows
What can I do?
I've tried to add
network_mode: "host"
but it ruins my docker-compose startup
When I'm trying to send request to host.docker.internal I see this:
The requested URL was not found on this server. If you entered
the URL manually please check your spelling and try again.
The host network is not supported on Windows. If you are using Linux containers on Windows, make sure you have switched to Linux containers on Docker Desktop. That uses WSL2, so you should be able to use that in there.

How to correctly config docker-compose to connect to localhost (non-docker service) [duplicate]

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 1 year ago.
I'm new to Docker. I'm build a Spring Boot Application deploying on Docker.
Here is my example docker-compose.yml:
version: "3"
services:
user:
container_name: user
image: user-service
build:
context: user-api/
ports:
- "3001:8000"
restart: always
volumes:
- /home/ubuntu/logs/user_service:/opt/app/logs
networks:
- api_network
cms:
container_name: cms
image:cms-service
build:
context: cms-service/
ports:
- "3003:8000"
restart: always
volumes:
- /home/ubuntu/logs/cms_service:/opt/app/logs
networks:
- api_network
networks:
api_network:
driver: bridge
In the server machine, there's a Redis Server running on Ubuntu. I cannot connect the the Redis Server from Docker container to the host machine.
Here is my redis config inside application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=Password123!##
I also tried to change the localhost to
127.0.0.1
172.17.0.1
0.0.0.0
host
host.docker.internal
That's I've found on the internet. But nothing works. Do I need to specifically config anything to allow my Spring Boot Service inside Docker connect to Redis that's running on localhost.
The issue is probably due to the fact your Redis is bound to the address 127.0.0.1 (which is the default configuration) and your containers are not running on the network host.
To solve this, you should reconfigure Redis to bind to both 127.0.0.1 as well as to the IP address of the host as seen from api_network (sudo ip addr show on the host): the easiest thing to do here, if your scenario allows that, is to just bind Redis to 0.0.0.0 (via redis.conf).
As an alternative, you may also want to run your containers on the host network instead of using the api_network bridge: this appears to be overkill according to your issue, by the way, and may lead to security issues and exposed network ports.

Laravel Sail SQL Connection timed out

I just started using Laravel Sail, but Laravel can't connect to the MySQL server. Even Artisan in the container gives Connection timed out. I can connect from MySQL Workbench on my host machine.
My .env file has been updated for use with sail:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Laravel sail does not come with phpMyAdmin to visualize your database. so, you need to go with one of these:
add phpMyAdmin to your docker-compose.yml file manually.
services:
...
phpmyadmin:
image: phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- 8080:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: "${DB_USERNAME}"
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
...
volumes:
...
phpmyadmin:
driver: local
restart your project again.
sail up -d
sail now will download phpMyAdmin image to your docker container where you
will able to view database on port:8080 [http://localhost:8080/].
use a graphical database management application like TablePlus as mentioned in sail documentation.
I recently ran across this issue too, when upgrading an old Laravel project from using homestead to sail. As per the docs you need to set DB_HOST=mysql in your .env file so that your application container can communicate with the MySQL container.
You must write in "DB_HOST" are domain or ip
DB_HOST=1.2.3.4
or
DB_HOST=example.com

traefik proxy for docker container running in host network

i am running traefik as a proxy in docker container
i am using DockerToolBox in windows 10
the traefik proxy was able to recognize the service app which is running in 127.0.0.1, but the service app is actually running in docker host = 192.168.99.x ip
version: '3'
services:
reverse_proxy:
image: traefik
command: --api --docker
ports:
- "81:80"
- "8081:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- backend
whoami:
image: containous/whoami
labels:
- "traefik.frontend.rule=Host:whoami.default"
- "traefik.enable=true"
- "traefik.port=80"
network_mode: host
networks:
backend:
driver: bridge
in the Traefik dashboard http://192.168.99.100:8081
it shows http://127.0.0.1:80 for whoami service
instead of http://192.168.99.100:80
any help would be appreciated.
i want network_mode: host to pick 192.168.99.100 instead of 127.0.0.1
As traefik official documentation says, when resolving service IP, first it
try a lookup of host.docker.internal
and second
if the lookup was unsuccessful, fall back to 127.0.0.1
This means we can just add a host in the traefik container, using --add-host {docker0_IP}(it's the bridge's IP, you can easily use docker inspect {NAME_OF_TRAEFIK} and find the IP of Gateway(for me, it's 172.18.0.1). If you use docker-compose, you can use add following lines to your definition of traefik:
extra_hosts:
- host.docker.internal:{docker0_IP}
Also, I find that it's ok to use the IP my eth0 IP, which means the IP of your LAN(for me, it's 192.168.0.20).
Then, recreate traefik and everything works like a daisy.

Can't reach server inside docker container from host

I am hosting a mysql server and a go http server in docker. I am unable to hit the http server from my host machine. My host machine is a mac.
I have tried using localhost:8080 and ipofserver:8080. I get the ip from the docker inspect. I am able to connect to my mysql server from my host, but i can't hit the server from the host.
Here is my docker ps output.
0.0.0.0:8080->8080/tcp
0.0.0.0:3306->3306/tcp, 33060/tcp
Below are my details:
Docker Desktop version 2.0.0.3.
docker-compose
version: '3.1'
services:
mysql:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydb
volumes:
- mysql:/var/lib/mysql
ports:
- "3306:3306"
networks:
- mynetwork
server:
image: server:latest
networks:
- mynetwork
ports:
- "8080:8080"
volumes:
mysql: ~
networks:
mynetwork:
driver: "bridge"
mysql dockerfile
FROM mysql:8.0.16
COPY ./scripts/mysql/dbgen-v1.sql /docker-entrypoint-initdb.d/
EXPOSE 3306
server dockerfile
FROM golang:1.12.5
WORKDIR a/go/path
COPY . .
ENV GOBIN=/usr/local/bin
RUN go get github.com/go-sql-driver/mysql
RUN go get github.com/iancoleman/strcase
RUN go get github.com/jmoiron/sqlx
RUN go get github.com/spf13/cobra
RUN go get github.com/gorilla/websocket
RUN go get github.com/spf13/viper
RUN go install -v cmd/project/main.go
EXPOSE 8080
CMD ["main"]
(This answer is based on the chat we had in the comments)
In order to expose the web server from inside the container to the host it needs to bind to 0.0.0.0 and not to 127.0.0.1. Using 0.0.0.0 ensures that the web server binds to the bridge interface that can be accessed from the host side.
Relevant Docker docs: https://docs.docker.com/v17.09/engine/userguide/networking/default_network/binding/

Resources