How to execute sql file in docker? - mysql

I have lamp project and try to run it in the Docker compose. The app is working but doesn't have connection with mysql. The reason: sql file does not execute and the database is not created. When I go into the container and run the sql file manualy, then everything works good.
version: '3.7'
services:
db:
build:
context: ./database
dockerfile: Dockerfile
container_name: data36
environment:
- MYSQL_ROOT_PASSWORD=password123
- MYSQL_DATABASE=php_mysql_crud
volumes:
- ./database/script.sql:/docker-entrypoint-initdb.d/script.sql
- mysqlvol:/var/lib/mysql
restart: always
ports:
- 3306:3306
networks:
- app
php:
container_name: php8
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html/
ports:
- 8000:80
networks:
- app
depends_on:
- db
volumes:
database:
mysqlvol:
networks:
app:
driver: bridge
Dockerfile:
FROM mysql:5.7.17
ENV MYSQL_ROOT_PASSWORD password123
COPY script.sql /docker-entrypoint-initdb.d/
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/
EXPOSE 3306
CMD ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
How can I execute script.sql in container ???

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:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db'

I have an error in logs of my docker-compose
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db'
Full error text https://controlc.com/3e1bc743
On the server docker-compose works without this error.
It seems that mysql isn't running on port 3306
sudo netstat -tunpl | grep 3306 # did not show anything
Also didn't see any tasks on this port
sudo lsof -i tcp:3306
Could you please recommend direction how to solve the proble?
db/Dockerfile
FROM mysql
RUN mkdir /var/log/mysql && chmod 777 /var/log/mysql
docker-compose.yaml
version: '3'
services:
sender:
container_name: sender
build:
context: ./sender
dockerfile: Dockerfile
image: sender:latest
#command: tail -f /dev/null
command: python run.py
#command: python run_output_to_files.py
depends_on:
- stream_server
- db
env_file:
- sender/variables.env
- database.env
volumes:
- ./sender/logs/:/logs/:rw
ports:
- 8083:8083
- 554:554
- "1241-1244:1241-1244/udp"
receiver:
container_name: receiver
ports:
- 5004:5000
image: receiver:latest
build:
context: ./receiver
dockerfile: Dockerfile
command: flask run --host=0.0.0.0
depends_on:
- stream_server
- sender
- db
environment:
- STREAM_SERVER_URL=stream_server
- DELAY_MSEC=750
stream_server:
container_name: stream_server
ports:
- 1935:1935
- 1985:1985
- 8080:8080
image: ossrs/srs:3
#command: ./objs/srs -c conf/realtime.conf
db:
container_name: db
image: db:latest
build:
context: ./db
dockerfile: Dockerfile
restart: on-failure
ports:
- "3306:3306"
volumes:
- ./db/mysql/:/var/lib/mysql/
- ./db/conf.d/:/etc/mysql/conf.d/
#- ./db/logs/:/var/log/mysql/:rw
- ./db/startup_scripts/:/docker-entrypoint-initdb.d/
env_file:
- database.env
networks:
default:
external:
name: buildings_default

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
...

Database created but no tables in MySQL Docker

I have a Application run into a tomcat-embededd container and linked with a MySQL database
I have running my container by
docker-compose up --build -d
, the two container is up, i can log in to the application
But , when i put
docker exec -it myapp-mysql bash
and i choise my database with
use dbname;
, i don't see any table into my database
This is my docker-compose
version: "2"
services:
db:
container_name: myapp-mysql
image: mysql:latest
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: XXXXXXXX
MYSQL_DATABASE: XXXXXXX
networks:
app_net:
ipv4_address: A.B.C.D
back-end-java:
container_name: myapp-tomcat
build:
context: .
dockerfile: Dockerfile
restart: always
image: application
ports:
- 8008:8443
links:
- db
networks:
app_net:
ipv4_address: A.B.C.D
networks:
app_net:
driver: bridge
ipam:
driver: default
config:
- subnet: A.B.C.D/24
gateway: A.B.C.D
and this is the Dockerfile
FROM tomcat:8.0.33-jre8
ADD myapp.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
Thank YOU
You are using --build which means you force the recreation of mysql container and since you are not mounting your data volume to the host machine, your data will be wiped out each time you run this command. you can avoid that by mounting mysql data volume to your docker-compose as the following:
version: "2"
services:
db:
container_name: myapp-mysql
image: mysql:latest
restart: always
ports:
- 3306:3306
volumes:
- "./data/mariadb:/var/lib/mysql:rw"
environment:
MYSQL_ROOT_PASSWORD: XXXXXXXX
MYSQL_DATABASE: XXXXXXX
networks:
app_net:
ipv4_address: A.B.C.D