How can I add phpMyAdmin in docker-compose - mysql

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

Related

Docker: can't connect to mysql server in laravel

I have a problem that I cannot connect to mysql and I use docker. phpmyadmin does not allow creating database and project error C:\xampp\htdocs\news\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70. Please help me, thank you very much!!!
my error:
docker-compose.yaml:
version: '3.8'
services:
app:
build:
context: .
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- "database"
web:
build:
context: .
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- "app"
- "database"
ports:
- 80:80
database:
image: mysql
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_DATABASE: news
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
ports:
- "3306:3306"
pma:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOST=database
- PMA_USER=${DB_USERNAME}
- PMA_PASSWORD=${DB_PASSWORD}
- PMA_PORT=${DB_PORT}
ports:
- 3400:80
depends_on:
- database
volumes:
dbdata:
my .env:
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=news
DB_USERNAME=laravel-user
DB_PASSWORD=laravel-password
Add network
version: '3.8'
services:
app:
build:
context: .
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- "database"
networks:
- app-network
web:
build:
context: .
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- "app"
- "database"
ports:
- 80:80
networks:
- app-network
database:
image: mysql
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_DATABASE: news
MYSQL_ROOT_PASSWORD: password
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
ports:
- "3306:3306"
networks:
- app-network
pma:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOST=database
- PMA_USER=${DB_USERNAME}
- PMA_PASSWORD=${DB_PASSWORD}
- PMA_PORT=${DB_PORT}
ports:
- 3400:80
depends_on:
- database
networks:
- app-network
volumes:
dbdata:
networks:
app-network:
driver: bridge
change laravel .env
DB_HOST=host.docker.internal

mysql database name and credentials inside docker container not updating

I have a LAMP stack with laravel setup in docker-compose containers,everything seems to be working fine so far. I initially had a test database with the name of homestead setup in docker-compose.yml that was just there for testing now i want to change it to something more meaningful like my project name. let me show you my docker-compose.yml
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- 80: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
environment:
MYSQL_DATABASE: alis
MYSQL_USER: alis
MYSQL_PASSWORD: pass
MYSQL_ROOT_PASSWORD: pass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
volumes:
- ./mysql:/var/lib/mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
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
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
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
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
the problem is phpmyadmin is still using the old database and credentials , it tried the following solutions
docker-compose down
docker-compose down -v
then
docker-compose up -d
this didnt work.Out of frustration even thought this is probably not the best way to do it, I removed all containers, all images and then ran
docker-compose up -d
to re-install everything from scratch to my surprise phpmyadmin still picks up the old database config.Does anyone know the reason for this behaviour?
Your phpmyadmin service tries to connect as root:secret (MYSQL_ROOT_PASSWORD) while your mysql service defines root:pass (MYSQL_ROOT_PASSWORD) as admin user.
See the available configuration here:
https://hub.docker.com/_/phpmyadmin
https://hub.docker.com/_/mysql

Docker-compose database table reset

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

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.

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.