how to make my database accessible to my docker container? - mysql

I have a docker container which is based on a springboot project and generated with this command :
docker run --net=host -d --restart unless-stopped -v /home/ramses/dockerTest:/uploads/deployment --name ramses-bl2 abyster01/ramses-bl2:528
but the container does not access to my database, as you can see in this error :
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
My container is launched in host mod, and i verified the root (the user i set in my springboot project properties) is set as user in my database as you can see in this screenshots :
Finally i test connection to my database successfully with the same credentials as in my properties.
But i don't know why i'm getting this error.

For what you need it is not necessary to run in host mode. In general you should always avoid doing so.
First of all check the IP address of the host on your local network (not the local interface 127.0.0.1 but something like 192.168.X.X or similar).
Make sure you can access your database from host by connecting to this IP and the correct port. Also check that the user you use can connect also from hosts other than localhost.
Then run the container with the additional flag: --add-host mydatabase:192.168.X.X. Your application inside the container should use mydatabase as the database server name.

I would recommend for you to use a docker-compose.yml file. Add to the file the following:
version: '3'
services:
mysql-standalone:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=***
- MYSQL_USER=***
- MYSQL_PASSWORD=**
volumes:
- data:/var/lib/mysql
spring-boot:
image: *your-app-name-image*
ports:
[8080:8080]
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-standalone
volumes:
data:
Then in the application.properties have the following configuration
server.port=8080
spring.datasource.url=jdbc:mysql://<container-name>:3306/<db-name>
spring.datasource.username=***
spring.datasource.password=***
Finally, run docker-compose up.
Note that one of the differences between docker run versus docker-compose is that docker-compose reads configuration data from a YAML file instead of having to set your configuration from a cli

Related

Spring Boot + MySQL docker containers

I have two containers, one with a Srping app and another with mysql. When I run the Spring app on my local machine, it successfully connects to my MySQL db running in a docker container. Once I spin up the Spring container I get a connection error, the app is unable to communicate with the MySQL container. How do I configure the Spring container to communicate with the db container? I've tried creating a bridge network to no avail. I believe my issue is spring.datasource.url=jdbc:mysql://localhost:3309/library but when I try with the network id the jar fails to build spring.datasource.url=jdbc:mysql://lms-network/library. I've been following this tutorial. docker image.
There are couple of ways we can solve it.
Using docker-compose
Put both the containers in docker-compose file. Example:
version: "3"
services:
spring-boot-service:
build:
context: .
ports:
- 8080:8080
environment:
DB_HOST: mysql-db
DB_NAME: library
DB_USER: <user>
DB_PASSWORD : <pwd>
mysql-db:
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: <db_name>
#... other properties
then in application.properties, use connection string as :
spring.datasource.url=jdbc:mysql://${DB_HOST}:3306/${DB_NAME}
then use docker-compose up to run both containers together. This way they will share network through docker-compose.
Using host.docker.internal
When you run your spring boot app and mysql in separate containers, you can't refer the mysql database from localhost anymore as localhost means pointing to your container. Since mysql is running on your host machine, you can refer it by using host.docker.internal instead of localhost in connection string.
So you connection string should be
spring.datasource.url=jdbc:mysql://host.docker.internal:3309/library

Error while running docker compose up while connecting to sql database

I have setup docker environment in my local machine to run our development code by pointing to dev mysql database url. Done all configurations and was able to build the docker using docker-compose build command. Even though i was able to build it successfully , on running the command docker-compose up , i am getting numerous errors.
Few of the errors are :
ERROR - TransactionManager Failed to start new registry transaction.
ERROR- AsyncIndexer. Error while indexing
I saw another answers in stackoverflow and updated my code accordingly like limiting max active connections to field to 50 in master-datasources.xml file and changing the configuration url to the below format.
jdbc:mysql://${db_url}:${db_port}/${carbon_db}?autoReconnect=true&relaxAutoCommit=true
Docker version : 19.03.5
Docker compose version : 1.24.1
My docker-compose.yml file:
services:
wso2am:
build: ./apim
image: wso2am:2.1.0
env_file:
- sample.env
ports:
- "9444:9444"
- "8281:8281"
- "8244:8244"
depends_on:
- wso2is-km
wso2is-km:
build: ./is-as-km
image: wso2is-km:5.3.0
env_file:
- sample.env
ports:
- "9443:9443"
My sample.env file:
HOST_NAME=<hostname>
DB_URL=<db_connection_url>
DB_USER=admin
DB_PASS=adminpassword
DB_PORT=3306
CARBON_DB=carbondb
APIM_DB=apimdb
ADMIN_PASS=<wso2_password>
Can anyone provide a solution for this issue.

Can't connect to database: Access denied for user

