MySQL and volumes in Docker - mysql

I have an application that runs in a Docker environment. I tried to configure docker volume in a docker-compose file for persisting data written in MySqln while executing docker compose down -v.
Here is my file :
version: "2"
services:
db:
container_name: db-server
image: mysql:5.8
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: p#$$w0rd
MYSQL_DATABASE: apps
volumes:
- /home/john/myapp/volumes-sql:/var/lib/mysql
networks:
app_net:
ipv4_address: 10.1.14.2
back-end-java:
container_name: web-server
build:
context: .
dockerfile: Dockerfile-back
restart: always
image: web-server:$VERSION
ports:
- 8022:8443
links:
- db
networks:
app_net:
ipv4_address: 10.1.14.3
networks:
app_network:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.1.14.0/24
gateway: 10.1.14.1
I recived this message after ran : docker logs db-server : chown: cannot read directory '/var/lib/mysql/': Operation not permitted
How can I resolve this problem ? and how can I use the volume when run docker run or docker up -d
Thank you
John.

Any relation between the permissions on /home/john/myapp/volumes-sql and '/var/lib/mysql/' ??
0 drwxrwxr-x. 2 john john 6 Sep 24 08:10 volumes-sql

Related

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 / phpmyadmin: Unable to connecto

I have the following docker-compose file.
version: "3.7"
services:
main:
container_name: buspack_main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${API_PORT}:${API_PORT}
command: bash -c "npm install --save && npm run start:dev"
env_file:
- .env
networks:
- webnet
depends_on:
- db
links:
- db
restart: always
db:
container_name: buspack_db
image: mysql:5.7.33
networks:
- webnet
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: "backendnest"
ports:
- 23306:3306
restart: always
volumes:
- ./db:/var/lib/mysql:rw
phpmyadmin:
container_name: buspack_phpmyadmin
image: phpmyadmin/phpmyadmin
depends_on:
- db
restart: always
ports:
- '8030:80'
environment:
PMA_HOST: buspack_db
MYSQL_ROOT_PASSWORD: 123456
networks:
webnet:
volumes:
db: {}
My problem Is that when I try to login with phpmyadmin I get the following error:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
And also
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
Is there something I'm missing?
networks:
- webnet
add webnet network to phpmyadmin service
There are more options to solve the connection.
Add phpmyadmin container to webnet network as Def Soudani suggests. The YML example provided overrides the default network. Since the phpMyAdmin is lacking the network config, it won't be on the webnet docker network by default.
Remove all networks related config from the YML. Since compose already creates a default network (docs) for the containers within one YML file.
Use the PMA_ARBITRARY=1 (docs )which allows you to enter a database server hostname on login form.
Example of my YML setup with custom bridge networking:
version: '3.8'
services:
mysql:
image: mysql:5.7
container_name: myapp-mysql-db
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./database/dbdata:/var/lib/mysql
networks:
- myapp-network
phpmyadmin:
image: phpmyadmin:latest
container_name: phpmyadmin
ports:
# 8080 is the host port and 80 is the docker port
- 8080:80
environment:
PMA_HOST: mysql
UPLOAD_LIMIT: 20M
MYSQL_USERNAME: ${DB_USERNAME}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
restart: unless-stopped
networks:
# define your network where all containers are connected to each other
- myapp-network
networks:
myapp-network:
driver: bridge

MySQL is not working when Docker images started

