Can't connect to MySQL container from other container - mysql

I'm trying to connect my FASTAPI app container to a MySQL database container using the docker-compose file. In the Docker documentation it says that docker creates a default network for both containers. However, I would like to use a pre-existing network that I've created(app-net).
This is my docker-compose file:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local
this is the output I get when i run docker inspect mysql -f "{{json .NetworkSettings.Networks }}":
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["mysql","5e998f9fb646"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}
However, when I run each container individually using CMD with the --network app-net the output is different:
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["46157e588c87"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"6a6922a9a6ea8f9d113447decbbb927cb93ddffd3b9563ee882fa2e44970cde5","Gateway":"172.20.0.1","IPAddress":"172.20.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:14:00:02","DriverOpts":null}}
In my app code in order to connect the mysql server, I specified the container name as the hostname since they are supposed to share the same network. But, as I mentioned it seems both containers can't talk to each other.
I'm pretty sure that is the reason I can't connect the database through my app and get that error when I run:
docker-compose -f docker-compose.yml up
I get this error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2005, "Unknown MySQL server host 'mysql' (0)")
What am I missing?

If the error you're getting is specifically "unknown host" or something similar, this can happen if your application container starts before the database container exists. You can work around this situation by telling Compose about the dependency:
version: '3.8'
services:
mysql: { ... }
app:
depends_on:
- mysql
This has two key effects. If you try to start only the application container docker-compose up app, it will also start the mysql container, following the depends_on: chain. When Compose does start containers, it at least creates the mysql container before creating the app container.
Note that this doesn't guarantee the database will actually be running by the time your application starts up. If you do encounter this, you will get a different error like "connection refused". You can work around this by embedding a script in your image that waits for the database to be available, or by using a version 2 Compose file with health-check support; see Docker Compose wait for container X before starting Y for more details on this specific problem.

You could add the key networks to both containers, this way:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
networks:
- app-net
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
- app-net
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local

Related

How to set 'spring.datasource.url' inside a Docker Container

I created a Spring Boot application which uses a MySQL database. I use a docker-compose to launch the database.
services:
adminer:
image: adminer
restart: always
ports:
- 8888:8080
db:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'example' # TODO: Change this
volumes:
- "./config/my.conf:/etc/mysql/conf.d/config-file.cnf"
- "./data:/var/lib/mysql:rw"
The Spring Boot Application (Backend) currently does not use Docker, I run it inside Eclipse. Before launching the Backend I have to grep the Docker Container for IPAddress:
docker inspect mysql_ex_db_1 | grep 'IPAddress'
which results something like this (this exact address changes time-to time)
"IPAddress": "",
"IPAddress": "172.21.0.2",
Then I take this value and I set spring.datasource.url inside Eclipse in the file Application.properties with it.
spring.datasource.url=jdbc:mysql://172.21.0.2:3306/employee_management_system?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true
After this I can launch the Backend in Eclipse the Connection to database is there, everything works.
Now I want to move the launching of Backend from Eclipse to the same docker-compose file I use to launch the database. Therefore I built an image, and appended the docker-compose file:
version: '3.1'
services:
adminer:
image: adminer
restart: always
ports:
- 8888:8080
db:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'example' # TODO: Change this
volumes:
- "./config/my.conf:/etc/mysql/conf.d/config-file.cnf"
- "./data:/var/lib/mysql:rw"
backend:
image: backend:latest
restart: always
ports:
- 8090:8080
In this case how can I configure the IPAddress in spring.datasource.url?
The exact IPAddress changes whenever I re-launch the mysql containers.
spring.datasource.url=jdbc:mysql://172.21.0.2:3306/employee_management_system?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true
So what should I write instead of '172.21.0.2' ?
I tried localhost here but it
does not seem to work.
First of all, you can set environment variables like spring.datasource.url outside of your docker image. This allows you to dynamically set these variables according to your deployment needs (like connecting to a dev or prod database).
All docker containers running from your docker-compose file run in the same virtual network and their service names correspond to their hostnames within this network. When you want to access your database from your dockerized spring backend the hostname and port will be db:3306. You can overwrite spring.datasource.url in your docker-compose file by introducing an environment variable like:
version: '3.1'
services:
adminer:
image: adminer
restart: always
ports:
- 8888:8080
db:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'example' # TODO: Change this
volumes:
- "./config/my.conf:/etc/mysql/conf.d/config-file.cnf"
- "./data:/var/lib/mysql:rw"
backend:
image: backend:latest
restart: always
ports:
- 8090:8080
environment:
spring.datasource.url: "jdbc:mysql://db:3306/employee_management_system?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true"
In your Spring Boot app, your spring.datasource.url must be like this:
spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:5432/my_db
(here I am connecting to a postgres db). Then you set the variable in your docker compose:
...
environment:
DATABASE_HOST:container_name
you can also test outside a docker-compose, by command line like this:
docker run -it -p 8080:8080 -e "JAVA_OPTS=-Xmx128m" --network=my_network -e DATABASE_HOST=postgres_container_name --name myapp myregistry/myimage:version
Add this env variable to your backend in docker-compose:
backend:
...
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/employee_management_system?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = ms_api_shop

