Can't link symfony app container to mysql container - mysql

I have a tow running containers one with symfony app and the other is mysql database server:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99d17c328612 sbc/gestcom:1.0 "docker-php-entrypoi…" 18 hours ago Up 2 minutes 0.0.0.0:4400->80/tcp musing_moser
97dafcfb4acd mysql "docker-entrypoint.s…" 18 hours ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp suspicious_brattain
I linked mysql container to symfony contianer like so:
docker container run -d -p 4400:80 --link suspicious_brattain:db sbc/gestcom:1.0
suspicious_brattain: is the name of the mysql container
sbc/gestcom:1.0: is the name of the image I built that contains my symfony app (with apache, php and pdo driver needed to work with mysql)
Now when I access the symfony app container to run doctrine:database:create I get this exception:
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection
refused
This is parameters.yml file:
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: my_database
database_user: root
database_password: root
Of course I gave root as password for mysql image when running it like so:
docker container run -e MYSQL_ROOT_PASSWORD=root -d mysql
And to be sure that the symfony container is linked to mysql container this what I get when I inspect the symfony container:
...
"Links": [
"/suspicious_brattain:/musing_moser/db"
],
...
As you can see they are connected, however I still unable to create my database using symfony command and I get Connection refused!

You shouldn't be trying to connect on 127.0.0.1, but to db.
When linking containers, a /etc/hosts entry is created in the container to make configuration more convenient.

Related

Cannot connect to MYSQL docker container

I have created an image of a MySQL database and run it in a container. I would like it on ports 3406/3407, so I call the docker run command like this :
docker run -d -p 3406:3407 --name db ollyw123/shape-shop-db:latest
If I look at my containers it looks like this :
C:\Users\owatkins.ext>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b907f878f82b ollyw123/shape-shop-db:latest "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 3306/tcp, 33060/tcp, 0.0.0.0:3406->3407/tcp db
I would very much like to connect to my database now but I can't seem to get my URL working.
This is what my URL looks like :
jdbc:mysql://localhost:3406/db
Default port of mysql is 3306 not 3407, so you should use port-forwarding with 3306
docker run -d -p 3406:3306 --name db ollyw123/shape-shop-db:latest

Not able to connect to mysql running in docker from another docker container

