initialize mysql in host and access in docker container - mysql

I am creating very simple database like
mysql -u root -e "CREATE DATABASE IF NOT EXISTS example"
and I want to access it in the doker container like
"mysql://root#localhost/example"
both host and conteiner is ubuntu machine
I suppose answer is very simple but I got lost - Thanks !

With docker, all the stuff can be accessible with localhost. But you need to expose the service to a local port.
So with -p 80:80 in your run command, you will be able to expose the port 80 on your container, to your local port 80.
Docker run doc
I hope it will help you!

Related

Docker access to GCP SQL proxy

I'm facing a issue and I will appreciate you someone can help me.
I cannot connect to mysql proxy from my docker container.
GCP Enviroment
I have a GCP VM unix with docker installed.
This VM has already the Google Could Sql Proxy configured, and accessible through mysql -u USER -p --host=127.0.0.1 --port=5435
I have a docker container with Ubuntu
Problem
The problem is, inside my container, if I run mysql -u USER -p --host=host.docker.internal --port=5435 or mysql -u USER -p --host=172.17.0.1 --port=5435 I connect access
I cannot connect to mysql. I don't know what I'm missing or doing wrongly.
Thank you!

MySql Docker container refuses to connect to Microsoft Azure (Mac)

First of all - hello. Secondly, I just downloaded Docker and got it to run MySql on my Mac. It has never connected before and I am trying to learn how to make databases.
Everything was running smoothly - I opened Docker and ran my container (it is running), but when I tried to connect with Microsoft Azure, it persistently refused to do so.
Since my password was strong enough to fill the criteria, I checked the port and it is indeed 1433.
So I started to use every variation I could think of in username, just to see what happens.
Nothing. No connection. I read it could be the firewall, but all the instructions I found were Windows and other applications.
Can anyone here point me in the right direction? It seems such a pathetic hill to die on.
docker run --name <your-mysql-name> -e MYSQL_ROOT_PASSWORD=<your-sql-password> -p <host-port>:<container-port> -d mysql
You'll need to expose / publish the port (-p command), if not MySQL cannot be accessed by external sources.
For example:
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=somepassword -p 3306:3306 -d mysql
The user name is "sa". I found it on a blog and tried it out. It worked.

How to bind mysql port from host to docker container without port clash

I have a docker container running a Flask application that connects to a mySQL server. The mySQL server is hosted on the host machine at port 3308 on a windows 10 machine.
When executing
docker run -p 5000:5000 -p 3308:3308 -t webui
I receive the error
Ports are not available: listen tcp 0.0.0.0:3308: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
due to the port being used by the mySQL server on the host machine
How do I map the port of the mySQL to the docker container such that the Flask application can access the database?
There are 2 ways to achieve this. The first approach is the recommended one.
The first is to add an entry to /etc/hosts inside the container:
docker run -p 5000:5000 -p 3308:3308 --add-host database:<HOST_IP> -t webui
You need to replace HOST_IP with the network IP of your host. Then you can reference the database inside your container using the name "database" (you can also customize this one).
The second is to bind your container to your host's interface:
docker run -p 5000:5000 -p 3308:3308 --bind 127.0.0.1 -t webui
Then you can refer to your database with 127.0.0.1 inside your container.
The issue was caused by the host name. The mySQL database port did not need to be bound to the container as it did not need to receive any inbound calls, only outbound to the database. Resolved by adding a new entry into the container's /etc/hosts file as described here.

Cannot connect to MySQL server inside Docker

First I run mysql image:
docker run -e MYSQL_ROOT_PASSWORD=password -d -p 127.0.0.1:3308:3306 mysql
Then I use container bash:
docker exec -it my_container_name bash
In Bash I can successfully connect to MySQL server via command:
mysql -uroot -ppassword
But when I try to connect to MySQL container from Windows cmd:
mysql -uroot -ppassword -h127.0.0.1 -P3308
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (10061)
If I connect to 192.168.99.100 instead (this ip is returned by docker-machine ip), then the result is the same.
The question is: How do I correctly expose my MySQL port inside Docker to outside Windows?
The error is in your port mapping in the original docker run command, you just need to provide the ports, not the IP address:
docker run -e MYSQL_ROOT_PASSWORD=password -d -p 3308:3306 mysql
You can run docker ps -a to check for the port mapping in the running containers.
You should now be able to connect to MySQL using
mysql -uroot -ppassword -h192.168.99.100 -P3308
First, check netstat -an to make sure the port is open in Windows. If it is, also check the Windows firewall to make sure nothing is blocking connections to the port.
Most of my Docker experience is in CoreOS, so I'm not exactly sure how Windows handles routing traffic into the container. In CoreOS, it uses a proxy. If there is a proxy in Windows, make sure nothing is interfering with it.
Changing the port in which I was running the image worked.
I checked if this port was used by something else, but it was not used. so I just -desperately- run a new container in a different port '3309'. and it worked fine!
Make sure you have entered the port with -P.

Make proxy container for running docker MySQL container

I have running docker MySQL container on production server.
I need to connect to MySQL database from another server.
Container just have EXPOSE 3306, but no binded ports.
So, i understand that binding port to a running container is not possible.
I thinking about creating new "proxy" container, bind ports to listen outside and link it to existing MySQL container.
Will this work?
Sorry for my english
Just run your container with -P option or with -p <host_machine_port>:<container_port>
For MySQL it can be done with docker run -p 3306:3306 mysql
And you can connect to MySQL through yourmysqldomain.com:3306