docker-compose error when creating MySQL database container - mysql

I have the following docker-compose.yml and Dockerfile in my Spring Boot app but when I run docker-compose up -d, I get the following error:
"failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount967315702/Dockerfile: no such file or directory"
As I use Windows10, I think I need some tweaks related to the WORKDIR, etc. I have WSL where Docker installed). But even I tried different combinations, I have not managed to fix the problem.
Here is the docker-compose.yml and Dockerfile in my root project folder (springboot-backend):
docker-compose.yml: I am not sure about
version: "3.8"
services:
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- db:/var/lib/mysql
app:
depends_on:
- mysqldb
build: ./springboot-backend
restart: on-failure
env_file: ./.env
ports:
- $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
Dockerfile:
FROM maven:3.8.2-jdk-8
WORKDIR /springboot-backend
COPY . .
RUN mvn clean install
CMD mvn spring-boot:run
.env file:
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=******
MYSQLDB_DATABASE=employee_management_system
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306
SPRING_LOCAL_PORT=6868
SPRING_DOCKER_PORT=8080
Any idea?
Here is folder structure:

app:
depends_on:
- mysqldb
build: ./springboot-backend
Docker compose yaml try to build springboot-backend but there is no such directory in the same path of compose yaml. Just simply you can move docker-compose yaml to outside of springboot-backend project folder.

Related

running my spring boot project without the need for mysql database application when it is run with docker when it is pulled by someone else

Since my project is wanted to be used by anyone, I want it to be run with docker without the need to apply a mysql database on the person's computer or without any settings, but I could not do it, I need some help
.env file:
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=12345
MYSQLDB_DATABASE=familybudgetapp
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306
SPRING_LOCAL_PORT=8081
SPRING_DOCKER_PORT=8081
Dockerfile:
FROM openjdk:17-jdk-slim
EXPOSE 8081
ARG JAR_FILE=target/family-budget-app-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} family-budget-app.jar
ENTRYPOINT ["java","-jar","family-budget-app.jar"]
docker-compose.yml file:
version: "3.8"
services:
mysqldb:
image: mysql
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- ${MYSQLDB_LOCAL_PORT}:${MYSQLDB_DOCKER_PORT}
volumes:
- db:/var/lib/mysql
web_app:
depends_on:
- mysqldb
build:
context: .
dockerfile: DockerFile
restart: on-failure
env_file: ./.env
ports:
- ${SPRING_LOCAL_PORT}:${SPRING_DOCKER_PORT}
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?allowPublicKeyRetrieval=true&useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQLDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
Honestly, I couldn't figure out how to make a change.
I want the project to run by running these commands in the terminal:
./mvnw clean package && docker-compose build && docker-compose up
without needing db setup directly.

Can't connect to mysql docker container from springboot docker container

I want to connect from the sprinboot container to the mysql container but i get this error:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
I am using docker-compose.
This is my docker-compose file:
version: '3'
services:
trips-db:
image: "mysql"
container_name: "trips-db"
volumes:
- newDatabase:/home/sharedData
ports:
- 49152:3306
environment:
- MYSQL_USER=adham
- MYSQL_PASSWORD=123
- MYSQL_ROOT_PASSWORD=admin
trips-app:
build: ./Wasalny_BE
container_name: trips-app
ports:
- 8080:8080
links:
- trips-db
volumes:
newDatabase:
This is my spring boot docker file:
FROM openjdk:8-jdk-alpine
COPY target/wasalny-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
This is my application.properties file:
spring.datasource.url= jdbc:mysql://trips-db:49152/wslny?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username= root
spring.datasource.password= admin
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update
I run using the following command form the terminal:
docker-compose up -d
--force-recreate
Please create a docker network and attach it into the compose file.
Create a docker Network first like below.
docker network create ext-network
Add network settings into your yaml file as below and give a try
version: '3'
services:
trips-db:
image: "mysql"
container_name: "trips-db"
volumes:
- newDatabase:/home/sharedData
ports:
- 49152:3306
networks:
- ext-network
environment:
- MYSQL_USER=adham
- MYSQL_PASSWORD=123
- MYSQL_ROOT_PASSWORD=admin
trips-app:
build: ./Wasalny_BE
container_name: trips-app
ports:
- 8080:8080
networks:
- ext-network
links:
- trips-db
networks:
ext-network:
external: true
volumes:
newDatabase:

Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose

