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
Related
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 ???
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
I have setup docker-compose to run phpmyadmin and mysql. Here is my docker-compose.yml
version: "3.9"
networks:
local_web_network:
driver: bridge
volumes:
mysql_data:
driver: local
services:
mysql:
image: mysql:8.0
container_name: local_mysql
restart: always
tty: true
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: local_master
MYSQL_ROOT_PASSWORD: secret
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
networks:
- local_web_network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: local_phpmyadmin
restart: always
tty: true
ports:
- "8083:80"
environment:
PMA_HOST: local_mysql
PMA_ABSOLUTE_URI: http://db.test
MYSQL_ROOT_PASSWORD: secret
networks:
- local_web_network
extra_hosts:
- 'db.test:127.0.0.1'
I have configured the windows hosts file with the below settings
127.0.0.1 db.test
I have successfully run the docker containers without any errors. I have tried accessing the http://127.0.0.1:8083 or http://db.test:8083 it works and displays the phpmyadmin login page.
But the problem here is, i am unable to access the page without specifying the port i.e http://db.test. How do i get this working without specifying the port?
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
...
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