DB in Docker container keeps getting deleted - mysql

I'm new to docekr and I'm setting up a Codeigniter docker image on my local machine
bitnami/codeigniter:3
Everything works fine but the mysql container looses it's DB and all the data everytime I perform
sudo docker-compose down
This is docker-compose file
version: '3.3'
services:
myapp:
image: docker.io/bitnami/codeigniter:3
container_name: app-backend
ports:
- '8000:8000'
volumes:
- '.:/app'
depends_on:
- mariadb
mariadb:
image: docker.io/bitnami/mariadb:10.3
container_name: app-marriadb
volumes:
- app_dbdata:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_db
ports:
- '3307:3306'
environment:
- ALLOW_EMPTY_PASSWORD=yes
volumes:
app_dbdata:
I tried multiple possiblities but not sure where I'm going wrong. My OS is Ubuntu 18.04

The bitnami/mariadb image uses a different path inside the container for its database storage. Quoting the "Persisting your database" section of its Docker Hub page:
For persistence you should mount a directory at the /bitnami/mariadb path. If the mounted directory is empty, it will be initialized on the first run.
So in your Compose setup, specify that path as the mount directory:
version: '3.8'
services:
mariadb:
volumes:
- app_dbdata:/bitnami/mariadb # <-- not /var/lib/mysql
volumes:
app_dbdata: # unchanged

Related

My mysql container in Docker, via the feature I gave it from the dockerfile to restart itself, keeps turning itself on and off

My mysql container in docker, via the feature I gave it from the dockerfile to restart itself, keeps turning itself on and off after I attempted the docker-compose up --build command.
My Dockerfile contains several containers, including an apache, a mysql with a volume to save the data of a database and a php.
I ran into this problem as I was carrying out various tests since I had just created the volume and I wanted to see if the database was not losing the data inside it.
After various commands of docker-compose down and docker-compose up it happened that the mysql container did not work anymore, not even after other commands docker-compose down and docker-compose build --no-cache.
below i added my docker.compose.yml
all versions of the images and data for the database are taken from the various dockerfiles
version: "3.2"
services:
php:
build:
context: './php/'
args:
PHP_VERSION: ${PHP_VERSION}
networks:
- backend
volumes:
- ${PROJECT_ROOT}/:/var/www/html/
container_name: php
apache:
build:
context: './apache/'
args:
APACHE_VERSION: ${APACHE_VERSION}
depends_on:
- php
- mysql
networks:
- frontend
- backend
ports:
- "80:80"
volumes:
- ${PROJECT_ROOT}/:/var/www/html/
container_name: apache
mysql:
image: mysql:${MYSQL_VERSION:-latest}
restart: always
ports:
- "3306:3306"
volumes:
- data:/var/lib/mysql
networks:
- backend
environment:
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
MYSQL_DATABASE: "${DB_NAME}"
MYSQL_USER: "${DB_USERNAME}"
MYSQL_PASSWORD: "${DB_PASSWORD}"
container_name: mysql networks:
frontend:
backend:
volumes:
data:
Anyone can describe me what I can do in this case in order not to lose the progress made so far within the database since doing a little research they tell me that the volume and the image of mysql may have been corrupted?

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

How to organise mysql volumes for different projects with different mysqld versions in docker-compose file?

Usually, I create a folder with a name 'docker' inside my project directory so I can use different configurations for different projects. I put all docker files in this folder.
I've created this docker-compose file to work with a project that uses MySQL v5.5.:
version: "3"
services:
mysql:
image: mysql:5.5
ports:
- 13307:3306
environment:
- MYSQL_ROOT_PASSWORD=123
volumes:
- mysqldata:/var/lib/mysql
container_name: mysql
volumes:
mysqldata:
driver: "local"
Now I need to create another project that uses MySQL v.5.6
So how should look the same file for this project? Especially volumes, service name and container name.
Thanks.
You can use multiple services and volumes in one compose YAML definition. Like this:
version: "3"
services:
mysql-v1:
image: mysql:5.5
ports:
- 13307:3306
environment:
- MYSQL_ROOT_PASSWORD=123
volumes:
- mysqldata-v1:/var/lib/mysql
container_name: mysql-v1
mysql-v2:
image: mysql:5.6
ports:
- 13308:3306
environment:
- MYSQL_ROOT_PASSWORD=123
volumes:
- mysqldata-v2:/var/lib/mysql
container_name: mysql-v2
volumes:
mysqldata-v1:
driver: "local"
mysqldata-v2:
driver: "local"
You configure image, container name and volume to your liking in such way.
Another approach would be to use env vars as parameters. You can configure the compose in such a way:
version: "3"
services:
mysql:
image: mysql:5.5
ports:
- ${port}:3306
environment:
- MYSQL_ROOT_PASSWORD=123
volumes:
- mysqldata:/var/lib/mysql
container_name: mysql-port-${port}
volumes:
mysqldata:
driver: "local"
Then all you need to do is "export port=2222" for example to set the port to 2222 and then use the compose. This will set both the port and the name of the container to to mysql-port-2222.

Why did mysql data ownership change to systemd-journal-remote after running a docker container

I have the mysql database stored in /home/mysql instead of /var/lib/mysql. The directory used to be owned by mysql. However, when I run the command docker-compose up with this yml file:
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
environment:
- "ES_JAVA_OPTS=-Xms750m -Xmx750m"
- bootstrap.memory_lock=false
site:
build: .
volumes:
- "./app:/app"
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.19.0.2
depends_on: ['elasticsearch','mariadb']
ports:
- "3000:3000"
The docker container is able to run, but the entire folder and files in /home/mysql are owned by systemd-journal-remote, which causes the node server fails to connect to mariadb. I have to stop the docker instance, restore the mysql folder ownership and delete ib_logfile0 and ib_logfile1.
Why does mounting /home/mysql cause such a fatal problem?
Update:
My solution is to add user: "mysql":
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
user: "mysql"
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
environment:
- "ES_JAVA_OPTS=-Xms750m -Xmx750m"
- bootstrap.memory_lock=false
site:
build: .
volumes:
- "./app:/app"
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.19.0.2
depends_on: ['elasticsearch','mariadb']
ports:
- "3000:3000"
You should start Docker's container with --user parameter. If you do this and set the same uid:gid as owner of the MySQL storage you will no have problems with permissions. You have to check how exactly to do this in Docker Compose because I show you example for normal command line execution
Most likely, uid of your user systemd-journal-remote is the same as uid of user mysqld in container. Check with ls -n. To avoid confusion, either use common uids, perhaps test as root:root with chmod o+rwx.