Docker-compose database table reset - mysql

I am new to docker technology. I use docker-compose in my project and every time I start docker-compose my database tables are reset.
docker-compose.yaml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/php/Dockerfile
image: php-7.4.1-extended
container_name: php
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I found an example of docker-compose.yaml file from some sources and made changes on it. What should I do to fix the problem.

Your docker image is storing its data within the image, which isn't persisted. You need to tell Docker to store the data on a volume, instead. This is a bit version-dependent, but something like
#MySQL Service
db:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
- "./.mysql-data/db:/var/lib/mysql"
And then delete
dbdata:
driver: local

Related

multiple mysql containers only one is working

What I am doing wrong? Only one is working, I am using phpmyadmin for accesing it, and in the log files I got this in all the others containers:
2021-02-19 16:13:24+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
2021-02-19 16:13:24+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-02-19 16:13:24+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
And phpmyadmin says mysqli::real_connect(): (HY000/2002): Connection refused (only mysql1 is connection)
This is my docker-compose
version: '3'
services:
mysql1:
image: mysql:5.7.33
container_name: mysql1
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db1
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db1
MYSQL_PASSWORD: db1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql1:/var/lib/mysql/
- ./etc/mysql/my1.cnf:/etc/mysql/my.cnf
networks:
- app-network
mysql2:
image: mysql:5.7.33
container_name: mysql2
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: db2
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db2
MYSQL_PASSWORD: db2
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql2:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my2.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql3:
image: mysql:5.7.33
container_name: mysql3
restart: unless-stopped
tty: true
ports:
- "3308:3306"
environment:
MYSQL_DATABASE: db3
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db3
MYSQL_PASSWORD: db3
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql3:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my3.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql4:
image: mysql:5.7.33
container_name: mysql4
restart: unless-stopped
tty: true
ports:
- "3309:3306"
environment:
MYSQL_DATABASE: db4
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db4
MYSQL_PASSWORD: db4
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- mysql4:/var/lib/mysql/
- ./etc/mysql/my-4.cnf:/etc/mysql/my.cnf
networks:
- app-network
# PHPMYADMIN
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
environment:
PMA_USER: root
PMA_PASSWORD: root
PMA_HOSTS: mysql1,mysql2,mysql3,mysql4
PMA_PORTS: 3306,3307,3308,3309
links:
- mysql1
- mysql2
- mysql3
- mysql4
restart: always
ports:
- 8080:80
volumes:
- phpmyadmin:/sessions
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
phpmyadmin:
driver: local
studioweb:
driver: local
sessions:
driver: local
mysql1:
driver: local
mysql2:
driver: local
mysql3:
driver: local
mysql4:
driver: local
They all use same network and port is used. It does not corrupt or show an error but not working. I added MYSQL_TCP_PORT to each service to work on different port(3307 not included) and it is working fine.
version: '3'
services:
mysql1:
image: mysql:5.7.33
container_name: mysql1
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db1
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db1
MYSQL_PASSWORD: db1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3306
volumes:
- mysql1:/var/lib/mysql/
- ./etc/mysql/my1.cnf:/etc/mysql/my.cnf
networks:
- app-network
mysql2:
image: mysql:5.7.33
container_name: mysql2
restart: unless-stopped
tty: true
ports:
- "3310:3310"
environment:
MYSQL_DATABASE: db2
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db2
MYSQL_PASSWORD: db2
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3310
volumes:
- mysql2:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my2.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql3:
image: mysql:5.7.33
container_name: mysql3
restart: unless-stopped
tty: true
ports:
- "3308:3308"
environment:
MYSQL_DATABASE: db3
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db3
MYSQL_PASSWORD: db3
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3308
volumes:
- mysql3:/var/lib/mysql/
- ./var/log/mysql:/var/log/mysql
- ./etc/mysql/my3.cnf:/etc/mysql/my.cnf
- ./var/transit/:/var/transit
networks:
- app-network
mysql4:
image: mysql:5.7.33
container_name: mysql4
restart: unless-stopped
tty: true
ports:
- "3309:3309"
environment:
MYSQL_DATABASE: db4
MYSQL_ROOT_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: db4
MYSQL_PASSWORD: db4
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_TCP_PORT: 3309
volumes:
- mysql4:/var/lib/mysql/
- ./etc/mysql/my-4.cnf:/etc/mysql/my.cnf
networks:
- app-network
# PHPMYADMIN
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
environment:
PMA_USER: root
PMA_PASSWORD: root
PMA_HOSTS: mysql1,mysql2,mysql3,mysql4
PMA_PORTS: 3306,3310,3308,3309
links:
- mysql1
- mysql2
- mysql3
- mysql4
restart: always
ports:
- 8080:80
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
phpmyadmin:
driver: local
studioweb:
driver: local
sessions:
driver: local
mysql1:
driver: local
mysql2:
driver: local
mysql3:
driver: local
mysql4:
driver: local