I have a docker container running a mysql image,docker run command -
docker run --name=mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql/mysql-server:5.7
I am able to connect to this database from my SpringBoot app on localhost:3306, but when I run my springboot app -docker run command-
docker run --name=myservice -d -p 3306:3306 -p 8888:8888 <image-id>
it fails to start up and upon checking the logs I see, the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'liquibase' defined in class path resource
[org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration
$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is
liquibase.exception.DatabaseException:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
How do I make my spring boot app connect to the container running the mysql?
EDIT: adding the data-source part of my application.yml file for my app :
spring:
datasource:
url: jdbc:mysql://localhost:3306/<db-name>
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
jpa:
show-sql: true
hibernate:
ddl-auto: none
h2:
console:
enabledt: true
Got it working by using --link mysql in the docker run command and changing datasource url from 'localhost:3306' to 'mysql:3306' in my application.yml , as by default Docker has a DNS and it can call containers by name.So, once linked and adding grant permission for my user 'root' for all IPs in my DB by bashing into 'mysql' container, I was able to start my service with the DB in another container. However, for a more general solution , creating a network or using docker compose seems like a better idea

How to open MySQL database from docker container in my local HeidiSQL

I ran centos/mysql-57-centos7 container with
docker run -e MYSQL_ROOT_PASSWORD=root centos/mysql-57-centos7
and it works well
ae0b9060f99c centos/mysql-57-centos7 "container-entrypoin…" 16 minutes ago Up 16 minutes 3306/tcp angry_blackwell
but I don`t know how to connect it with my HeidiSQL, i saw the container ip with
docker inspect -f <ContainerID> //172.17.0.2
and I tried to open with the following login:
user: root, pass: root, hostname/ip: 172.17.0.2 port: 3306 but I get: Can`t connect to MySQL server on '172.17.0.2 (10060)'
By default, MySQL only allow root login on localhost.
My advice is to create a new user for you remote access :
https://dev.mysql.com/doc/refman/8.0/en/creating-accounts.html
You need to forward a port
"docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root centos/mysql-57-centos7"
and then connect to localhost:3306.

is it possible to use 3306 port (same port) in docker mysql running container as well as in windows 10 mysql

I deploy my war file in tomcat 7 successfully and start tomcat using following command
docker run -it --rm -p 7008:8080 -v //d/docker_tomcat/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml:ro -v //d/docker_tomcat/webapps:/usr/local/tomcat/webapps:rw tomcat:7.0
when tomcat start it shows following error logs:-
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#9030ca2)
LogAbandoned: true
RemoveAbandoned: true
RemoveAbandonedTimeout: 90
[localhost-startStop-1] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot create PoolableConnectionFactory
i think that above error means that database studentdb is not accessible
here is my hibernate.properties file:-
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.url=jdbc:mysql://10.0.75.x<ip of docker>:3306/studentdb?autoreconnect=true&zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false
show_sql=false
hibernate.jdbc.use_streams_for_binary=false
hibernate.dbcp.testOnBorrow=true
hibernate.dbcp.validationQuery=SELECT 1 FROM DUAL
hibernate.dbcp.testOnReturn=false
hibernate.dbcp.maxWait=2000
hibernate.dbcp.testWhileIdle=true
hibernate.dbcp.minEvictableIdleTimeMillis=1800000
hibernate.dbcp.timeBetweenEvictionRunsMillis=300000
hibernate.dbcp.numTestsPerEvictionRun=5
hibernate.dbcp.removeAbandoned=true
hibernate.dbcp.removeAbandonedTimeout=90
hibernate.dbcp.logAbandoned=true
i think there may be error in hibernate.connection.url property of
hibernate.properties file.
and also doubt is it becuase my windows 10 uses port 3306 for mysql as well as docker also uses port 3306 for mysql. if is it problem then how can i change port of mysql container running in docker with some different port
Use following command to forward your local port to docker container port
docker run -p <LOCAL-PORT>:3306 <mysql-image-name>

Can't connect to Windows 10 Docker mysql

Win 10
Composer version 1.4.1 2017-03-10 09:29:45
PHP 7
npm/Node
Docker CE
Apache 2.4
Powershell
git BASH shell
drush (installed via composer)
Noob Composer/Docker skills
I have a docker config yml specifying how mysql service can start:
version: "2"
services:
mysql:
image: mysql:5.6
ports:
- 3306:3306
volumes:
- /data/nbif_mysql:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
And when I call
#!/bin/bash
docker-compose up -d mysql
I see that the container is running:
PS C:\dev\appname> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1a0ecab8af6 mysql:5.6 "docker-entrypoint..." 2 hours ago Up 5 seconds 0.0.0.0:3306->3306/tcp appname_mysql_1
But, notice that the reported IP is 0.0.0.0:3306->3306/tcp
So when I try to connect with the expected IP, it fails:
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.1' (10060)
How to I tell docker-compose to use that expected IP for docker?
Is this a setup issue, or some config tweak I need to do?
When you bind a port to your host, you have to use localhost instead of the container's IP address, because you're not assigning any local IP address.
Every container always runs in a isolated network (bridge), the container in your compose file will be able to find the others by their hostname, but inside of those containers, they are isolated from the local network so that's why you can't reach them.
In your compose file you only have a mysql container and you're binding that port in your host, so the only way to reach that container is by using localhost:3306
Remember, when you run a docker container it isn't like a server with an IP in your host network, it's more like a virtual machine with an isolated network configuration.
Take a look on the docker-compose docs in this specific topic:
https://docs.docker.com/compose/networking
UPDATE:
The link that finally answered the question was:
https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/