1

it's my first time using docker so sorry if I'm making dumb mistakes. I'm trying to setup a docker container for laravel development. These are my files:

Dockerfile:

FROM php:8-fpm

COPY . /app
WORKDIR /app

COPY composer.lock composer.json ./
RUN apt-get update -y && apt-get install -y sendmail libpng-dev

RUN apt-get update && apt-get install -y \
    build-essential \
    curl

RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
      gd \
      zip 
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer update
RUN composer install

docker-compose.yaml:

version: '3.9'
name: caas-portal
services:
      
  front:
    build:
      context: ./webapp
      target: builder
    ports:
      - 4200:4200
    volumes:
      - ./webapp:/project
      - /project/node_modules

  mysql:
    container_name: mysql
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=db
    ports:
      - 3307:3306/tcp
    networks:
      - laravel

  elasticsearch:
    container_name: elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.7
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200/tcp
      - 9300:9300/tcp
    volumes:
        - 'elasticsearch:/usr/share/elasticsearch/data'
    networks:
      - laravel

  app:
    container_name: back
    build: .
    ports:
      - 8000:80
    networks:
      - laravel
      
networks:
  laravel:
    driver: bridge

volumes:
    elasticsearch:
        driver: local

And this is the relevant part of my .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=password

When I run docker exec -it <container> sh and inside shell I run php artisan migrate I get the following error:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = db and table_name = migrations and table_type = 'BASE TABLE')

I've seen similar posts, and tried to follow the answers given but still no luck. Where is the mistake, and how can it be corrected?

  • I'm going to give a wild guess here that this is because you changed the default port of mysql from 3306 to 3307 without making the same changes on whatever the php script is using to connect. – Jorge Campos Jan 05 '23 at 23:39
  • Hello, thank you for answering. I don't think that's it because when I run it directly on my host it works fine. – Julia Olcese Jan 05 '23 at 23:50
  • Does that mean you changed the PHP script (laravel configs?) to explicitly connect to mysql using the port 3307? – Jorge Campos Jan 06 '23 at 00:45
  • Here is a similar post where some answers mention the docker problem also the port problem I mentioned above, see if you find something that can help there: https://stackoverflow.com/q/29395452/460557 – Jorge Campos Jan 06 '23 at 00:48
  • Connections between containers ignore `ports:`. You need to use the standard MySQL port 3306 in your `DB_PORT` variable. – David Maze Jan 06 '23 at 01:16

1 Answers1

0

In your .env, try DB_PORT=3306.


According to Docker on Networking in Compose,

It is important to note the distinction between HOST_PORT and CONTAINER_PORT. [...] Networked service-to-service communication uses the CONTAINER_PORT.

If we apply this concept to your docker-compose.yaml, we know that the mysql service has a host port of 3307 while having a container port 3306.

Now if you want a your app service to connect to your mysql service (service-to-service communication), then within your app container you should connect using the mysql container port 3306, not 3307.

Christian
  • 553
  • 4
  • 16