How can I add phpMyAdmin in docker-compose

Hello everyone I have this docker-compose file
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: gestionParking
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
ports:
- "9000:9000"
networks:
- laravel
composer:
build:
context: .
dockerfile: composer.dockerfile
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
networks:
- laravel
entrypoint: ['composer', '--ignore-platform-reqs']
npm:
image: node:13.7
container_name: npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
user: laravel
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
And I want to add phpMyAdmin to have graphical interface of my database, I try this but it doesn't work.
...
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: gestionParking
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: secret
PMA_HOST: mysql
PMA_PORT: 3306
restart: always
ports:
- "8081:80"
networks:
- laravel
I think it's possible, but I think I'm going about it wrong.
I know we need to make a connection between mysql service and phpmyadmin but I don't know how to do it.
Can I help me please
version: '3'
services:
nginx:
container_name: nginx
image: nginx:1.17
restart: always
ports:
- "9998:80"
volumes:
- ../:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
container_name: php
build:
context: .
dockerfile: Dockerfile
restart: always
volumes:
- ../:/var/www
- ~:/home
- ./php-config/php.ini:/usr/local/etc/php/php.ini
app:
build:
context: .
dockerfile: Dockerfile
image: hakase-labs/laravel
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html
volumes:
- ./:/var/www/html
networks:
- mynet
composer:
container_name: composer
image: composer:1.9.0
command: tail -f /dev/null
volumes:
- ../:/var/www
mysql:
container_name: mysql
image: mysql:5.7
command: mysqld --max-allowed-packet=64M --character-set-server=utf8 --collation-server=utf8_unicode_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
volumes:
# Mount mysl data directory to keep it perisstent on host system.
# Use this only in development environment
# Mysql cannot write to data folder as it is owned by user on host.
# So chown 999:999 data folder when it is first created
# Todo: For some reason we are not able to mount log directory from host to mysql container. We need to fix this in future so that we can better manage mysql logs from host machine - Harsha
#- ~/storage/mysql/log:/var/log/mysql
- ~/storage/mysql/data:/var/lib/mysql
environment:
- "MYSQL_ROOT_PASSWORD=pasword"
- "MYSQL_DATABASE=name"
- "MYSQL_USER=name"
- "MYSQL_PASSWORD=password"
ports:
- "3306:3306"
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
environment:
- PMA_HOST=mysql
- PMA_PORT=3306
ports:
- 8001:80
volumes:
- /sessions

Docker-compose file with multiple database containers in Laravel

