Docker database container does not get data in existing db - mysql

I have created a docker container for my existing local Mysql db. Although container creates tables in my local db correctly, the data in it is not passed at all and it returns an empty set constantly. Should I insert my data once again in container or is there a way to synchronize my local db and db in container? Here is the docker.complose.yml:
version: '3'
services:
mysql_st:
image: mysql:latest
container_name: mysql_st
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: components
MYSQL_PASSWORD: 123456
ports:
- "3308:3306"
app-mysql:
image: app-mysql:latest
container_name: app-mysql
ports:
- "9090:9090"
depends_on:
- mysql_st
volumes:
- ./target/app-mysql.jar:/app-mysql.jar
command: ["java", "-jar", "app-mysql.jar"]
build:
context: ./
dockerfile: Dockerfile
links:
- "mysql_st"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql_st:3306/components?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Dockerfile:
FROM openjdk:latest
ADD target/app-mysql.jar app-mysql.jar
EXPOSE 9090
ENTRYPOINT ["java","-jar","app-mysql.jar"]
VOLUME /var/lib/mysql
Application properties:
spring.datasource.username= root
spring.datasource.password= 123456
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mysql://mysql_st:3306/components?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
server.port = 9090
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB

why a mysql docker container if you already have one mysql service on your local machine (host)?
I think the mysql service part should be removed from you composefile
and then you need to connect from the container (app-mysql) to the database running on the local machine
have a look at this:
From inside of a Docker container, how do I connect to the localhost of the machine?

Related

Run Mysql in a docker container with Spring Boot

I am trying to change my embedded in-memory H2 database to a MySQL instance using docker.
The application runs fine in-memory using docker, but I can't manage to change to MySQL.
I did some research, and managed to write this docker-compose.yml:
version: '3.3'
services:
#service 1: definition of mysql database
db:
image: mysql:latest
container_name: mysql-db2
environment:
- MYSQL_ROOT_PASSWORD=sa
- MYSQL_USER=password
ports:
- "3306:3306"
#service 2: definition of phpMyAdmin
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: my-php-myadmin
ports:
- "8082:80"
restart: always
depends_on:
- db
environment:
SPRING_DATASOURCE_USERNAME: sa
SPRING_DATASOURCE_PASSWORD: password
#service 3: definition of the spring-boot app
customerservice:
image: mysqldockerapp
container_name: mysql-docker-app
build:
context: .
dockerfile: Dockerfile
ports:
- "8084:8084"
depends_on:
- db
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db2:3306/customer?createDatabaseIfNotExist=true
SPRING_DATASOURCE_USERNAME: sa
SPRING_DATASOURCE_PASSWORD: password
And these are my application.properties
server.port=8084
spring.application.name=customer-service
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://mysql-db2:3306/customer?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=never
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.SQL=DEBUG
And my dockerfile
FROM openjdk:16-alpine3.13
MAINTAINER krzysztof.com
EXPOSE 8084
COPY target/myapp.jar myapp.jar
ENTRYPOINT ["java","-jar","/myapp.jar"]
The containers build correctly, but the app exits and I get:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
But the MySQL container is running, so I would appreciate any tips where should I look for possible reasons.

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.

Laravel dockerized app taking DB_HOST as SERVER_ADDR instead of container

