0

I have a mysql service as a docker container.

Here's the config at docker-compose.yml:

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  mysql:
    image: mysql:5.7.22
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports:
      - "3307:3306"
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel



php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www
    ports:
      - "9000:9000"
    networks:
      - laravel

And .env file inside laravel app:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

I can connect to container's mysql from my host:

mysql -uhomestead -p -h localhost -P 3307

and there are information_schema and homestead databases listed on show databases;

But when I try to run artisan migrate command, I get enter image description here

I cannot seem to locate the issue, since I've been following this tutorial.

2
  • Could you show the compose section of Laravel container? Commented Jul 25, 2019 at 11:36
  • @JanshairKhan yes.. I have updated the post. Commented Jul 25, 2019 at 11:42

2 Answers 2

1

It seems to me that you are connecting to MySQL from Nginx via the External (host) Port. Edit the .env file as:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

And add .env below in the Nginx service of the docker-compose.yml file:

  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    env_file:
     - ./.env
    networks:
      - laravel
...

Make sure .env file is at the directory where docker-compose.yml is and try again. It should work.

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

2 Comments

thanks.. how silly of me to use 3307 inside .env. :) I didn't have to add .env to nginx service though. What does it do and why is it necessary?
It is okay if you don't add .env to the nginx service. docker-compose will pick it up by default. This is used when you have a number of environment variables to be used in various services of the docker-compose rather than replicating them in each service and making docker-compose difficult to read & manage.
0

If you find yourself unable to run php artisan commands which require database connection after changing "DB_HOST=localhost" to "DB_HOST=mysql".

commands such as:

  • php artisan migrate
  • php artisan db:seed

Solution:

Add the following to your hosts file: 127.0.0.1 [name_of_container]

Name of container in your case is: mysql

Comments

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.