I have a Laravel project with two mysql database containers. During seeding we're fetching data from database one and mapping/importing this into database two.
docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- "9000:9000"
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.29
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: aas
MYSQL_USER: aas_admin
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
access_db:
image: mysql:5.7.29
container_name: access_db
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: access_aas
MYSQL_USER: aas_admin
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- accessdbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/accessmy.cnf
networks:
- app-network
#Redis
cache:
image: redis:rc-alpine3.11
container_name: cache
ports:
- "6382:6379"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
accessdbdata:
driver: local
.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=aas
DB_USERNAME=aas_admin
DB_PASSWORD=secret
ACCESS_DB_CONNECTION=access
ACCESS_DB_HOST=access_db
ACCESS_DB_PORT=3307
ACCESS_DB_DATABASE=access_aas
I can access the database through SequelPro using the above credentials for both databases. I can also create migrations but seeding seems to be a problem when I query data from the access_db
When I try seed the database I get the following error:
Turns out in the .env file I needed to change:
.env
ACCESS_DB_PORT=3307 => ACCESS_DB_PORT=3306
I believe the reason for this is that Laravel is looking for a container inside the host in which case port 3307 doesn't exist.

Connecting separate WordPress Docker-Compose Stacks to an external MySQL and phpMyAdmin Containers

How to achieve the following with docker-compose:
Have a separate container for each WordPress environment.
A single container dedicated for MySQL and phpMyAdmin.
Setting up MySQL and phpMyAdmin:
version: '3.7'
services:
#Database
db:
image: mysql:latest
volumes:
- db_data:/var/lib/mysql
ports:
- "1207:3306"
restart: always
container_name: my_wordpress_db
environment:
MYSQL_ROOT_PASSWORD: password
#phpMyAdmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
container_name: my_wordpress_phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOST: db:3306
MYSQL_ROOT_PASSWORD: password
volumes:
db_data:
networks:
default:
external:
name: my_wordpress_network
Setting up the WordPress docker environment:
version: '3.7'
services:
#Wordpress
wordpress:
image: wordpress:latest
external_links:
- my_wordpress_db:db
volumes: ['./:/var/www/html']
ports:
- "33003:80"
restart: always
container_name: my_project_container
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: my_project_db
networks:
default:
external:
name: my_wordpress_network

Docker - Use same network and database for another app?

I've set up a development environment for an app on docker in which I'm using a specific network to connect the app to the database
version: '3'
services:
app:
image: appimg
container_name: appwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:80
depends_on:
- mysql
network:
- appnet
mysql:
image: mysql:5.7
container_name: appdb
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: homestead
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
ports:
- 3306:3306
network:
- appnet
volumes:
dbdata:
driver: 'local'
network:
- appnet
My question is can I use the same network to connect to the same database from another app? something like this perhaps?
version: '3'
services:
anotherapp:
image: anotherappimg
container_name: anotherappwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:80
depends_on:
- mysql
network:
- appnet
mysql:
image: mysql:5.7
container_name: anotherappdb
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: homestead
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
ports:
- 3306:3306
network:
- appnet
volumes:
dbdata:
driver: 'local'
network:
- appnet
Yes it's possible, you can have the setup done like this:
docker-compose.base.yml:
version: '3'
services:
mysql:
image: mysql:5.7
container_name: anotherappdb
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: root
MYSQL_USERNAME: homestead
MYSQL_PASSWORD: password
volumes:
- dbdata:/var/lib/mysql
ports:
- 3306:3306
network:
- appnet
volumes:
dbdata:
driver: 'local'
network:
- appnet
docker-compose.app.yml:
version: '3'
services:
app:
image: appimg
container_name: appwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:80
depends_on:
- mysql
network:
- appnet
docker-compose.anotherapp.yml:
version: '3'
services:
anotherapp:
image: anotherappimg
container_name: anotherappwww
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/var/www/html
ports:
- 9000:1080
depends_on:
- mysql
network:
- appnet
Then you can start everything using:
docker-compose -f docker-compose.base.yml -f docker-compose.app.yml -f docker-compose.anotherapp.yml up -d
If you want to start only anotherapp, you'll remove -f docker-compose.app.yml from the previous command.
NOTE: I've changed the port mapping for anotherapp since they'd conflict with app if started at the same time.