I have a laravel app and the database containers are configured in this way:
db:
image: "mysql:5.7"
command: ["--sql- mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"]
environment:
MYSQL_DATABASE: mydb
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
networks:
- simple_net
app:
build:
context: .
dockerfile: ./setup/Dockerfile
restart: unless-stopped
tty: true
ports:
- "${APP_PORT}:80"
working_dir: /var/www/
volumes:
- ./:/var/www/
environment:
DB_CONNECTION: mysql
DB_HOST: db
DB_DATABASE: mydb
DB_USERNAME: root
# SERVER_ADDR: localhost
When I tried to start the app with docker-compose up it throws an exception that says SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.23.0.6' (using password: NO). The IP refers to the environmental variable SERVER_ADDR, I tried to set it up with the values localhost and '' (empty) but nothing has worked so far.
You should set the .env's DB_HOST to the name of your container, in your case db.
DB_HOST=db
I do not believe injecting environment variables replaces filling the .env.
Also, if you want to Dockerize your Laravel applications, several options already exist and work out of the box:
Laradock
Docker Compose Laravel
Laradose (which I maintain)
You don't need DB_CONNECTION: mysql there.
Instead inside your .env file in lavel you will use this like:
DB_CONNECTION: mysql
DB_HOST: db
DB_PORT: 3306
DB_DATABASE: mydb
DB_USERNAME: root
DB_PASSWORD: password
Your app container does not need this environment, that's your server running. You try to configure the environment of the db container inside server container which will run your app.
Change your docker-compose to:
db:
image: "mysql:5.7"
command: ["--sql- mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"]
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: password
MYSQL_DATBASE: mydb
MYSQL_USER: root
MYSQL_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
networks:
- simple_net
app:
build:
context: .
dockerfile: ./setup/Dockerfile
restart: unless-stopped
tty: true
ports:
- "${APP_PORT}:80"
working_dir: /var/www/
volumes:
- ./:/var/www/
networks:
- simple_net
Bridge your containers under your network so they can interact with each other.
Run docker-compose up --build and then access your laravel container and run inside the container php artisan optimize to configure cache for routes,env variables, files etc etc.
docker exec -it db mysql -u root -p password
Based on your comments you have issues to access the container and the reason is you don't use credentials for it and mysql tries to use the default ones. Above command defines the right user and password, you will also be asked to fill the password.
Also mysql has more parameters to define the host etc etc you can have a look further for that.

Spring Boot + MySQL + Docker Compose - Cannot make Spring Boot connect to MySQL

