I have installed docker on Windows machine using docker tool box.
I also have mysql installed on my windows machine and the server is running on port 3306 (localhost - 127.0.0.1 - outside the docker machine)
I am running a sprint boot application inside a docker container with name as 'micra-workq-svc' inside the docker network called 'micra-network'
I want application running inside the docker to connect to this local host.
I spent hours searching on google and none of the links help me to resolve this issue.
I tried running mysql image with this command:
docker run --name micra-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<pass> -e MYSQL_DATABASE=<db name> -e MYSQL_USER=<roo user> -e MYSQL_PASSWORD=<root password >--net=micra-network mysql:latest
I am using following command to run my container:
docker run -e "SPRING_PROFILES_ACTIVE=dev" --network micra-network --env "eureka.client.enabled=true" --env "eureka.host=micra-eureka-server" --env "eureka.instance.preferIpAddress: true" --env "eureka.client.serviceUrl.defaultZone=http://micra-eureka-server:8761/eureka" --env DATABASE_HOST=127.0.0.1 --env DATABASE_PORT=3306 --env DATABASE_NAME=<db name> --env DATABASE_USER=<db user> --env DATABASE_PASSWORD=<db password> --expose 8083 -p 8083:8083 --name micra-workq-svc --link micra-mysql -t <my docker service image>
This is how application.properties look like in spring boot:
spring:
datasource:
password: ${DATABASE_PASSWORD}
url: jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
username: ${DATABASE_USER}
When I run my service I get following error:
Failed to obtain JDBC Connection; nested exception is 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.
My docker machine ip is 192.168.99.100.
Just clean your containers and images as administrator execute this command docker prune - a , put DATABASE_HOST=micra-mysql and run again.
Related
I have MYSQL container Running i am able to execute it and able to connect from host machine using "mysql -h 172.17.0.2 -u root -p". I have executed "docker run --name=mysql-host -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32" to run the docker image.
but when i trying to access through Spring boot app it`s giving connection refused below are the properties in the application.properties
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/auditLog?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
i am runnung my jar as given below
java -jar service-1.0.0.jar spring.config.location=file:///home/10662598/Downloads/entity-search-service-deploymentFolder/entity-search-service/configs/application.properties
Error getting Caused by: java.net.ConnectException: Connection refused (Connection refused)
Your issue is probably because the Spring boot app can't reach the database (mysql), the reason for this is probably because they're not in same network.
If your Spring boot app is running inside container
From your question, I see that you're running containers using docker run..., therefore when you start MySQL and Spring boot app using that command, they're not in same network.
Solution for this is, first create a network, then start MySQL and Spring app to run within that network:
docker network create my-net
Then start your containers as usual but remember to add your containers to the network you just created:
docker run ... --network my-net
Now your containers are in same network and should reach each other, you can verify this by exec to Spring boot app and try curl to Mysql container using its container name (and port):
curl <mysql_container_name>:3306
If Spring boot app is running on host machine
Then simply make sure you map Mysql port to host machine and connect using localhost:<mapped_port>
Good luck :)
Here, you must create a network layer between the containers.
First create network layer
docker network create spring-mysql-net
Build & Run MySql Docker image
docker run --name mysql -d -e MYSQL_ROOT_PASSWORD=root -v
mysql:/var/lib/mysql --network spring-mysql-net mysql:8
Build your spring boot or any API
docker build -t restapi .
Start container on that created the network
docker container run --network spring-mysql-net --name
mysql-restapi-container -p 8080:8080 -d restapi
a bit late but i think you need to forward port 3306 on local machine to the contain port 3306
try this
docker run --name=mysql-host -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32
I am building an application which has parent and child dependency and to build of my application which is the final stage of build i need to connect to mysql for it during build stage itself.
In this i am getting the error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I have mentioned my docker file code i am using and for mysql i have pulled image from dockerhub following instructions from below link:
https://dev.mysql.com/doc/mysql-installation-excerpt/5.5/en/docker-mysql-getting-started.html
And i was planning to run this as a separate container using bridge to communicate with my above container using below command:
docker run -d -name app-container-name --link mysql-container-name app-image-name
FROM maven:3.5.4-jdk-8 as maven
COPY ZP ZP
COPY CommonApp CommonApp
RUN cd ZP && mvn clean install
RUN cd CommonApp && mvn clean install package -U && mvn install:install-file -Dfile=/CommonApp/target/commonapp-0.0.1-SNAPSHOT.jar -DgroupId=com.z -DartifactId=commonapp -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar;
FROM mysql:5.7
# ROOT PASSWORD
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_USER=root
ENV MYSQL_PASSWORD=root
ENV MYSQL_DATA_DIR=/var/lib/mysql \
MYSQL_RUN_DIR=/run/mysqld \
MYSQL_LOG_DIR=/var/log/mysql
RUN /etc/init.d/mysql start && \
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root';FLUSH PRIVILEGES;"
#PORT
EXPOSE 3306
FROM maven:3.5.4-jdk-8
COPY ZCApp ZCApp
RUN cd ZCApp && mvn clean package -U
How should i approach this problem. How can i build mysql along with the application itself using dockerfile.?
Had the same issue when built maven project. What makes this different from similar requests is that here you don't link two running containers but instead you link docker daemon, preforming build process, to running container.
For Docker to get access to database during build you have to expose ports of database. Using --link will have no effect because it links containers (and you dont have second container at the moment) and btw is considered as obsolete technique.
You have to explicitly start database container before build process and somehow expose its ports for docker daemon to access them.
Option 1 - using host networking.
First start database:
docker run -d --network=host mysql
Then build:
docker built -t foo .
Docker will see database on localhost during build process because database uses host's network and doesn't need any port exposion.
Option 2 - Expose ports
First start database:
docker run -d -p 3306:3306 mysql
Then build:
docker built -t foo .
Docker will again see database on localhost during build process because port is exposed.
What you have to double check is your connection string in mvn. It has to use localhost and default tcp port 3306
Im running docker on windows and I start up a docker container with MySql like this
docker run -p 3306:3306 --name test -e MYSQL_ROOT_PASSWORD=secret-pw -d mysql/mysql-server:5.5
Then on my host I start up Mysql workbench and try to connect but it does not work.
docker inspect test reveal IP address on 172.17.0.2 but when I ping this I get no reply
Got this working on a linux host and I am pretty sure I have done the exact same steps
What am I doing wrong ?
Help Doc: https://docs.docker.com/samples/library/mysql/
Image link: https://store.docker.com/images/mysql
Command: docker run --name mysql_container_name --expose=3306 -p 3306 -v /my/own/datadir:/path/to/data/dir -e MYSQL_ROOT_PASSWORD=root_pwd -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
mysql_container_name: docker container name
expose: contaner exposeing port
p: host buinding port
/path/to/data/dir: share path between container and host
root_pwd: mysql root password
tag: repository tag
utf8mb4: mysql server character set and collation type
utf8mb4_unicode_ci: mysql server character set and collation type
Example: docker run --expose=3306 -p 3306 --name mysql -v /my/own/datadir:/opt/mysql -e MYSQL_ROOT_PASSWORD=0112358139 -d mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
Following steps to connect remotely:
docker exec -it mydb bash --> this will connect to mySql container.
echo "bind-address = 0.0.0.0" >> /etc/mysql/my.cnf --> this will update the my.cnf file.
service mysql restart --> restart the mySql service.
exit --> the mySql container.
docker inspect mysql | grep IPAddress --> grep the IP address of the contaner.
mysql -h 172.17.0.2 -u root –p --> remotely connect to the mySql.
Your host 3306 port should be forwarding to the container, so try connecting on localhost:3306. When I tried to replicate, got the "Host 172.17.0.1 not allowed to connect to this MySQL server" which means it got through at least.
More on the latter: https://github.com/fideloper/docker-mysql/issues/10
Background:
Ubuntu 14.04 LTS - Docker 1.7 - Virtual Interface with its own IP specfically for use with Docker.
Running Wordpress Latest with linked MySQL 5.7 each with their own data containers.
I need to map ports 80 & 443 to the Wordpress container to enforce SSL site wide.
This run string works perfectly:
docker run --name Web --link db-server:mysql -d -e WORDPRESS_DB_NAME=WP -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=somepassword -p 172.31.25.94:80:80 --volumes-from wp-data wordpress
When I run this string I get an error:
docker run --name Web --link db-server:mysql -d -e WORDPRESS_DB_NAME=WP -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=somepassword -p 172.31.25.94:80:80 —p 172.31.25.94:443:443 volumes-from wp-data wordpress:latest
Error message:
Unable to find image '—p:latest' locally
Invalid repository name (—p), only [a-z0-9-_.] are allowed
I have read the Docker documentation and Googled for this issue and from everything I have found this should work. Is there something I am missing or is this not possible in 1.7?
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.