I am working on a laravel project where I successfully configured the docker-compose with laravel using built in images (nginx, mysql, php etc).
The containers are working fine even the data persistence is working correct. But now i want to connect the docker-compose to remote database rather then using the sql container database.
It can the the localhost database that is on my local system may be in xampp or it can be an AWS remote database. In simple words the docker should pick the database outside of container.
I have tried different solution using the IP address and make changes to .env and docker-compose.yml but i didn't find any solution.
Here is my default configurations for docker-compose.yml :
version: '3'
networks:
laravel:
services:
site:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
ports:
- 81:80
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- mysql
- phpmyadmin
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- 3307:3306
environment:
MYSQL_DATABASE: test_db
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
volumes:
- ./mysql:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: secret
UPLOAD_LIMIT: 1G
networks:
- laravel
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
volumes:
mysql:
And this is how my .env looks like:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=secret
As mentioned above it is working fine but i am confused about how to integrate/configure my local database or remote database like AWS with docker-compose in laravel. I don't want to push my data with the sql image.
I would appreciate if someone might help me in this regard about what changes are required and where to implement them.
Thanks
Related
I am new to docker, I am trying to connect the existing MySQL container to my Laravel application through docker-compose.
Where MySQL docker-compose.yml file is like
version: '3'
services:
web:
container_name: ${APP_NAME}_web
build:
context: ./docker/web
ports:
- "9000:80"
volumes:
- ./:/var/www/app
depends_on:
- db
db:
container_name: ${APP_NAME}_db
image: mysql:5.7
ports:
- "3307:3306"
restart: always
volumes:
- dbdata:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_docker_db
volumes:
dbdata:
driver: local
driver_opts:
type: none
device: /storage/docker/laravel_mysql_data
o: bind
This script is downloading the latest mysql version and making a new container. I just want to connect MYSQL container with my application
Where my existing MYSQL container is created with a specific IP using a bridge network and running on docker-machine.
I am wondering, can I define the existing MYSQL container configurations on the db context?
Try this docker-compose file:
version: '3.5'
services:
db:
container_name: myapp_db
image: mysql:5.7
ports:
- "3307:3306"
restart: always
volumes:
- dbdata:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_docker_db
networks:
- myapp-network
networks:
myapp-network:
driver: bridge
Situation has two cases:
1. If your laravel instance is in docker too - just add to .env file next rows:
DB_HOST=myapp_db
DB_PORT=3306
DB_DATABASE=laravel_docker_db
DB_USERNAME=root
DB_PASSWORD=root
2.If you don't use docker for laravel - just change variable DB_HOST=localhost
In .env file:
DB_HOST=db
DB_PORT=3306
DB_DATABASE=your database name
DB_USERNAME=root
DB_PASSWORD=root
I am trying to set up a local environment for web development (LAMP stack) using Docker.
All websites live in folder called Sites with this structure:
/Sites
-- site1.local
-- www
-- site2.local
-- www
For every website I need its own version of PHP and MySQL.
So far I was able to run one local website with this docker-compose.yml (uses php:7.1-apache):
version: "3"
services:
webserver:
build:
context: ./bin/webserver
container_name: 'sp-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
mysql:
build: ./bin/mysql
container_name: 'sp-mysql'
restart: 'always'
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: sp-demo
MYSQL_USER: sp-demo
MYSQL_PASSWORD: sp-demo
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'rb-phpmyadmin'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- '8080:80'
volumes:
- /sessions
redis:
container_name: 'rb-redis'
image: redis:latest
ports:
- "6379:6379"
The website is available at http://localhost:80
Questions:
1) How do I make it accesible by domain domain, for example, http://site1.local instead of http://localhost:80? I heard I need some Nginx Proxy for this (https://github.com/jwilder/nginx-proxy), but I can't understand how to set it up with Apache in my case.
2) How to set up the second website (http://site2.local) the same way to run it simultaneously? As far as I understand, I would need to change all ports (80, 443 and 3306), otherwise I will have a conflict when I run docker-compose up -d? Is it possible without changing ports?
Thanks for the answers!
Losing my mind on this one.
I've got a Lumen and MySQL setup in a Docker container. Most everything is good to go. I can run the container and access Lumen through a browser. I can access MySQL through Sequel Pro, no problem. And I can run php artisan migrate and it works fine.
But if I try to do anything through Lumen in the browser, it won't connect to the database, and it gives me the Connection refused error.
I'm using Lumen 5.7.7 and .env file looks like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
docker-compose.yml looks like this:
version: '3'
services:
api:
build:
context: .
dockerfile: .docker/Dockerfile
image: laravel-docker
ports:
- 8080:80
depends_on:
- mysqldb
volumes:
- .:/srv/app
# container_name:
mysqldb:
image: mysql:5.7
container_name: mysqldb
command: mysqld --user=root --verbose
volumes:
- ./schemadump.sql:/docker-entrypoint-initdb.d/schemadump.sql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
EDIT: Ran phpinfo() on the site and on the command line and realized that they aren't even the same version, let alone the same exact details. Could that be the problem here? Looking at it more...
Well, it is working now, sort of. I've got it now working from the browser and not from the command line, which I can work with. From a comment above, it looks like a missing "link" section connecting the api to the database was the issue. .env file is unchanged, but the docker-compose.yml now looks like this:
version: '3'
services:
api:
build:
context: .
dockerfile: .docker/Dockerfile
image: laravel-docker
ports:
- 8080:80
links:
- mysql
volumes:
- .:/srv/app
environment:
DB_HOST: mysql
DB_DATABASE: test
DB_USERNAME: test
DB_PASSWORD: test
mysql:
image: mysql:5.7
ports:
- 13306:3306
environment:
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
Added the "environment" section to the api section of the yml file. That helped out. And changed the port so it has a different external port compared to the internal port. Not 100% which part made it work right, but it is working OK for now and I'm not about to change it more.
I have installed laradoc as per documentation but I am unable to login phpmyadmin.
here is my docker-compose.yaml file:
phpmyadmin:
build: ./phpmyadmin
environment:
- PMA_ARBITRARY=1
- MYSQL_USER=${PMA_USER}
- MYSQL_PASSWORD=${PMA_PASSWORD}
- MYSQL_ROOT_PASSWORD=${PMA_ROOT_PASSWORD}
ports:
- "${PMA_PORT}:80"
depends_on:
- "${PMA_DB_ENGINE}"
networks:
- frontend
- backend
and here is .env file
PMA_DB_ENGINE=mysql
PMA_USER=default
PMA_PASSWORD=secret
PMA_ROOT_PASSWORD=secret
PMA_PORT=8080
Difficult to reproduce the error from the given info but this small setup works so maybe you can find what went wrong?
First bring your existing stack down and delete dangling volumes:
$ docker-compose down
$ docker volume prune
Start the following docker-compose.yaml:
version: '3.1'
services:
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- MYSQL_USER=${PMA_USER}
- MYSQL_PASSWORD=${PMA_PASSWORD}
- MYSQL_ROOT_PASSWORD=${PMA_ROOT_PASSWORD}
ports:
- "${PMA_PORT}:80"
depends_on:
- "${PMA_DB_ENGINE}"
networks:
- frontend
- backend
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: db
MYSQL_USER: default
MYSQL_PASSWORD: secret
networks:
- frontend
- backend
networks:
frontend:
backend:
info (I had to use mysql5.7 otherwise I was facing this bug:
docker-compose up -d
Creating network "test_frontend" with the default driver
Creating network "test_backend" with the default driver
Creating test_mysql_1 ... done
Creating test_phpmyadmin_1 ... done
I know I'm a bit 'messing' with the syntax + you can use the env vars too for mysql but I had to be quick.
May be this question asked few times before but I did't get a valid answer which can solve my problem.
I am trying to run phpmyadmin in docker on different container using docker-compose but It always through the following error:
#2002 - Connection refused — The server is not responding (or the local server's socket is not correctly configured).
My docker compose file contains the following code:
version: "2"
services:
web:
build: .
ports:
- "80:80"
networks:
- web
volumes:
- .:/code
restart: always
db:
image: "mysql:5"
volumes:
- ./mysql:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: toor
MYSQL_DATABASE: phpapp
networks:
- web
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8000:80"
restart: always
networks:
- web
networks:
web:
driver: bridge
In web container I am trying to connect with database and it works fine, but the problem occur with phpmyadmin connection
Any help would be appreciated. :)
Interestng enough, I have your compose-file running and phpmyadmin is accessible
from host.
Had to change port 8000 to 8004 though (port 8000 is occupied on my host).
In case your db-container does not start fast enough for phpmyadmin to connect, I suggest adding depends_on into phpmyadmin service. Makes sure db starts before phpmyadmin.
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8004:80"
restart: always
depends_on:
- db
networks:
- web
Please show logs from docker-compose up if problem persists.
Now you need to add command to mysql service for connecting to phpmyadmin.
command: --default-authentication-plugin=mysql_native_password
version: "2"
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: drupal
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- /var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
PMA_HOST: db
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test