I try to run local docker environment for WhatsApp api. But when I run docker-compose it writes me that MySQL is not up yet - sleeping. I don't have MySql installed on my PC because I think that docker works smth like VM, so it will download mysql and run it. What can be a problem?
docker-compose.yml
version: '3'
volumes:
whatsappMedia:
driver: local
mysqlData:
driver: local
services:
db:
image: mysql:5.7.35
restart: always
environment:
MYSQL_ROOT_PASSWORD: testpass
MYSQL_USER: testuser
MYSQL_PASSWORD: testpass
expose:
- "33060"
ports:
- "33060:3306"
volumes:
- mysqlData:/var/lib/mysql
network_mode: bridge
cap_drop:
- MKNOD
wacore:
image: docker.whatsapp.biz/coreapp:v${WA_API_VERSION:-2.35.5?Run docker-compose with env var WA_API_VERSION (ex. WA_API_VERSION=2.37.1 docker-compose <command> <options>)}
command: ["/opt/whatsapp/bin/wait_on_mysql.sh", "/opt/whatsapp/bin/launch_within_docker.sh"]
volumes:
- whatsappMedia:/usr/local/wamedia
env_file:
- db.env
environment:
# This is the version of the docker templates being used to run WhatsApp Business API
WA_RUNNING_ENV_VERSION: v2.2.3
ORCHESTRATION: DOCKER-COMPOSE
depends_on:
- "db"
network_mode: bridge
links:
- db
cap_drop:
- MKNOD
waweb:
image: docker.whatsapp.biz/web:v${WA_API_VERSION:-2.35.4?Run docker-compose with env var WA_API_VERSION (ex. WA_API_VERSION=2.37.1 docker-compose <command> <options>)}
command: ["/opt/whatsapp/bin/wait_on_mysql.sh", "/opt/whatsapp/bin/launch_within_docker.sh"]
ports:
- "9090:443"
volumes:
- whatsappMedia:/usr/local/wamedia
env_file:
- db.env
environment:
WACORE_HOSTNAME: wacore
# This is the version of the docker templates being used to run WhatsApp Business API
WA_RUNNING_ENV_VERSION: v2.2.3
ORCHESTRATION: DOCKER-COMPOSE
depends_on:
- "db"
- "wacore"
links:
- db
- wacore
network_mode: bridge
cap_drop:
- MKNOD
db.env
WA_DB_ENGINE=MYSQL
WA_DB_HOSTNAME=127.0.0.1
WA_DB_PORT=3306
WA_DB_USERNAME=root
WA_DB_PASSWORD=testpass
WA_DB_CONNECTION_IDLE_TIMEOUT=180000
I edited the docker-compose file, I changed the version from 3 to 3.9, added a default network and deleted network_mode: bridge.
version: '3.9'
volumes:
whatsappMedia:
driver: local
mysqlData:
driver: local
networks:
default:
driver: bridge
services:
db:
image: mysql:5.7.35
restart: always
environment:
MYSQL_ROOT_PASSWORD: testpass
MYSQL_USER: testuser
MYSQL_PASSWORD: testpass
expose:
- "33060"
ports:
- "33060:3306"
volumes:
- mysqlData:/var/lib/mysql
networks:
- default
cap_drop:
- MKNOD
wacore:
image: docker.whatsapp.biz/coreapp:v${WA_API_VERSION:?Run docker-compose with env var WA_API_VERSION (ex. WA_API_VERSION=2.39.2 docker-compose <command> <options>)}
command: ["/opt/whatsapp/bin/wait_on_mysql.sh", "/opt/whatsapp/bin/launch_within_docker.sh"]
volumes:
- whatsappMedia:/usr/local/wamedia
env_file:
- db.env
environment:
# This is the version of the docker templates being used to run WhatsApp Business API
WA_RUNNING_ENV_VERSION: v2.2.3
ORCHESTRATION: DOCKER-COMPOSE
depends_on:
- db
cap_drop:
- MKNOD
waweb:
image: docker.whatsapp.biz/web:v${WA_API_VERSION:?Run docker-compose with env var WA_API_VERSION (ex. WA_API_VERSION=2.39.2 docker-compose <command> <options>)}
command: ["/opt/whatsapp/bin/wait_on_mysql.sh", "/opt/whatsapp/bin/launch_within_docker.sh"]
ports:
- "9090:443"
volumes:
- whatsappMedia:/usr/local/wamedia
env_file:
- db.env
environment:
WACORE_HOSTNAME: wacore
# This is the version of the docker templates being used to run WhatsApp Business API
WA_RUNNING_ENV_VERSION: v2.2.3
ORCHESTRATION: DOCKER-COMPOSE
depends_on:
- db
- wacore
cap_drop:
- MKNOD

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