I am using Lumen with Docker to create simple API for authentication. After installing LumenPassport, I cannot migrate the database. I can easily connect to the MySQL db with Dbeaver.
I have already created one Lumen Docker project for the same purpose, it is the second. The first one worked without a problem. Moreover, I have checked the MySQL databases, ms_api_shop was there
Errors:
Here is my docker-compose
services:
nginx:
build:
context: .
dockerfile: docker/Nginx.Dockerfile
image: nginx
ports:
- 8092:80
depends_on:
- fpm
volumes:
- ./:/var/www/lumen-docker
links:
- mysql
fpm:
build:
context: .
dockerfile: docker/fpm.Dockerfile
volumes:
- ./:/var/www/lumen-docker
depends_on:
- mysql
links:
- mysql
mysql:
image: mysql:5.7
ports:
- 33006:3306
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=ms_api_shop
- MYSQL_ROOT_USER=
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
And env:
DB_HOST=mysql
DB_PORT=33006
DB_DATABASE=ms_api_shop
DB_USERNAME=
DB_PASSWORD=
In your docker-file, you are binding the 33006 container port to the 3306 of the host port. In case you want to access the MySQL, you should use 3306 not 33006 as you did in your .env
I have a Laravel app running in Docker and a part of my docker config looks like:
But I personally feel that they should be within a docker network, look at the last two lines of the code in my docker-compose.yml below:
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql:/var/lib/mysql
networks:
- laravel
According to my knowledge, docker containers can communicate with each other when they are on the same network. You seem to have not connected these two docker in docker-compose yet. To get network information from a container, you can use the below command.
$ docker inspect --format='{{json .NetworkSettings.Networks}}' <docker_container_name>
In case you need to connect these two containers, follow these steps:
First, you create a network
$ docker network create my-net
Second, you connect container to the network.
$ docker network connect my-net <docker_container_name>
Don't forget to connect these two containers to the network.

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

How to fix 'ECONNREFUSED' error in my nodejs app using docker?

I am using Docker container for my node app which is a REST API Server. It use multiple backend (currently mongoDB and mysql). My mongoclient is connecting to the port defined in my docker-compose.yml but my mysql is not configuring to it, throwing ECONNREFUSED error.
Also i like to add In my localhost I have both mongodb and mysql installed on my system.
Below is the code of my Dockerfile and docker-compose.yml
FROM node:8
RUN mkdir -p /home/sharad/eapp
WORKDIR /home/sharad/eapp
COPY package.json /home/sharad/eapp/
RUN npm install
COPY . /home/sharad/eapp
EXPOSE 8016
CMD [ "npm","start" ]
version: '3.1'
services:
server:
image: sharadm20/eyantra-app
restart: always
ports:
- 8016:8016
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
links:
- mongo
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true'
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
adminer:
image: adminer
restart: always
ports:
- 8082:8080
And below is the error which I am encountering:
Unable to connect to the mysql database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 }
It worked by changing the Hostname in mysql config to the name similar to docker image name( in my case mysql is called db) so the hostname+port would look like db:3306.
Thanks to Marc Sances for pointing it out.

docker-compose run two instance of mysql

I want to run two instances of mysql using docker-compose.
I'm running MySQL in a Docker container and I have another Docker container running a python script to access the MySQL database.
One works fine on port 3306. In order to get two working, I thought I would just run the other one on a different port. But when I change it to a different port (e.g. 6603), but when I do, I get the below error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql:6603' (111 Connection refused)
I have read every question on s.o. I can find that seems relevant but none of the solutions work. I feel certain the fix will involve changing a line or two of configuration but I've spent many hours on this so far so any help would be greatly appreciated.
The docker-compose script is below (works fine if 6603 is replaced with 3306).
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
links:
- mysql:mysql
volumes:
- ../py:/app
tty: true
mysql:
image: mysql
expose:
- "6603"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
And it is being accessed like this:
cnx = mysql.connector.connect(user='root', password='password',
host='mysql',
port="6603",
database='project')
Try to specify another port for MySQL by modifying its my.cnf file.
Have eventually found a couple of ways that work. The neatest one is for each app to create a network and connect the containers to it.
If each app uses a different network then mysql can run on 3306 on that Docker network and can be accessed on mysql://3306 from app1 and mysql2://3306 from app2. (Assuming you name you give the mysql service for app 2 is mysql2).
The Docker file with the new lines is below:
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
volumes:
- ../py:/app
tty: true
networks:
-net
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net
networks:
net:
driver: bridge
The Docker file for the second app is identical except the names are different (I put a 2 after each for simplicity).
server2:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8081:8080"
volumes:
- ../py:/app
tty: true
networks:
-net2
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net2
networks:
net2:
driver: bridge