Below is my docker-compose.yml file
version: '3.2'
services:
mysql-server:
container_name: mysql
ports:
- "13306:3306"
environment:
MYSQL_ROOT_PASSWORD: 12345
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: secret
image: mysql/mysql-server
grafana:
image: grafana/grafana
container_name: grafana
environment:
DB_HOST: mysql-server:3306
ports:
- "3000:3000"
depends_on:
- mysql-server
I would like to connect to grafana and use mysql to be my datasource.
However, when I connect to grafana using 'container ip address + port' 172.19.0.3 (retrieved by using 'docker inspect containerID'):3306. It cannot connect to grafana. I tried to close my firewall but still failed. Or my method of connection is wrong?
Besides, when I set MySQL as datasource in grafana, is the host be: mysql-server:3306?
So far you have not cared about networking of the two containers. Yes, both of them expose a port on the host - but you are not trying to access that one. Instead you are trying to make a connection from one container directly to the other container.
First step is to create a bridged network.
Then define both containers to be connected to that network.
Finally you can use the name of one container as hostname to connect from the other container.
Related
I am facing a problem trying to use docker and two port forwardings. Basically I have:
A docker container hosting a MySQL database running on port 3306 in the container
The host of the container, where port XXXX is linked to the 3306 of the container with the docker-compose command ports: - XXXX:3306; I can access my container within the host using PhpMyadmin. So, so far so good
I create a bridge with a proxy server on port 3336 created with a command: ssh -i key.pem -R 3336:localhost:XXXX ubuntu#IP
I then have a client (say Mysql Workbench) which is connected to the proxy using another tunnel : ssh -i key.pem -L 3306:3336 ubuntu#IP
I tried to summarize everything in the following picture with XXXX being 3306 (the green box).
When I try to connect to the database using this rather complex method, it succeeds when XXXX=3306. However, when XXXX=8701 for example, it does not work anymore. Do you have any idea why ? The error I get is a classic timeout: UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT
Thank you in advance for your help.
Best,
B
I found the issue, which was related to the docker-compose.yml file;
Previously I had:
version: "3.7"
services:
db:
build:
context: ./database
command: --default-authentication-plugin=mysql_native_password --sql_mode=""
restart: always
cap_add:
- SYS_NICE
volumes:
- db_data:/var/lib/mysql
ports:
- ${MYSQL_HOST_PORT}:${MYSQL_PORT}
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_TCP_PORT: ${MYSQL_HOST_PORT}
env_file: ./.env
The trick was to have the same port in and outside of the container ${MYSQL_HOST_PORT}:${MYSQL_HOST_PORT}
Hope it would help others in the same situation
I am running MySQL via docker-compose and trying to connect to it from the host, not another container, via a JDBC connection string for an app running on the host and not in a container. The environment is locked down, I cannot create new docker networks or bind a container to 'host' network, bridge is the only option. I can connect to the MySQL instance using the IP address of the container, but this is not much use - I need to use localhost or 127.0.0.1. Is this possible? If so, how?
docker-compose.yml
version: '2.2'
services:
db:
container_name: mysql_db
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'test-db'
MYSQL_USER: 'test-user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
network_mode: bridge
ports:
- '3306:3306'
expose:
- '3306'
volumes:
my-db:
I can connect using the container's IP, like so:
jdbc:mysql://172.17.0.3:3306/test-db
But cannot connect using localhost, 127.0.0.1 or the container name mysql_db like so:
jdbc:mysql://localhost:3306/test-db
jdbc:mysql://127.0.0.1:3306/test-db
jdbc:mysql://mysql_db:3306/test-db
Any ideas?
Since 127.0.0.1 and localhost means the host your docker is currently running, so you cannot connect to your host's mysql.
I also recommend connect container using hostname:
docker run -it --network bridge --rm mysql mysql -hmysql_db -utest-user –ppassword
I am new to Docker. I have overtaken another guys business and I need to exchange a running website content with a new one. The running content is a typo3 website and the new one is Wordpress based. The servers handles many different Websites with different urls managed by a nginx reverse proxy.
My problem: I cannot connect to the new database.
My compose file:
version: "2"
services:
#Database
db:
image: mysql
volumes:
- /srv/db/production/website/wordpress/current:/var/lib/mysql
restart: always
networks:
- proxy-tier
- post
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress_website
MYSQL_USER: wordpress_website
MYSQL_PASSWORD: password
#Wordpress
wordpress:
depends_on:
- db
image: wordpress
restart: always
networks:
- proxy-tier
- post
volumes:
- /srv/www/production/website/wordpress/current:/var/www/html
environment:
- VIRTUAL_HOST=website.com
- VIRTUAL_NETWORK=nginx-proxy
- VIRTUAL_PORT=80
#- VIRTUAL_PORT=443
- LETSENCRYPT_HOST=website.com
- LETSENCRYPT_EMAIL=email#website.com
- WORDPRESS_DB_USER=wordpress_website
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_NAME=wordpress_website
networks:
proxy-tier:
external:
name: nginxproxy_default
post:
external:
name: mail_frontend
Info:
the networks have been made prior to my work so I think they are
working.
I can perfectly connect to the new database within the terminal (it
is just completely empty)
The wp-config data is correct but the Host just says "mysql".
I have checked with other working docker+mysql+wordpress files and
they have the same host.
I have tried to set different port like ports:- "3307:3306" but it
did not help.
What am I doing wrong? I really need help because I am lost.
Thank you!
$ docker-compose down
$ docker ps -a ##check that the containers are not running
Clean the volumes of the "db" and start again
If the containers are OK, you shuld try again to connect to a host port and see the DB from there.
In the container "db" ->
ports:
- "3309:3306"
- "portHOST:porContainer"
Bye.
What I'm trying to do is, connect from my spring-boot app to mysql database in Docker. Each in their own container.
But I must be having something wrong because I can't do it.
To keep it simple :
application-properties :
# URL for the mysql db
spring.datasource.url=jdbc:mysql://workaround-mysql:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640
# User name in mysql
spring.datasource.username=springuser
# Password for mysql
spring.datasource.password=admin
#Port at which application runs
server.port=8080
docker-compose for MySQL:
version: '3'
services:
workaround-mysql:
container_name: workaround-mysql
image: mysql
environment:
MYSQL_DATABASE: workaround
MYSQL_USER: springuser
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
MYSQL_ROOT_HOST: '%'
ports:
- "3308:3306"
restart: always
So pretty simple right ? Database I start with docker-compose up:
All seems to be working fine so far.
Now that I have db started, to the application, this is its docker-compose.yml:
version: '3'
services:
workaround:
restart: always
# will build ./docker/workaround/Dockerfile
build: ./docker/workaround
working_dir: /workaround
volumes:
- ./:/workaround
- ~/.m2:/root/.m2
expose:
- "8080"
command: "mvn clean spring-boot:run"
For its Dockerfile I use Linux Alpine and Java.
FROM alpine:3.9
....add java...
RUN apk update
RUN apk add dos2unix --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community/ --allow-untrusted
RUN apk add bash
RUN apk add maven
Super simple. Now let's start the application :
Unknown host, so let's try the IP then :
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' workaround-mysql
# URL for the mysql db
spring.datasource.url=jdbc:mysql://172.20.0.2:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640
Now I get timeout:
As you can see I get error. What is wrong with my setup and how to fix
this? Either I have unknown host exception or Refused to connect or connection timeout.
I have tried:
Using ip of a container in my application.properties, didn't work
Different ports for MySQL and application
Different images and versions of MySQL
Having everything in one docker compose with wait
timer for database.
Minimal setup with
https://github.com/hellokoding/hellokoding-courses/tree/master/docker-examples/dockercompose-springboot-mysql-nginx
Also resulted in communication link failure, Site was accessible but I
doubt that db was connected properly.
Notes:
I run this all on one computer I use port 3308 because I have local
MySQL db at 3306.
Here is docker ps -a
#Vusal ANSWER output :
Only thing different from code in answer I did wait for database to be ready 30 seconds
command: /bin/bash -c "sleep 30;mvn clean spring-boot:run;"
Try this docker-compose.yml:
version: '3'
services:
workaround-mysql:
container_name: workaround-mysql
image: mysql
environment:
MYSQL_DATABASE: workaround
MYSQL_USER: springuser
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
MYSQL_ROOT_HOST: '%'
ports:
- "3308:3306"
restart: always
workaround:
depends_on:
- workaround-mysql
restart: always
# will build ./docker/workaround/Dockerfile
build: ./docker/workaround
working_dir: /workaround
volumes:
- ./:/workaround
- ~/.m2:/root/.m2
expose:
- "8080"
command: "mvn clean spring-boot:run"
And update your application.properties to use the next JDBC connection url:
spring.datasource.url=jdbc:mysql://workaround-mysql:3306/workaround?serverTimezone=UTC&max_allowed_packet=15728640
It should work when both containers in the same docker-compose file, because docker-compose creates default network for containers, so they can resolve each other by name.
What you haven't tried so far is running both containers on the same Docker network.
First, forget about IP addressing - using it should be avoided by all means.
Second, launch both compose instances with the same Docker network.
Third, do not expose ports - inside bridge network all ports are accessible to running containers.
Create global network
docker network create foo
Modify both compose files so that they use this network instead of creating each one its own:
version: '3.5'
services:
....
networks:
default:
external: true
name: foo
Remove expose directives from compose files - inside one network all ports are exposed by default
Modify connection strings to use default 3306 port instead of 3308
Enjoy
In order for the service to connect with MySql through docker it has to be in same network, look into Docker network
But for better solution I would suggest you to write a single docker compose file for MySql and Spring boot.The reason is it will easily be linked when you do that.No need any other configuration.
version: "3"
services:
mysql-service:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=db
- MYSQL_USER=root
- MYSQL_PASSWORD=pass
- MYSQL_ROOT_PASSWORD=pass
spring-service:
image: springservce:latest
ports:
- "8080:8080"
depends_on:
- mysql-service
Before you try to connect to the Docker container you should stop mysql in your computer then go to the application.properties and type:
spring.datasource.url=jdbc:mysql://localhost:3306/NAME_OF_YOUR_DB_HERE?useSSL=false&allowPublicKeyRetrieval=true
Regarding localhost, you should inspect the mysql container and pick the IP address and use it instead. most likely is 172.17.0.2. If it did not work then use localhost.
I'm trying to containerize my django file, and I keep running into the issue:(2006, ’Can\‘t connect to local MySQL server through socket \‘/var/run/mysqld/mysqld.sock\’ (2 “No such file or directory”)
I found out later mysql.sock is in this location:/tmp/mysql.sock instead of /var/run/mysqld/mysqld.sock, how do I change the location for docker to see /tmp/mysql.sock
Here is my docker-composr.yml:
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: somepassword
adminer:
image: adminer
restart: always
ports:
- 8080:8080
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have followed the instructions on the mysql docker website to link mysql instance to a container
EDIT: I read another stack overflow similar to this, I changed my django code to 'HOST': '127.0.0.1' in DATABASES now I get : (2006, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
Your host should be db. When using docker-compose, you address different servers by their service name.
So, in settings.py, you should have:
DATABASES = {
'default': {
'HOST': 'db',
...
}
}
If you want to connect to your containerized MySQL server both inside and outside of the container, you'll first need to make sure the port is mapped on the host machine:
services:
db:
image: mysql
ports:
- "3306:3306"
...
That will allow you to access MySQL using localhost or 127.0.0.1 directly on your host machine.
If you want to be able to run Django in both the web container and also on the host, you'll need to override the DATABASES setting depending upon the scenario. The web container will need to use a HOST value of db, whereas your local machine will need a value of localhost.