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.
Related
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
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
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
I dockerized a Laravel project and I want to run PhpMyAdmin while running beside MySQL.
My problem is I can't login PhpMyAdmin in PhpMyAdmin page.
Here is the error: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known.
This is the content of my docker-compose.yml file:
version: '3'
networks:
laravel:
services:
mysql:
image: mysql:5.7.22
container_name: mysql
tty: true
restart: unless-stopped
ports:
- "4506:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
MYSQL_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- mysql
ports:
- 8181:80
environment:
MYSQL_USERNAME: homestead
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
What am I missing?
You need to specify the PMA_HOST environment variable to tell phpmyadmin where to find the mysql server (in this case, the hostname is simply the service name, mysql):
version: '3'
networks:
laravel:
services:
mysql:
image: mysql:5.7.22
container_name: mysql
tty: true
restart: unless-stopped
ports:
- "4506:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
MYSQL_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- mysql
ports:
- 8181:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: homestead
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
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