1

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.

enter image description here

I am wondering, can I define the existing MYSQL container configurations on the db context?

3
  • Can you show us all your docker-compose.yml contents? Commented Nov 5, 2019 at 9:22
  • What's the specific problem you're encountering? Have you read through Networking in Compose in the Docker documentation? Commented Nov 5, 2019 at 10:59
  • @MuhammadSipra Post your env too..I mean env for the DB_*.. Commented Nov 5, 2019 at 19:21

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

8 Comments

the issue is still the same, I have the case 1: I am using laravel on docker. and the script created 2 containers 1st for laravel 2nd for mysql
Have you added networks: - myapp-network to your laravel container? docker connects containers in one network, and inside container you can call other container by name
also, if you use 1 option - change DB_PORT=3306
Yes , as per your answer i haeve added the networks in db context as well as I have also defined networks: myapp-network: driver: bridge in it.
I think, if I have an MYSQL docker container that is already running on docker-machine, I just need to add the configuration in my laravel .env file. and remove MYSQL configs from the docker-compose.yml file. What do say?
|
1

In .env file:

DB_HOST=db
DB_PORT=3306
DB_DATABASE=your database name
DB_USERNAME=root
DB_PASSWORD=root

1 Comment

3007 is the published port. Since you're using db as host, then port should be 3006.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.