I am setting up a CakePHP 3.7 application and using docker compose. I have a mysql service as well that I'm trying to connect to, but I am getting this error: Access denied for user 'ws_user'#'172.20.0.3' (using password: YES)
I am granting permissions to the user like so: GRANT ALL PRIVILEGES ON mydb.* TO 'ws_user'#'%' IDENTIFIED BY '<superSecretPasswordHere>'.
If I use the root credentials, cakephp is able to make the connection just fine.
I also expose the mysql service on port 3030 to my local machine and I am able to connect with the ws_user credentials just fine.
I also setup mysql running on my local machine with the same credentials and cake is able to connect to host 172.17.0.1 just fine as well.
I'm perplexed as what could be the problem. It sure seems like it's a permissions problem (because of the error message), but I'm able to connect via the exposed port via the command line. My next thought was that it might be because of special characters in the password, but again, if I connect to mysql running on my host machine, it works fine with the same password.
Here is my docker-compose file:
version: '2'
# define all services
services:
# our service is called CakePHP ;-)
cakephp:
# we want to use the image which is build from our Dockerfile
build:
context: .
dockerfile: Dockerfile
# apache is running on port 80 but we want to expose this to port 4000 on our local machine
ports:
- "80:80"
# we are depending on the mysql backend
depends_on:
- mysql
# we mount the working dir into the container, handy for development
volumes:
- .:/var/www/html/
environment:
- SECURITY_SALT
- MYSQL_HOST
- MYSQL_USERNAME
- MYSQL_PASSWORD
mysql:
# we use the mysql base image, version 5.6.36
#image: mysql:5.6.39
build:
context: .
dockerfile: Dockerfile.mysql
ports:
- "3030:3006"
# we mount a datavolume to make sure we don't lose data
volumes:
- mysql_data:/var/lib/mysql
# setting some envvars to create the DB
environment:
- MYSQL_ROOT_PASSWORD
- MYSQL_DATABASE
volumes:
mysql_data:
From "cakephp" you connect to "mysql:3306". This should be in your connection string.
From your host you can connect to "127.0.0.1:3030" to verify that your database accepts remote login.
Then you should check the credentials that they are the same. I suggest you put them in a .env file and then test the connection by "copy-paste" of the values.
you can check the values that are actually passed to the containers by running:
docker-compose config
This shows you the exact version of the docker-compose file that will be sent to the docker engine.
Hope this works, otherwise drop me a comment.

Docker-compose problem copying MySQL config in CircleCI

The following works fine on a local machine, but fails when checked into CircleCI:
mysql:
image: mysql:5.7
ports:
- 3306:3306
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_ROOT_HOST=%
restart: always
volumes:
- ./docker/mysql/mysqld.cnf:/etc/mysql/conf.d/mysql.cnf
There is a file at ./docker/mysql/mysqld.cnf under the checked out project.
The error shown on CircleCi is:
ERROR: for proj-server_mysql_1 Cannot start service mysql: b'oci
runtime error: container_linux.go:265: starting container process
caused "process_linux.go:368: container init caused
\"rootfs_linux.go:57: mounting
\\\"/home/circleci/max/proj-server/docker/mysql/mysqld.cnf\\\"
to rootfs
\\\"/var/lib/docker/aufs/mnt/4a9af90d342b491ae92af5a88360d2e34fce0d21c15f8a648767e89fb51aa\\\"
at
\\\"/var/lib/docker/aufs/mnt/4a9af90d342b491ae92af5a88360d2e34fce0d21c15f8a648767e89fb51aa/etc/mysql/conf.d/mysql.cnf\\\"
caused \\\"not a directory\\\"\""\n: Are you trying to mount a
directory onto a file (or vice-versa)? Check if the specified host
path exists and is the expected type'
It's not possible to use volume mounting with the docker executor, but with using the machine executor it's possible to mount local directories to your running Docker containers. You can learn more about the machine executor here on our docs page.
https://support.circleci.com/hc/en-us/articles/360007324514-How-can-I-mount-volumes-to-docker-containers-

JDBC Communications link failure with mysql docker container

When my java spring application tries to connect to the database I get the following:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The connection url in the spring application is as follows:
jdbc:mysql://mysqldbserver:3306/supersede_orchestrator_spring?useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
where mysqldbserver is the service name from the docker-compose config:
version : '3'
services:
springappserver:
build:
context: .
dockerfile: web.dockerfile
ports:
- "8081:8080"
networks:
- mt-network
volumes:
- .:/vol/development
depends_on:
- mysqldbserver
mysqldbserver:
build:
context: .
dockerfile: db.dockerfile
ports:
- "13306:3306"
networks:
- mt-network
environment:
MYSQL_DATABASE: supersede_orchestrator_spring
MYSQL_USER: supersede_orch
MYSQL_PASSWORD: ****
MYSQL_ROOT_PASSWORD: ****
container_name: orchestrator_mysqldbserver
networks:
mt-network:
driver: bridge
In the mysql docker container I already adjusted the bind-address to 0.0.0.0. The privileges of the supersede_orch user are set to %.
When I connect to the springappserver docker container, I can reach the database via telnet mysqldbserver 3306 and I can also connect to the database on the command line from the spring docker container: mysql -h mysqldbserver -u supersede_orch -p.
Though the java spring application fails to connect to the DB running on the other docker container.
The web.dockerfile looks as follows:
FROM java:8-jre
VOLUME /tmp
ADD build/libs/feedback_orchestrator-2.0.0.jar app.jar
RUN apt-get update
RUN apt-get install mysql-client -y
RUN apt-get install libmysql-java -y
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Djava.net.preferIPv4Stack=true","-jar","/app.jar"]
So, I tried to make sure that java uses IP4 to resolve mysqldbserver (I also used the corresponding IP address for mysqldbserver which also failed).
Finally docker images -a:
42db4e656e6e orchestrator_springappserver "java '-Djava.secu..." 16 minutes ago Up 16 minutes 0.0.0.0:8081->8080/tcp orchestrator_springappserver_1
e2b0b5cc15ac orchestrator_mysqldbserver "docker-entrypoint..." 2 hours ago Up About an hour 0.0.0.0:13306->3306/tcp orchestrator_mysqldbserver
Do you have any idea what the problem could be? Thank you!
The above configuration is correct. I was just stupid enough to forget rebuilding the jar. So, the jdbc url was not updated. It works now perfectly.
I got this error. For me I forgot to start up WAMP, which has the mysql info.