0
votes

When I try to add a command php artisan migrate to the docker file, an error occurs:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = vacancylist and table_name = migrations)

Dockerfile

FROM php:7.2.10-fpm
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN apt-get update -y && apt-get install -y \
build-essential \
mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
WORKDIR /app
COPY . /app
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install
RUN php artisan migrate
CMD php artisan serve --host=0.0.0.0 --port=80

docker-compose.yml

version: '2'
services:
  app:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/app
    env_file: .env
    working_dir: /app
#    command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0'
    depends_on:
      - db
    links:
      - db
  db:
    image: "mysql:5.7.25"
    environment:
      - MYSQL_ROOT_PASSWORD=newrootpassword
      - MYSQL_DATABASE=vacancylist
      - MYSQL_USER=root
      - MYSQL_PASSWORD=newrootpassword
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
    volumes:
      - ./data/:/var/lib/mysql
    ports:
      - "3306:3306"

.env

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=vacancylist
DB_USERNAME=root
DB_PASSWORD=newrootpassword
1

1 Answers

0
votes

All you configuration looks correct. One thing I would fix is remove links section - it is obsolete and not recommended for use anymore.

I would suggest that database simply takes more time for startup comparing to php and that is your problem. depends_on section doesnt solve this issue - it only controls the startup order. Try adding a little pause in php or use wait-for-it toolkit - it is specially designed for such cases: it waits until host/port become available.