Evgeny Musonov
Evgeny Musonov

Reputation: 331

Why php artisan migrate doesn't work with Docker?

I use docker and do laravel new blog, after that I do php artisan migrate and get this error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = books and table_name = migrations and table_type = 'BASE TABLE')

So what I do wrong?

This is my docker-compose.yml, I use mariaDB and adminer instead phpmyadmin

version: '3'

services:

  web:
    build: ./web
    environment:
      - APACHE_RUN_USER=#1000
    volumes:
      - ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
      - ./web/php.ini:/usr/local/etc/php/php.ini
    ports:
      - 8080:80
    working_dir: ${APP_PATH_CONTAINER}

  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 12345
    volumes:
      - ${DB_PATH_HOST}:/var/lib/mysql

  adminer:
    image: adminer
    restart: always
    ports:
      - 6080:8080

  composer:
    image: composer:1.6
    volumes:
      - ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
    working_dir: ${APP_PATH_CONTAINER}
    command: composer install

Upvotes: 4

Views: 7442

Answers (1)

pr1nc3
pr1nc3

Reputation: 8358

In your db container add :

ports:
  - "3306:3306"
environment:
  MYSQL_ROOT_USER: root
  MYSQL_ROOT_PASSWORD: secret
  MYSQL_DATABASE: databaseName
  MYSQL_USER: root
  MYSQL_PASSWORD: secret
  SERVICE_TAGS: dev
  SERVICE_NAME: mysql

Then go inside your container using:

docker exec -it web bash

This will open the console of your laravel container and in there run

php artisan optimize 

To make use of your env file variables.

A step you must take before that is to create your database inside docker using mysql workbench or a similar tool.

You can connect using below credentials:

Host: 0.0.0.0
username : root
password : secret
port : 3306

And then create an empty database using the name of your database you are using in your env file & in the docker_compose

Your laravel env file should be changed a bit so it loads the mysql container.

DB_CONNECTION=mysql
DB_HOST=db //This is the container name you use
DB_PORT=3306
DB_DATABASE=myDatabaseName
DB_USERNAME=root
DB_PASSWORD=secret

Comment Answer:

The error in your comment means that you are missing gd extension. Inside your docker file use the below lines:

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

Upvotes: 3

Related Questions