Howto link a prestashop docker to an existing mysql server - mysql

I'm using the following Prestashop docker: https://hub.docker.com/r/prestashop/prestashop/
It so happens that I already have a mysql server running on the host, because that was included with the whole DirectAdmin panel.
The variable DB_SERVER does nothing if I specify localhost or the IP adres of my server. -p 3306:3306 is also not allowed, since that port is already in use.
How can a Docker container reach the host:3306 mysql server?

If I understand correctly, you have your Prestashop installation inside a container and MySQL server running on the Docker host?
Unless configured differently, both the container and the host are in the same bridge network.
Verify that your container is in the bridge network:
docker inspect <container-name>
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
Notice the gateway IP address - this is the address of the docker host inside the bridge network. Use it as a DB_SERVER variable to connect to the MySQL.

One year later...
Like what #NetworkMeister said, but, IPAddress instead.
docker inspect <container-name> | grep IPAddress
In other words, I used 172.17.0.2 and it worked.

I believe what you are looking to do is to connect your container process with mysql server that is running on that host. To retain using localhost as the assignment of DB_SERVER you can do the following...
docker run --net=host <YOUR_CONTAINER_OPTIONS>
By doing this your container will able to reach the mysql service that is deployed on localhost:3306.

Related

How to connect to MySQL database cluster

I tried to install a MySQL cluster with the Docker image below.
mysql/mysql-cluster - Docker Image | Docker Hub
The Docker image is pulled and run successfully.
Despite that I could connect to the cluster in the terminal (as shown in the screen capture below), I don't know how to connect to it with MySQL Workbench or DBeaver.
In your docker run command, you can use -p 3306:3306 (or any available port). Then you can use <host>:<port> from Workbench or Dbeaver connection URL.
I assume that you already know how to add new DB connection to MySQL Workbench or DBeaver. The information that you want is the connection URL and the username/password of an authenticated user that you need to use to connect to your MySQL cluster.
For the connection URL: 192.168.0.10 (no port in your example)
You need to have your MySQL Workbench or DBeaver connect to the URL of the MySQL node, which is mysql1 node in your example. As shown in your screen capture, it is 192.168.0.10 without any explicit port. But if you have troubles with the URL, you can run docker ps to check what host and port that your mysql1 is running and exposed at.
For the username/password: root/tpffnrtm1 (the password is the value of MYSQL_ROOT_PASSWORD as shown in your docker run of MySQL node command)
I assume that you just want to connect the DB cluster by any means (root or non-root privileges is totally fine for you).

How to access mysql databases inside docker from a SQL GUI?

I am using vessel to run my Laravel project.
As I understand it basically simply creates a put-together docker image of my laravel project with mysql and everything.
Now since its running in docker, is it possible for me to access the mySQL databases with datagrip or tableplus on my host machine?
You just need to expose a port via docker. If you dont,you will have port 3306 which can be accessed via the local containers but if you bind port 3307:3306 for instance you can connect to mysql locally on 3307
you will need to connect to MySQL server using the docker containers IP that can be found using docker inspect.
This can also help you:
https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55

Runing multiple mysql containers with different port using host network

I have a Mysql server running on the host using default port 3306. I Want to run a MySQL docker container using network host but with a different port.
My configuration is defined in a docker-compose file. After building the image and tried running the container, it starts and shutdown with port conflict notice.
Is there a way to dynamically change the container port before starting up? I don't want to use the network bridge.
If using host networking is a hard requirement, then nothing in Docker space will be able to control or limit what ports the service does or doesn't use. You need to change a service-specific configuration file, environment variable, or command-line argument to make it listen somewhere else.
Particularly for servers that listen on a single TCP port (like most database and HTTP-based servers) the default Docker bridge/NAT setup should work fine; alternate setups like host networking and macvlan are unnecessary. If you're willing to use the standard setup, this is trivial:
version: '3'
services:
mysql:
image: mysql
ports: ['9999:3306'] # listen on host port 9999 instead
docker run --name 'dockername' -e MYSQL_ROOT_PASSWORD='password' -p 1000:3306 -d mysql
docker exec -it 'dockername' mysql -uroot -p
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password'
flush privileges;
here 1000 is the port at which you want to run your mysql docker container.

How to access a Docker container in the local network?

I have pulled an MYSQL image from Docker Hub and ran it by setting necessary credentials, such as username, password etc. Then I have inspected the container by:
docker inspect CONTAINER_ID
where I got the IP address for the MySQL databese. Since the Docker deamon connect through bridge network, my IP addess was: 172.17.0.2. By specifying this IP I can connnect to the Database from the host computer(Fedora). But, how do I connectd to the MySQL database from another machine in the same LAN?
You need to bind the container port to a host machine port. Adding -p to the docker run command:
docker run -p HOST_PORT:CONTAINER_PORT
I.e.:
docker run -p 3306:3306 .......
So MySQL is available from the Fedora's LAN interface.

Connecting to localhost mysql server from inside docker container on macOS

How can I connect to localhost mysql server from a docker container on macOS ?
One way to do it using --add-host but that requires me to pass some name like "myhost".
Is there any way in macOS so that references to localhost from inside docker container actually refer to docker host ?
On MacOS docker provide special DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.
use host.docker.internal
to connect to the host running the Docker.
this works From docker version 18.03 onwards only and This is for development purposes and will not work in a production environment outside of Docker Desktop for Mac.
( refer the page https://docs.docker.com/docker-for-mac/networking/ for more info )
sample connection string for oracle, jdbc:oracle:thin:#host.docker.internal:1521/orcl
From inside of a Docker container, how do I connect to the localhost of the machine?
You should be able to connect to MySql running on host machine using host machine actual IP address. In MacOS, try to find your ip by command ifconfig. Mostly using IP assigned to en0 i.e. your ethernet interface should work. Just call that IP from within your container.
*localhost or 127.0.0.1 or 0.0.0.0 doesnt call host machine as they are local to container itself.