I am new to Docker. Currently, I am trying to connect my Scala container with a MySQL container but just can't find any resources on how I can do it. Is it able for me to create a JDBC connection on the Scala container and connect to the MySQL container using the linking method?
https://docs.docker.com/userguide/dockerlinks/#docker-container-linking
http://www.javacodegeeks.com/2014/12/wildflyjavaee7-and-mysql-linked-on-two-docker-containers-tech-tip-65.html
mysql runs on a port.The default port for this is 3306.
This port on your mysql container should be exposed by adding EXPOSE 3306 in dockerfile.
For best results use official mysql image from docker hub (https://registry.hub.docker.com/_/mysql/ )
use -h"$MYSQL_PORT_3306_TCP_ADDR" in your scala container.
Eg: mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -u -p
This should do the trick!!
Related
As a normal, I start nodejs (3333) and connect it to mysql (3306) installed on my computer successfully.
Now, I start my nodejs to docker (3333:3333), it run successfully, but I want to connect my nodejs inside docker to mysql on my computer or mysql from other server (mysql outside docker), what I need to do now, please.
I see a lot of instruction but they seem to use both node and mysql inside docker
I tried to install a MySQL cluster with the Docker image below.
mysql/mysql-cluster - Docker Image | Docker Hub
The Docker image is pulled and run successfully.
Despite that I could connect to the cluster in the terminal (as shown in the screen capture below), I don't know how to connect to it with MySQL Workbench or DBeaver.
In your docker run command, you can use -p 3306:3306 (or any available port). Then you can use <host>:<port> from Workbench or Dbeaver connection URL.
I assume that you already know how to add new DB connection to MySQL Workbench or DBeaver. The information that you want is the connection URL and the username/password of an authenticated user that you need to use to connect to your MySQL cluster.
For the connection URL: 192.168.0.10 (no port in your example)
You need to have your MySQL Workbench or DBeaver connect to the URL of the MySQL node, which is mysql1 node in your example. As shown in your screen capture, it is 192.168.0.10 without any explicit port. But if you have troubles with the URL, you can run docker ps to check what host and port that your mysql1 is running and exposed at.
For the username/password: root/tpffnrtm1 (the password is the value of MYSQL_ROOT_PASSWORD as shown in your docker run of MySQL node command)
I assume that you just want to connect the DB cluster by any means (root or non-root privileges is totally fine for you).
I am using vessel to run my Laravel project.
As I understand it basically simply creates a put-together docker image of my laravel project with mysql and everything.
Now since its running in docker, is it possible for me to access the mySQL databases with datagrip or tableplus on my host machine?
You just need to expose a port via docker. If you dont,you will have port 3306 which can be accessed via the local containers but if you bind port 3307:3306 for instance you can connect to mysql locally on 3307
you will need to connect to MySQL server using the docker containers IP that can be found using docker inspect.
This can also help you:
https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55
I am working on simple project with mysql + springboot using docker container and i am able to run in my local without any issue. I tried to bring the same containers to AWS ECS to standup but i am facing multiple problems. I did research multiple documents and blogs but could not get the correct content to make this works.
I used the below to stand up the mysql in my local with the container name of "mysqlcontainer"
docker run --restart always --name mysqlcontainer --net dev-network -v /Users/myuser/Develop/mysql_data/8.0:/var/lib/mysql -p 3306:3306 -d
-e MYSQL_ROOT_PASSWORD=password mysql:8.0
Once mysql stood up then i ran the below command to bring my springboot application. This is simple springboot service which has the CRUD operation with mysql.
docker run -d -p 8061:8061 --name user-mysqlapp --net dev-network
--link mysqlcontainer user-mysql
mysql container name is "mysqlcontainer" which runs on the network "dev-network" and i have used these container and network name in second docker command to stand up the springboot application as both container needs to talk each other within the same network.
here is the docker file for springboot application.
FROM openjdk:8-jdk-alpine
EXPOSE 8061 ARG
JAR_FILE=target/user-mysql.jar
ADD ${JAR_FILE} user-mysql.jar
ENTRYPOINT ["java","-jar","user-mysql.jar"]
Here is the datasource that is being used in application.yml within springboot application.
datasource:
url: jdbc:mysql://mysqlcontainer:3306/sample
username: dummy
password: password
driver-class-name: com.mysql.jdbc.Driver
after both containers are running, i was able to connect the mysql with mysqldeveloper and able to execute the below command.
CREATE USER 'dummy'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'dummy'#'%';
create database sample;
use sample;
I have no issue of executing this setup in my local and i did push the springboot application image to dockerhub to use in AWS ECS.
Problem statement:
If i run the mysql container alone in ECS, i am able to run it and able to connect using mysql developer from my local. but if i try to run springboot app image, its not properly linking with mysql container. I tried in FARGATE instance type with awsvpc network mode where i was unable to give link to mysql container. i did tried to create single taskdefinition where both containers were added and both of them were running successfully but in the spring boot log, it says unable to create the communication with mysql container.
Can someone please share some information to stand up this setup in aws ecs or share some link/tutorial/blog where i can see how this mysql + springboot app setup can stand up in aws ecs? if i am able to stand up these containers then i need to create/attach volume to the persisting the data in aws. Thanks in advance...
I am able to setup this spring boot + mysql setup successfully in ECS. Below are the steps were followed.
Create one task definition in which add mysql container first where
provide the necessary env variable to mysql container.
Add another spring boot container in which set the dependency order
container as mysql container with condition of "START".
Create new cluster
Create service on this cluster and then add this task definition on the service.
Since i used fargate type, we can't use link to refer the other container but instead i used 127.0.0.1 ip address in spring boot application yml data source url so that it will refer the local host within the same network. This solves the container linking issue and i am sure we would have some better option but for time being i am using this option.
datasource:
url: jdbc:mysql://127.0.0.1:3306/sample
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