Trying to connect Springboot app dicom_viewer: Imagename: sample with Mysql: Imagename: pb_mysql running in docker container. Error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.
Application.properties file:
#MySQL database connection strings
spring.datasource.url=jdbc:mysql://pb_mysql:3306/test?autoReconnect=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=pb
spring.datasource.password=pb#123
#JPA property settings
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
Docker-compose file:
version: '2'
services:
pb_mysql:
container_name: pb_mysql
restart: always
image: mysql/mysql-server:latest
environment:
MYSQL_ROOT_PASSWORD: 'root123' # TODO: Change this
volumes:
- sql-datavolume:/var/lib/mysql
patient_browser:
container_name: patient_browser
restart: always
image: patient_browser
ports:
- 8080:8080
volumes:
- ./dicom_images:/usr/src/app/dicom_images
springboot-image:
container_name: dicom_viewer
restart: always
image: sample
ports:
- 8081:8080
volumes:
sql-datavolume:
networks:
f4:
driver: bridge
Dockerfile:
FROM openjdk:8-jdk-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./target/springboot-image-0.0.1-SNAPSHOT.jar /usr/src/app
COPY . /usr/src/app
EXPOSE 8080
ENTRYPOINT ["java","-jar","springboot-image-0.0.1-SNAPSHOT.jar"]
Database: test, table: pm_of_india
Docker images, docker ps -a
docker-compose up
Error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
There are two things to notice,
depends_on
You kept pb_mysql ahead of others in the yaml configuration file. This does not ensure the mysql-server to be up and running ahead of others.
You want to use depends_on, so the depended pb_mysql will be initiated first.
ports
You are using bridged network, so you need to map 3306 to 3306 for mysql-server.
version: '2'
services:
patient_browser:
container_name: patient_browser
restart: always
image: patient_browser
ports:
- 8080:8080
volumes:
- ./dicom_images:/usr/src/app/dicom_images
- 8081:8080
depends_on:
- pb_mysql
springboot-image:
container_name: dicom_viewer
restart: always
image: sample
ports:
- 8081:8080
depends_on: # Let mysql-server start first
- pb_mysql
pb_mysql:
container_name: pb_mysql
ports:
- "3306:3306" # Port mapping.
restart: always
image: mysql/mysql-server:latest
environment:
MYSQL_ROOT_PASSWORD: 'root123' # TODO: Change this
volumes:
- sql-datavolume:/var/lib/mysql
volumes:
sql-datavolume:
networks:
f4:
driver: bridge
I would avoid using restart: always unless I absolutely need it.
The rest looks good to me. Let me know if it is resolved.
You are not referencing your network.
Try adding it to your services i.e.:
services:
pb_mysql:
networks:
- f4
...

Docker Compose Mysql init file error (Windows, Dockertoolbox)

