Docker: Unable to access mysql container remotely - mysql

I am using docker toolbox v17.03 in Windows 10 Home. I pulled latest mysql server and run the container.
> docker image pull mysql:latest
> docker container run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql
> docker logs test-mysql
I am able to see mysql running. I then moved to Sql client and used the ipaddress (given by docker-machine ip). I get Access denied. I also tried the ip address from docker inspect test-mysql, same result )
Not sure what is wrong here?

I think I found the problem. I didn't map the port. As soon as I change the docker run as follows , I am able to connect via ip address of docker-machine
> docker container run --detach --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" -p 3306:3306 mysql

Related

Connecting 2 conatiners with docker --network

I am trying to connect a phpmyadmin container to another mysql container. I have created a network with docker network create and added my two containers. But when I tried to login with phpmyadmin, the password is incorrect error displayed each time.
I checked that the two containers are available to my networks which I already created so I tried to ping mysql container to phpmyadmin container but unfortunately I had negative results.
I followed this tutorial:
https://tecadmin.net/tutorial/docker/docker-networking-example/
Can you explain to me how the commend docker run --network works exactly ?
If everything is ok, so you can ping one container from another using container name. When you create a network between 2 containers, it creates an alias with the container name and the container IP.
For instance
docker network create my-bridge-network
docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server --network my-bridge-network
docker run --name phpmyadmin -d -e PMA_HOST=mysql -p 8080:80 phpmyadmin/phpmyadmin --network my-bridge-network
In this case when you exec ping from phpmyadmin container you should be able to ping mysql container
docker exec phpmyadmin ping mysql # if in phpmyadmin `ping` program is available

Connect SQL developer with MYSQL server running on Docker container

Could you please guide me - How can I connect my local SQL developer to MY SQL Server running on Docker container.
Check for the "bind"in my.cnf in docker running container, if its says 127.0.0.1 it will not connect from outside , you probably needs to change to 0.0.0.0 and restart the mysql server
I have used this command to run container -> docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
It solved that issue for me.

Docker communication without using legacy links

I'm trying to create a mini-demo with docker using mysql and phpmyadmin and i'm trying to make the two docker containers communicate with each other without using the --link flag since this has been flagged as "legacy" by docker (https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/#/connect-with-the-linking-system)
I managed to do this using docker-compose using the network section, but I want to implement the same scenario using normal dockerfiles and running the two containers in command prompt.
Here are the two dockerfiles I created:
Dockerfile for mysql
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=12345678
ENV MYSQL_DATABASE=mysql
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=12345678
Dockerfile for pma
FROM phpmyadmin/phpmyadmin:4.6
ENV PMA_HOST=mysql
ENV MYSQL_ROOT_PASSWORD=12345678
Docker images are created correctly using docker build and these are the commands that i use to run the two containers:
mysql:
docker run -d --name mysql sebastian/db-mysql
pma:
docker run -d -p 7777:80 --name pma sebastian/db-pma
When i try to connecto to Pma using username root and password 12345678 i get the following error:
mysqli_real_connect(): (HY000/2005): Unknown MySQL server host 'mysql' (-2)
I'm sure I'm missing something when spinning the two containers and I cannot fully understand how the two containers are suppose to communicate and/or how pma will find host mysql (the name i defined when running the mysql container)
Is docker suppose to allow communication between the two containers?
How do containers should find each other by using names and not ip addresses?
P.S. i'm using dockertoolbox on windows 10 (maybe that is the real problem :D )
The problem:
You are not specifying any networks in your docker run so you will use default bridge, Default bridge will not give you internal DNS but containers on that network can communicate via IP Addresses.
Follow these steps:
First create a user-defined network:
docker network create <yournetworkname>
Now run containers using the network we just created:
docker run -d --name mysql --network <yournetworkname> sebastian/db-mysql
docker run -d -p 7777:80 --name --network <yournetworkname> pma sebastian/db-pma
User defined networks provide connectivity by default and internal dns to the containers on the same network. For example you can ping mysql from pma by:
ping mysql

Connect docker container to local workbench MySQL DB

HI I have my web app running on my local machine and connected to Mysql workbench, I am now trying to dockerize the webapp. I can't seem to get it to connect to the DB on my local dev machine (I am running Docker Desktop for Windows), can anyone tell me how I would go about this? Here is what I have so far.
`docker run -it -e "CATALINA_OPTS=-Dspring.profiles.active=dev -DPARAM1=DEV" -p 8080:8080 -p 8005:8005 -p 8009:8009 -p 3306:3306 --add-host=docker:192.168.1.7 -v C:\myapp\trunk\target\myapp.war:/usr/local/tomcat/webapps/myapp.war --name waitapp tomcat:8.0.38-jre8`
after a few second, I run docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a1764dd9640 tomcat:8.0.38-jre8 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 0.0.0.0:8005->8005/tcp, 0.0.0.0:8009->8009/tcp, 0.0.0.0:8080->8080/tcp waitapp
The container seems to be running, but I get a 404 Not found when I try the rest request, this is the same as I do when running from inside spring tool suite using built in tomcat server.
NOTE
I don't want to run a separate mysql container and link the two over a network, I just want to try get my newly created docker app to connect to my local DB MySQL.
As mentioned on this post, you can try 2 things:
Add gateway and use it from containers, like
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
In addition to your app container, run proxy (ngnix?) container, which will rout the calls to DB when required
This answer also show how can you obtain the host IP inside the docker container.

unable to connect to dockerized mysql container locally

I am still a beginner with docker, trying to use docker to help in my development prototyping. My environment is Mac using boot2docker, version as below
Client version: 1.3.1
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 4e9bbfa
OS/Arch (client): darwin/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa
I ran the command as below:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -d mysql -p 3306:3306
docker start mymysql
I can see the process running as below:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22d3f780c270 mysql:5 "/entrypoint.sh -p 3 2 minutes ago Up 2 seconds 3306/tcp mymysql
However I still could not connect to the mysql instance running in the docker. I tried connect to the ip retrieved by :
$ boot2docker ip
The VM's Host only interface IP address is: 192.168.59.103
Please give me a pointer on how to solve this issue, I went through the tutorial but I am not sure what went wrong.
The command you used should give an error. The syntax for docker run is as follow:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
You have to submit the options to docker run before specifying the image used (mysql in your case), and if it's the case, the command and possible argument(s) to that command.
Not specifying a command will run the default command in the image.
Before running again the container you should stop and remove the old one:
docker kill mymysql
docker rm mymysql
And, following your example you should run:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -p 3306:3306 -d mysql
As you set manually a port mapping from container's port 3306 to the same port of your Boot2docker VM, you should can access to MySQL using the IP of the Boot2docker instance, typically 192.168.59.103, and connecting to port 3306.