Django container cannot find its mysql container - mysql

I have built my django docker with hostname server_default and with --network=server_default and ran mysql with same network(mysql container has ran before django server) when I check my mysql container everything is ok but when I run my django server it fails with error :
"Can't connect to MySQL server on 'server_default' ([Errno -2] Name or service not known)"
I attached to my server container and I couldn't connect to mysql container.
server_default is a bridge type.
my run commands :
sudo docker run -d -p 8000:8000 --network=server_default scotech-server
sudo docker run -d --network=server_default scotech-db

I couldn't do it with docker individually but with docker-compose.yml and 2 build context I could connect them together : it seems a bad solution :(
version: '3'
services:
scotech-db:
build:
context: ./scotech-mysql-docker
expose:
- 3306
web:
build:
context: ./server
depends_on:
- scotech-db
restart: on-failure
ports:
- "8002:8000"

Related

Laravel Connection with docker mysql

I am connecting my Laravel application with docker-composer.yml
I pull MySQL image and PHPMyAdmin image through CLI and connect MySQL and PHPMyAdmin in a container in the same network and I am trying to connect my Laravel application in localhost.
Laravel ENV:
'DB_CONNECTION=mysql'
'DB_HOST=mysql'
'DB_PORT=3306'
'DB_DATABASE=select_elect'
'DB_USERNAME=root
'DB_PASSWORD=root'
Container Info:
Error:
php_network_getaddresses: getaddrinfo failed: No such host is known.
Please guide me where I am doing a mistake.
If you want to connect not from a container, but from a host - then you need to add docker-compose for MySQL service first:
ports:
- "3306:3306"
e.g.
version: '3.7'
services:
db:
restart: unless-stopped
image: mysql:latest
ports:
- "3306:3306"
CLI:
docker run -p 3306:3306 mysql:latest
This will make the MySQL service and port accessible from outside the docker network.
But if you trying to connect from inside same docker network, check if your service has a name mysql, because you use values: 'DB_HOST=mysql'

How do I frame the SQLAlchemy URI for connecting the Database(8080) and Superset?

Pulled MySQL image using command
docker pull mysql
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
Then made a stack.yml file in my root for mysql
stack.yml :
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
And lastly composed it up.
docker-compose -f stack.yml up (after making the stack.yml file)
After it stopped running I accessed the localhost:8080 page and the mysql database login page was loaded.
Superset setup
git clone https://github.com/apache/incubator-superset.git
cd incubator-superset
docker-compose up
Accessed superset on localhost:8088 page.
How do I frame the SQLAlchemy URI for connecting the Database and Superset?
You can access both applications individually due to it has a different bridge network. But when it comes to connecting each other, You have to connect through the same network for both applications. Superset already running on incubator-superset_default and MySQL running on default bridge network.
Here tested stack.yaml
version: '3.7'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: sample
networks:
- proxynet
networks:
proxynet:
name: incubator-superset_default
Note:
As a user-defined network so use can use the service container name or container IP address as follows
mysql://root:sample#mysql/mysql
mysql://root:sample#172.19.0.5/mysql

MySQL commands inside Docker container

I have the following docker compose:
version: '3.1'
services:
db:
image: mysql:5.5
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=some_root_pw
- MYSQL_DATABASE=some_db
- MYSQL_USER=db_user
- MYSQL_PASSWORD=user_pw
ports:
- 8306:3306
volumes:
- ./db:/var/lib/mysql/some_db
I am accessing the container with the command:
docker-compose exec db /bin/sh
Which drops me at the shell as expected. However, I am unable to access mysql with the information from the docker-compose file.
mysql -u root -p
Prompts for the password, then rejects for bad username or password. I have tried with both root and user. Is there a way I can troubleshoot what is happening with user creation?
NOTE: intentionally older version of MySQL.

"Host '192.XXX.XXX.X' is not allowed to connect to this MySQL server" error when attempting to access mysql Docker container from another container

I have a docker compose file which creates one container to run my app (Ruby on Rails) and another to run a mysql server.
version: "3"
volumes:
test-db:
external: false
services:
db:
image: mysql:5.7
env_file: .env
environment:
MYSQL_DATABASE: test_development
MYSQL_ROOT_PASSWORD: foobar
MYSQL_USER: root
MYSQL_PASSWORD: foobar
volumes:
- test-db:/var/lib/mysql
ports:
- "3306:3306"
app:
build:
./
image: test_app:latest
env_file: .env
command: "rails server -b 0.0.0.0"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
tty: true
stdin_open: true
I exec into the db container, and can access the mysql cli as expected.
$ docker exec -it test_db_1 mysql -pfoobar
Welcome to the MySQL monitor. Commands end with ; or \g.
However, I am met with the following when I attempt to access the server through the app container.
$ docker exec -it test_app_1 mysql -hdb -pfoobar
ERROR 1130 (HY000): Host '192.168.144.3' is not allowed to connect to this MySQL server
Why is the container running the mysql server unable to receive requests from the test app container?
When I run docker ps, I see the following:
COMMAND PORTS
"rails server -b 0.0…" 0.0.0.0:3000->3000/tcp
"docker-entrypoint.s…" 0.0.0.0:3306->3306/tcp, 33060/tcp
By default when you start MySQL container, it creates automatically the root user which you are using it to access the same container test_db_1 based on environment variables passed, this root user is granted/allowed to connect from localhost only.
But if you want to access MySQL from the other container, you should create a user that is granted to connect to the database from a remote host (either username#% or username#container-name) - as each container has a different IP inside the docker default network,
Note: you can do that by logging into MySQL in the container, and create a user, in your case it will be something like: grant all on <your-database>#`%` to <yourusername>#`%` identified by '<password>'
portsoption expose the ports to your host machine but I don't think it does between the same between containers. Try using expose: 3306 on the db configuration. By the way, I'd like to point out that even though you are using depends_on option that doesn't guarantee your database will be up when you start your application only that the db container will be ready so keep that in mind.

Docker MySQL - can't connect from Spring Boot app to MySQL database

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.