I´m trying to setup two docker container with docker-compose, using this yml:
version: '3.5'
services:
db:
image: mysql:5.7
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: abc
MYSQL_DATABASE: shopsystem
MYSQL_USER: shoppingsystem
MYSQL_PASSWORD: abc
volumes:
- ./test.sql:/docker-entrypoint-initdb.d/test.sql
- db_data:/var/lib/mysql
backend:
build:
context: ./nodeserver
dockerfile: Dockerfile
image: node-test
container_name: servernode-1
ports:
- 8084:8082
volumes:
db_data: {}
The test.sql is on the root level like the docker-compose.yml
When I start the docker-compose
docker-compose up
I get the following error:
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/test.sql
ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
I already read some threads about that problem, but I can not solved it.
I clean the volumes with the following command:
docker-compose down -v
I`m using Windows with dockertoolbox to run docker-compose.
Can someone help me?
ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
your should to map volume as directory
and put test.sql file in there
volumes:
- myvolumes:/docker-entrypoint-initdb.d
- db_data:/var/lib/mysql
or option 2 (if you want just a single file )
version: "3.2"
services:
app:
image: app:latest
volumes:
- type: bind
source: ./bla.yaml
target: /location/bla.yaml
ref : https://docs.docker.com/storage/bind-mounts/
ref : Mount single file from volume using docker-compose

Docker-compose up for a spring service and a mysql service works, but init.sql does is not loaded and Postman return empty data

I am working for the first time with Docker. In my job, they asked me to deploy a complete application using docker-compose and I am learning right now.
I have a docker-compose.yml that starts two services: one for spring and another one for MySQL.
The one for Spring uses a Dockerfile to build the image.
The one for MySQL uses the official MySQL image.
I don´t know how to load the init.sql to initialize and load data into the database of the MySQL container.
I´ve tried to use another Dockerfile in order to copy init.sql into the MySQL container but,
how do I tell the compose to use it when creating the image or running the container?
This is for a Windows 10 OS Desktop. Docker version 18.09.2. Docker-compose version 1.23.2
Docker Desktop
Docker-compose.yml
version: '3.7'
services:
mysql-docker-container:
image: mysql
container_name: mysql-docker-container
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=database
- MYSQL_USER=user
- MYSQL_PASSWORD=user
ports:
- 2012:3306
tty: true
spring-jpa-app:
build:
context: .
dockerfile: Dockerfile
container_name: spring-jpa-app
links:
- mysql-docker-container:mysql-docker-container
depends_on:
- mysql-docker-container
ports:
- 8087:8080
tty: true
restart: always
Dockerfile
FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD app-1.0.0.jar app-1.0.0.jar
ENTRYPOINT ["java","-jar","app-1.0.0.jar"]
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://mysql-docker-container:2012/database?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=user
spring.jpa.database=database
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
logging.file=log/log.log
logging.level.org.springframework=info
logging.level.orghibernate=info
Docker Desktop running
Opened Git Bash as administrator
docker-machine ls
NAME ACTIVE DRIVER STATE URL DOCKER
app * hyperv Running tcp://172.18.67.35:2376 v19.03.0
In the app folder, where is located /src
mvn clean install -DskipTests
BUILD SUCCESS
In the app folder where is located application.properties, app.jar, docker-compose.yml, Dockerfile and init.sql
docker-compose up --build
Postman
URL = http://172.18.67.35:8087
POST {{url}}/login
{
"Authorization": "gnioengrlnkwejnR",
"username": "Administrator",
"role": "ADMIN",
"id": "1"
}
GET {{url}}/listAllCenters
[]
Returns empty because init.sql is not being loaded
PD: Also, if I open a new git Bash as Administrator and do
docker ps -a
I do not get the containers running so I cannot know container's ID to enter the MySQL container.
you need to add a volume to copy the sql.init to your db continer:
version: '3.7'
services:
mysql-docker-container:
image: mysql
container_name: mysql-docker-container
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=database
- MYSQL_USER=user
- MYSQL_PASSWORD=user
ports:
- 2012:3306
volumes:
- /bath/to/file/on/host/init.sql:/docker-entrypoint-initdb.d/init.sql
tty: true
network_mode: "host"
spring-jpa-app:
build:
context: .
dockerfile: Dockerfile
container_name: spring-jpa-app
links:
- mysql-docker-container:mysql-docker-container
depends_on:
- mysql-docker-container
ports:
- 8087:8080
tty: true
restart: always
network_mode: "host"
see this and search for "Initializing a fresh instance"
on the other hand you need to use network to setup the connection between the containers - see the compose file above-
and you need to change you configs to
spring.datasource.url=jdbc:mysql://localhost:2012/database?autoReconnect=true&useSSL=false