0

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

Amir Khan
  • 183
  • 2
  • 16
  • Does this answer your question? [Laravel Docker Container Cannot connect to remote AWS RDS Database](https://stackoverflow.com/questions/65958817/laravel-docker-container-cannot-connect-to-remote-aws-rds-database) – Lewis Smith Jun 09 '21 at 11:43
  • For local thing, read [forward-host-port-to-docker-container](https://stackoverflow.com/questions/17770902/forward-host-port-to-docker-container), someone there suggest `--net=Host` which would be `network_mode: host` in Docker-Compose world **WARNING:** merges container network with local (as if all Apps were outside of container). – Top-Master Jun 09 '21 at 17:48
  • @LewisSmith appologies for replying late, actually your mentioned link is related to integrating the RDS with docker remotely. My objective was to connect with localhost database and if i upload the project to server it should connect to the server database rather then to connect with the docker sql image. But now i am planing to use RDS with docker. – Amir Khan Jul 02 '21 at 05:52

0 Answers0