I've been trying to set up a connection between a backend (runs on Spring Boot) container and a pre-built MySQL container. However, I cannot get it to connect. My docker compose file is:
version: '3.7'
services:
test-mysql:
image: mysql
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_DATABASE: testdb
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
backend:
depends_on:
- test-mysql
build:
context: backend
dockerfile: Dockerfile
ports:
- "8080:8080"
restart: always
volumes:
db_data: {}
my application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mysql://test-mysql:3306/testdb?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=test
spring.datasource.password=test
When I use docker-compose up, Spring Boot is not able to recognize the container name test-mysql. It throws: java.net.UnknownHostException
When I change it to an IP, it says connection refused. I have been looking everywhere and couldn't come with a fix. I hope anyone can help me out. Thank you!
You have to mention the backend mysql properties in the composer file like below,
backend:
depends_on:
- test-mysql
build:
context: backend
dockerfile: Dockerfile
ports:
- "8080:8080"
restart: always
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://test-
mysql:3306/testdbautoReconnect=true&failOverReadOnly=false&maxReconnects=10
SPRING_DATASOURCE_USERNAME: test
SPRING_DATASOURCE_PASSWORD: test
links:
- test-mysql:test-mysql
If this wouldn't work try to create a common docker network and add it to your composer file like below,
backend:
depends_on:
- test-mysql
build:
context: backend
dockerfile: Dockerfile
ports:
- "8080:8080"
restart: always
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://test-
mysql:3306/testdbautoReconnect=true&failOverReadOnly=false&maxReconnects=10
SPRING_DATASOURCE_USERNAME: test
SPRING_DATASOURCE_PASSWORD: test
networks:
-common-network
test-mysql:
image: mysql
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_DATABASE: testdb
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
networks:
-common-network
#Docker Networks
networks:
common-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
You can define a common network on which both the application server and the database can connect. Please check the file (docker-compose.yml) below where I have defined a common network: backend
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3.7'
# Define services
services:
# App backend service
app-server:
# Configuration for building the docker image for the backend service
build:
context: . # Use an image built from the specified dockerfile in the `springboot-app-server` directory.
dockerfile: Dockerfile
ports:
- "8080:8080" # Forward the exposed port 4000 on the container to port 4000 on the host machine
restart: always
depends_on:
- db # This service depends on mysql. Start that first.
environment: # Pass environment variables to the service
SPRING_DATASOURCE_URL: jdbc:mysql://test-mysql:3306/testdb?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
SPRING_DATASOURCE_USERNAME: test
SPRING_DATASOURCE_PASSWORD: test
networks: # Networks to join (Services on the same network can communicate with each other using their name)
- backend
# Database Service (Mysql)
db:
image: mysql:5.7
ports:
- "3306:3306"
restart: always
environment:
MYSQL_DATABASE: testdb
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
volumes:
- db-data:/var/lib/mysql
networks:
- backend
# Volumes
volumes:
db-data:
# Networks to be created to facilitate communication between containers
networks:
backend:
I have written a blog and a simple working Spring Boot MySQL application on GitHub which tells about using Docker Compose. Please check: http://softwaredevelopercentral.blogspot.com/2020/10/spring-boot-mysql-docker-compose-example.html
If you would like to use this test-mysql in your spring config
spring.datasource.url=jdbc:mysql://test-mysql:3306/testdb?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
Then add the hostname attribute at service test-mysql
version: '3.7'
services:
test-mysql:
image: mysql
hostname: test-mysql
...
I hope this has been already solved but in case it hasn't yet, the problem lies in mysql docker container lagging behind the start up.
Another problem is that you might need to build the jar file and then copy it the container. That's a big problem because when you build the jar file, the database with db as hostname is unavailable. So when you are building the jar file, skip the test.
This is bash script i created but you can run command one by one:
#!/bin/bash
cd storage-service
rm -rf target/
mvn clean compile package -Dmaven.test.skip=true
cd ..
docker-compose up
In case you want to initialize db in the container. That file is in the folder env where i have a file database.env
-- create the databases
CREATE DATABASE IF NOT EXISTS model_storage;
-- create the users for each database
CREATE USER 'arsene'#'localhost' IDENTIFIED BY 'arsene';
GRANT CREATE, ALTER, INDEX, LOCK TABLES, REFERENCES, UPDATE, DELETE, DROP, SELECT, INSERT ON `model_storage`.* TO 'arsene'#'localhost';
FLUSH PRIVILEGES;
The backend service Dockerfile looks like this:
FROM adoptopenjdk/openjdk11
COPY target/*.jar storage.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /storage.jar" ]
EXPOSE 8089
The database env file looks like this:
MYSQL_ROOT_PASSWORD=arsene
MYSQL_DATABASE=model_storage
MYSQL_USER=arsene
MYSQL_PASSWORD=arsene
DATABASE_HOST=model_storage
DATABASE_USER=arsene
DATABASE_PASSWORD=arsene
DATABASE_NAME=model_storage
DATABASE_PORT=3306
In case you intend to pass JAVA_OPTS env in the image. These can be used later as seen in docker-compose.yml below
Your backend (the service that depends on the mysql db) needs to restart until the docker-compose is able to resolve the the container name of mysql, in my case its name is db. And don't forget to include datasource connection properties in docker-compose backend service image as i did below. I am not an expert in spring boot and neither in docker but for now it works!
Below is the way mine is structured:
I am using docker version: "3.8"
Storage service
storage-service:
container_name: storage-service
restart: always
build:
context: storage-service
image: "service_storage_image"
depends_on:
- db
ports:
- "8089:8089"
links:
- db
env_file:
- env/database.env
environment:
WAIT_HOSTS: db:3306
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/model_storage?allowPublicKeyRetrieval=true&useSSL=false
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: arsene
healthcheck:
test: "/usr/bin/mysql --user=arsene --password=arsene--execute \"SHOW DATABASES;\""
interval: 2s
timeout: 20s
retries: 10
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://eureka-registry-server:7070/eureka
-DZIPKIN_SERVER=http://zipkin:9411/
networks:
- private-network-mms
My db in docker-compose is structured this way:
Mysql database
db:
hostname: db
container_name: db
image: "mysql:latest"
env_file:
- env/database.env
volumes:
- type: bind
source: ./env/setup.sql
target: /docker-entrypoint-initdb.d/setup.sql
- db_volume:/var/lib/mysql
ports:
- 3307:3306
networks:
- private-network-mms